Update for LANG-1069: CharSet.getInstance documentation does not clearly explain how to include negation character in set. Javadoc expanded and unit tests added to match examples. Based on patch by Arno Noordover.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1672833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Duncan Jones 2015-04-11 06:02:34 +00:00
parent 9bebec9cf9
commit 71f6746f81
3 changed files with 30 additions and 4 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1069" type="update" dev="djones" due-to="Arno Noordover">CharSet.getInstance documentation does not clearly explain how to include negation character in set</action>
<action issue="LANG-1050" type="add" dev="djones" due-to="James Sawle">Change nullToEmpty methods to generics</action>
<action issue="LANG-1111" type="fix" dev="chas">Fix FindBugs warnings in DurationFormatUtils</action>
<action issue="LANG-1074" type="add" dev="djones" due-to="Haiyang Li">Add a method to ArrayUtils for removing all occurrences of a given element</action>

View File

@ -115,6 +115,7 @@ public class CharSet implements Serializable {
* <li>Negated single character, such as "^a"
* <li>Ordinary single character, such as "a"
* </ol>
*
* <p>Matching works left to right. Once a match is found the
* search starts again from the next character.</p>
*
@ -128,7 +129,24 @@ public class CharSet implements Serializable {
* as the "a-e" and "e-a" are the same.</p>
*
* <p>The set of characters represented is the union of the specified ranges.</p>
*
* <p>There are two ways to add a literal negation character ({@code ^}):</p>
* <ul>
* <li>As the last character in a string, e.g. {@code CharSet.getInstance("a-z^")}</li>
* <li>As a separate element, e.g. {@code CharSet.getInstance("^","a-z")}</li>
* </ul>
*
* <p>Examples using the negation character:</p>
* <pre>
* CharSet.getInstance("^a-c").contains('a') = false
* CharSet.getInstance("^a-c").contains('d') = true
* CharSet.getInstance("^^a-c").contains('a') = true // (only '^' is negated)
* CharSet.getInstance("^^a-c").contains('^') = false
* CharSet.getInstance("^a-cd-f").contains('d') = true
* CharSet.getInstance("a-c^").contains('^') = true
* CharSet.getInstance("^", "a-c").contains('^') = true
* </pre>
*
* <p>All CharSet objects returned by this method will be immutable.</p>
*
* @param setStrs Strings to merge into the set, may be null

View File

@ -18,10 +18,7 @@
*/
package org.apache.commons.lang3;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.lang.reflect.Modifier;
@ -466,4 +463,14 @@ public class CharSetTest {
assertTrue(ArrayUtils.contains(array, CharRange.isIn('0', '9')));
}
@Test
public void testJavadocExamples() throws Exception {
assertFalse(CharSet.getInstance("^a-c").contains('a'));
assertTrue(CharSet.getInstance("^a-c").contains('d'));
assertTrue(CharSet.getInstance("^^a-c").contains('a'));
assertFalse(CharSet.getInstance("^^a-c").contains('^'));
assertTrue(CharSet.getInstance("^a-cd-f").contains('d'));
assertTrue(CharSet.getInstance("a-c^").contains('^'));
assertTrue(CharSet.getInstance("^", "a-c").contains('^'));
}
}