Removing CharSequenceUtils in favour of putting the code in StringUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1082046 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e5b0844c4f
commit
196067da97
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.commons.lang3;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Operations on {@code CharSequence} that are
|
|
||||||
* <code>null</code> safe.</p>
|
|
||||||
*
|
|
||||||
* <p>#ThreadSafe#</p>
|
|
||||||
*
|
|
||||||
* @author Apache Software Foundation
|
|
||||||
* @author Gary Gregory
|
|
||||||
* @since 3.0
|
|
||||||
* @version $Id$
|
|
||||||
*/
|
|
||||||
public class CharSequenceUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>{@code CharSequenceUtils} instances should NOT be constructed in
|
|
||||||
* standard programming. Instead, the static methods on the class should
|
|
||||||
* be used, such as {@code CharSequenceUtils.subSequence(cs, 4);}.</p>
|
|
||||||
*
|
|
||||||
* <p>This constructor is public to permit tools that require a JavaBean
|
|
||||||
* instance to operate.</p>
|
|
||||||
*/
|
|
||||||
public CharSequenceUtils() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* <p>Returns a new {@code CharSequence} that is a subsequence of this
|
|
||||||
* sequence starting with the {@code char} value at the specified index.</p>
|
|
||||||
*
|
|
||||||
* <p>This provides the {@code CharSequence} equivalent to {@link String#substring(int)}.
|
|
||||||
* The length (in {@code char}) of the returned sequence is {@code length() - start},
|
|
||||||
* so if {@code start == end} then an empty sequence is returned.</p>
|
|
||||||
*
|
|
||||||
* @param cs the specified subsequence, null returns null
|
|
||||||
* @param start the start index, inclusive, valid
|
|
||||||
* @return a new subsequence, may be null
|
|
||||||
* @throws IndexOutOfBoundsException if {@code start} is negative or if
|
|
||||||
* {@code start} is greater than {@code length()}
|
|
||||||
*/
|
|
||||||
public static CharSequence subSequence(CharSequence cs, int start) {
|
|
||||||
return cs == null ? null : cs.subSequence(start, cs.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -5021,7 +5021,7 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return new StringBuilder(strLen)
|
return new StringBuilder(strLen)
|
||||||
.append(Character.toTitleCase(cs.charAt(0)))
|
.append(Character.toTitleCase(cs.charAt(0)))
|
||||||
.append(CharSequenceUtils.subSequence(cs, 1))
|
.append(StringUtils.subSequence(cs, 1))
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5056,7 +5056,7 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return new StringBuilder(strLen)
|
return new StringBuilder(strLen)
|
||||||
.append(Character.toLowerCase(cs.charAt(0)))
|
.append(Character.toLowerCase(cs.charAt(0)))
|
||||||
.append(CharSequenceUtils.subSequence(cs, 1))
|
.append(StringUtils.subSequence(cs, 1))
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6384,4 +6384,24 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* <p>Returns a new {@code CharSequence} that is a subsequence of this
|
||||||
|
* sequence starting with the {@code char} value at the specified index.</p>
|
||||||
|
*
|
||||||
|
* <p>This provides the {@code CharSequence} equivalent to {@link String#substring(int)}.
|
||||||
|
* The length (in {@code char}) of the returned sequence is {@code length() - start},
|
||||||
|
* so if {@code start == end} then an empty sequence is returned.</p>
|
||||||
|
*
|
||||||
|
* @param cs the specified subsequence, null returns null
|
||||||
|
* @param start the start index, inclusive, valid
|
||||||
|
* @return a new subsequence, may be null
|
||||||
|
* @throws IndexOutOfBoundsException if {@code start} is negative or if
|
||||||
|
* {@code start} is greater than {@code length()}
|
||||||
|
*/
|
||||||
|
public static CharSequence subSequence(CharSequence cs, int start) {
|
||||||
|
return cs == null ? null : cs.subSequence(start, cs.length());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.commons.lang3;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests CharSequenceUtils
|
|
||||||
*
|
|
||||||
* @author Gary Gregory
|
|
||||||
* @version $Id$
|
|
||||||
*/
|
|
||||||
public class CharSequenceUtilsTest extends TestCase {
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
public void testConstructor() {
|
|
||||||
assertNotNull(new CharSequenceUtils());
|
|
||||||
Constructor<?>[] cons = CharSequenceUtils.class.getDeclaredConstructors();
|
|
||||||
assertEquals(1, cons.length);
|
|
||||||
assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
|
|
||||||
assertEquals(true, Modifier.isPublic(CharSequenceUtils.class.getModifiers()));
|
|
||||||
assertEquals(false, Modifier.isFinal(CharSequenceUtils.class.getModifiers()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
public void testSubSequence() {
|
|
||||||
//
|
|
||||||
// null input
|
|
||||||
//
|
|
||||||
Assert.assertEquals(null, CharSequenceUtils.subSequence(null, -1));
|
|
||||||
Assert.assertEquals(null, CharSequenceUtils.subSequence(null, 0));
|
|
||||||
Assert.assertEquals(null, CharSequenceUtils.subSequence(null, 1));
|
|
||||||
//
|
|
||||||
// non-null input
|
|
||||||
//
|
|
||||||
Assert.assertEquals(StringUtils.EMPTY, CharSequenceUtils.subSequence(StringUtils.EMPTY, 0));
|
|
||||||
Assert.assertEquals("012", CharSequenceUtils.subSequence("012", 0));
|
|
||||||
Assert.assertEquals("12", CharSequenceUtils.subSequence("012", 1));
|
|
||||||
Assert.assertEquals("2", CharSequenceUtils.subSequence("012", 2));
|
|
||||||
Assert.assertEquals(StringUtils.EMPTY, CharSequenceUtils.subSequence("012", 3));
|
|
||||||
//
|
|
||||||
// Exception expected
|
|
||||||
//
|
|
||||||
try {
|
|
||||||
Assert.assertEquals(null, CharSequenceUtils.subSequence(StringUtils.EMPTY, -1));
|
|
||||||
Assert.fail("Expected " + IndexOutOfBoundsException.class.getName());
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Assert.assertEquals(null, CharSequenceUtils.subSequence(StringUtils.EMPTY, 1));
|
|
||||||
Assert.fail("Expected " + IndexOutOfBoundsException.class.getName());
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests {@link org.apache.commons.lang3.StringUtils} - Substring methods
|
* Unit tests {@link org.apache.commons.lang3.StringUtils} - Substring methods
|
||||||
|
@ -312,4 +313,38 @@ public class StringUtilsSubstringTest extends TestCase {
|
||||||
assertEquals(4,
|
assertEquals(4,
|
||||||
StringUtils.countMatches("oooooooooooo", "ooo"));
|
StringUtils.countMatches("oooooooooooo", "ooo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
public void testSubSequence() {
|
||||||
|
//
|
||||||
|
// null input
|
||||||
|
//
|
||||||
|
Assert.assertEquals(null, StringUtils.subSequence(null, -1));
|
||||||
|
Assert.assertEquals(null, StringUtils.subSequence(null, 0));
|
||||||
|
Assert.assertEquals(null, StringUtils.subSequence(null, 1));
|
||||||
|
//
|
||||||
|
// non-null input
|
||||||
|
//
|
||||||
|
Assert.assertEquals(StringUtils.EMPTY, StringUtils.subSequence(StringUtils.EMPTY, 0));
|
||||||
|
Assert.assertEquals("012", StringUtils.subSequence("012", 0));
|
||||||
|
Assert.assertEquals("12", StringUtils.subSequence("012", 1));
|
||||||
|
Assert.assertEquals("2", StringUtils.subSequence("012", 2));
|
||||||
|
Assert.assertEquals(StringUtils.EMPTY, StringUtils.subSequence("012", 3));
|
||||||
|
//
|
||||||
|
// Exception expected
|
||||||
|
//
|
||||||
|
try {
|
||||||
|
Assert.assertEquals(null, StringUtils.subSequence(StringUtils.EMPTY, -1));
|
||||||
|
Assert.fail("Expected " + IndexOutOfBoundsException.class.getName());
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Assert.assertEquals(null, StringUtils.subSequence(StringUtils.EMPTY, 1));
|
||||||
|
Assert.fail("Expected " + IndexOutOfBoundsException.class.getName());
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue