-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/main/java/org/apache/commons/lang3/CharSequenceUtils.java svn ps svn:eol-style native src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966488 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
1 parent
4d479dc
commit 34658f0
Showing
2 changed files
with
153 additions
and
153 deletions.
There are no files selected for viewing
122 changes: 61 additions & 61 deletions
122
src/main/java/org/apache/commons/lang3/CharSequenceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Null-safe CharSequence utility methods. | ||
* | ||
* @author Gary Gregory | ||
*/ | ||
public class CharSequenceUtils { | ||
|
||
/** | ||
* Gets a CharSequence length or <code>0</code> if the CharSequence is | ||
* <code>null</code>. | ||
* | ||
* @param cs | ||
* a CharSequence or <code>null</code> | ||
* @return CharSequence length or <code>0</code> if the CharSequence is | ||
* <code>null</code>. | ||
* @since 3.0 | ||
*/ | ||
public static int length(CharSequence cs) { | ||
return cs == null ? 0 : cs.length(); | ||
} | ||
|
||
/** | ||
* Returns a new <code>CharSequence</code> that is a subsequence of this | ||
* sequence starting with the <code>char</code> value at the specified | ||
* index. The length (in <code>char</code>s) of the returned sequence is | ||
* <code>length() - start</code>, so if <code>start == end</code> then an | ||
* empty sequence is returned. </p> | ||
* | ||
* @param cs | ||
* the specified subsequence, may be null | ||
* @param start | ||
* the start index, inclusive | ||
* @return a new subsequence or null | ||
* | ||
* @throws IndexOutOfBoundsException | ||
* if <code>start</code> is negative or if <code>start</code> is | ||
* greater than <code>length()</code> | ||
* @since 3.0 | ||
*/ | ||
public static CharSequence subSequence(CharSequence cs, int start) { | ||
return cs == null ? null : cs.subSequence(start, cs.length()); | ||
} | ||
} | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Null-safe CharSequence utility methods. | ||
* | ||
* @author Gary Gregory | ||
*/ | ||
public class CharSequenceUtils { | ||
|
||
/** | ||
* Gets a CharSequence length or <code>0</code> if the CharSequence is | ||
* <code>null</code>. | ||
* | ||
* @param cs | ||
* a CharSequence or <code>null</code> | ||
* @return CharSequence length or <code>0</code> if the CharSequence is | ||
* <code>null</code>. | ||
* @since 3.0 | ||
*/ | ||
public static int length(CharSequence cs) { | ||
return cs == null ? 0 : cs.length(); | ||
} | ||
|
||
/** | ||
* Returns a new <code>CharSequence</code> that is a subsequence of this | ||
* sequence starting with the <code>char</code> value at the specified | ||
* index. The length (in <code>char</code>s) of the returned sequence is | ||
* <code>length() - start</code>, so if <code>start == end</code> then an | ||
* empty sequence is returned. </p> | ||
* | ||
* @param cs | ||
* the specified subsequence, may be null | ||
* @param start | ||
* the start index, inclusive | ||
* @return a new subsequence or null | ||
* | ||
* @throws IndexOutOfBoundsException | ||
* if <code>start</code> is negative or if <code>start</code> is | ||
* greater than <code>length()</code> | ||
* @since 3.0 | ||
*/ | ||
public static CharSequence subSequence(CharSequence cs, int start) { | ||
return cs == null ? null : cs.subSequence(start, cs.length()); | ||
} | ||
} |
184 changes: 92 additions & 92 deletions
184
src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,92 @@ | ||
/* | ||
* 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.nio.CharBuffer; | ||
|
||
import junit.framework.Assert; | ||
import junit.framework.TestCase; | ||
|
||
/** | ||
* Tests CharSequenceUtils | ||
* | ||
* @author Gary Gregory | ||
*/ | ||
public class CharSequenceUtilsTest extends TestCase { | ||
|
||
public void testLength_CharBuffer() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(CharBuffer.wrap(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(CharBuffer.wrap("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(CharBuffer.wrap(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(CharBuffer.wrap("ABCDEFGH"))); | ||
} | ||
|
||
public void testLength_String() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(null)); | ||
Assert.assertEquals(0, CharSequenceUtils.length("")); | ||
Assert.assertEquals(1, CharSequenceUtils.length("A")); | ||
Assert.assertEquals(1, CharSequenceUtils.length(" ")); | ||
Assert.assertEquals(8, CharSequenceUtils.length("ABCDEFGH")); | ||
} | ||
|
||
public void testLength_StringBuffer() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(new StringBuffer(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuffer("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuffer(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(new StringBuffer("ABCDEFGH"))); | ||
} | ||
|
||
public void testLength_StringBuilder() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(new StringBuilder(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuilder("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuilder(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(new StringBuilder("ABCDEFGH"))); | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
} | ||
/* | ||
* 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.nio.CharBuffer; | ||
|
||
import junit.framework.Assert; | ||
import junit.framework.TestCase; | ||
|
||
/** | ||
* Tests CharSequenceUtils | ||
* | ||
* @author Gary Gregory | ||
*/ | ||
public class CharSequenceUtilsTest extends TestCase { | ||
|
||
public void testLength_CharBuffer() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(CharBuffer.wrap(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(CharBuffer.wrap("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(CharBuffer.wrap(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(CharBuffer.wrap("ABCDEFGH"))); | ||
} | ||
|
||
public void testLength_String() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(null)); | ||
Assert.assertEquals(0, CharSequenceUtils.length("")); | ||
Assert.assertEquals(1, CharSequenceUtils.length("A")); | ||
Assert.assertEquals(1, CharSequenceUtils.length(" ")); | ||
Assert.assertEquals(8, CharSequenceUtils.length("ABCDEFGH")); | ||
} | ||
|
||
public void testLength_StringBuffer() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(new StringBuffer(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuffer("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuffer(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(new StringBuffer("ABCDEFGH"))); | ||
} | ||
|
||
public void testLength_StringBuilder() { | ||
Assert.assertEquals(0, CharSequenceUtils.length(new StringBuilder(""))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuilder("A"))); | ||
Assert.assertEquals(1, CharSequenceUtils.length(new StringBuilder(" "))); | ||
Assert.assertEquals(8, CharSequenceUtils.length(new StringBuilder("ABCDEFGH"))); | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
} |