Skip to content

Commit 7489d6e

Browse files
committed
CharSet now maintains iteration order
Javadoc
1 parent b3cd50b commit 7489d6e

File tree

3 files changed

+146
-143
lines changed

3 files changed

+146
-143
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ The <action> type attribute can be add,update,fix,remove.
118118
<action type="fix" dev="ggregory" due-to="ThrawnCA, Gary Gregory">Fix StringUtils.abbreviate(String, String, int) contract violations #1572.</action>
119119
<action type="fix" dev="ggregory" due-to="TK_ENDO, Arnout Engelen, Gary Gregory">Add test coverage for negative-day adjustment in DurationFormatUtils #1596.</action>
120120
<action type="fix" dev="ggregory" due-to="Mohammad Bireybi, Gary Gregory">[Javadoc] Add usage examples to CharSet.contains(char) #1605.</action>
121+
<action type="fix" dev="ggregory" due-to="Gary Gregory">CharSet now maintains iteration order.</action>
121122
<!-- ADD -->
122123
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_27.</action>
123124
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_27.</action>

src/main/java/org/apache/commons/lang3/CharSet.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.io.Serializable;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22-
import java.util.HashSet;
22+
import java.util.LinkedHashSet;
2323
import java.util.Map;
2424
import java.util.Set;
2525
import java.util.stream.Stream;
@@ -79,7 +79,9 @@ public class CharSet implements Serializable {
7979

8080
/**
8181
* A Map of the common cases used in the factory.
82-
* Subclasses can add more common patterns if desired
82+
* <p>
83+
* Subclasses can add more common patterns if desired.
84+
* </p>
8385
*
8486
* @since 2.0
8587
*/
@@ -152,8 +154,8 @@ public class CharSet implements Serializable {
152154
*
153155
* <p>All CharSet objects returned by this method will be immutable.</p>
154156
*
155-
* @param setStrs Strings to merge into the set, may be null
156-
* @return a CharSet instance
157+
* @param setStrs Strings to merge into the set, may be null.
158+
* @return a CharSet instance.
157159
* @since 2.4
158160
*/
159161
public static CharSet getInstance(final String... setStrs) {
@@ -170,14 +172,14 @@ public static CharSet getInstance(final String... setStrs) {
170172
}
171173

172174
/** The set of CharRange objects. */
173-
private final Set<CharRange> set = Collections.synchronizedSet(new HashSet<>());
175+
private final Set<CharRange> set = Collections.synchronizedSet(new LinkedHashSet<>());
174176

175177
/**
176178
* Constructs a new CharSet using the set syntax.
177179
* Each string is merged in with the set.
178180
*
179-
* @param set Strings to merge into the initial set
180-
* @throws NullPointerException if set is {@code null}
181+
* @param set Strings to merge into the initial set.
182+
* @throws NullPointerException if set is {@code null}.
181183
*/
182184
protected CharSet(final String... set) {
183185
Stream.of(set).forEach(this::add);
@@ -192,7 +194,6 @@ protected void add(final String str) {
192194
if (str == null) {
193195
return;
194196
}
195-
196197
final int len = str.length();
197198
int pos = 0;
198199
while (pos < len) {
@@ -218,10 +219,10 @@ protected void add(final String str) {
218219
}
219220

220221
/**
221-
* Does the {@link CharSet} contain the specified
222-
* character {@code ch}.
223-
*
224-
* <p>Examples using the negation character:</p>
222+
* Tests whether this {@link CharSet} contain the specified character {@code ch}.
223+
* <p>
224+
* Examples using the negation character:
225+
* </p>
225226
* <pre>
226227
* CharSet.getInstance("^a-c").contains('a') = false
227228
* CharSet.getInstance("^a-c").contains('d') = true
@@ -232,8 +233,8 @@ protected void add(final String str) {
232233
* CharSet.getInstance("^", "a-c").contains('^') = true
233234
* </pre>
234235
*
235-
* @param ch the character to check for
236-
* @return {@code true} if the set contains the characters
236+
* @param ch the character to check.
237+
* @return {@code true} if the set contains the characters.
237238
*/
238239
public boolean contains(final char ch) {
239240
synchronized (set) {
@@ -248,8 +249,8 @@ public boolean contains(final char ch) {
248249
* <p>The two sets {@code abc} and {@code a-c} are <em>not</em>
249250
* equal according to this method.</p>
250251
*
251-
* @param obj the object to compare to
252-
* @return true if equal
252+
* @param obj the object to compare.
253+
* @return true if equal.
253254
* @since 2.0
254255
*/
255256
@Override
@@ -265,21 +266,21 @@ public boolean equals(final Object obj) {
265266
}
266267

267268
/**
268-
* Gets the internal set as an array of CharRange objects.
269+
* Gets the set of character ranges.
270+
* <p>
271+
* Package private for testing.
272+
* </p>
269273
*
270-
* @return an array of immutable CharRange objects
271-
* @since 2.0
274+
* @return the set of character ranges.
272275
*/
273-
// NOTE: This is no longer public as CharRange is no longer a public class.
274-
// It may be replaced when CharSet moves to Range.
275-
/*public*/ CharRange[] getCharRanges() {
276-
return set.toArray(CharRange.EMPTY_ARRAY);
276+
Set<CharRange> getCharRanges() {
277+
return set;
277278
}
278279

279280
/**
280281
* Gets a hash code compatible with the equals method.
281282
*
282-
* @return a suitable hash code
283+
* @return a suitable hash code.
283284
* @since 2.0
284285
*/
285286
@Override
@@ -290,7 +291,7 @@ public int hashCode() {
290291
/**
291292
* Gets a string representation of the set.
292293
*
293-
* @return string representation of the set
294+
* @return string representation of the set.
294295
*/
295296
@Override
296297
public String toString() {

0 commit comments

Comments
 (0)