Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StringBuffer with StringBuilder where possible #30964

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/** Additional methods added to classes. These must be static methods with receiver as first argument */
public class Augmentation {

// static methods only!
private Augmentation() {}

Expand All @@ -53,10 +53,10 @@ public static <T> int getLength(List<T> receiver) {
public static String namedGroup(Matcher receiver, String name) {
return receiver.group(name);
}

// some groovy methods on iterable
// see http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html

/** Iterates over the contents of an iterable, and checks whether a predicate is valid for at least one element. */
public static <T> boolean any(Iterable<T> receiver, Predicate<T> predicate) {
for (T t : receiver) {
Expand All @@ -66,7 +66,7 @@ public static <T> boolean any(Iterable<T> receiver, Predicate<T> predicate) {
}
return false;
}

/** Converts this Iterable to a Collection. Returns the original Iterable if it is already a Collection. */
public static <T> Collection<T> asCollection(Iterable<T> receiver) {
if (receiver instanceof Collection) {
Expand All @@ -78,7 +78,7 @@ public static <T> Collection<T> asCollection(Iterable<T> receiver) {
}
return list;
}

/** Converts this Iterable to a List. Returns the original Iterable if it is already a List. */
public static <T> List<T> asList(Iterable<T> receiver) {
if (receiver instanceof List) {
Expand All @@ -90,8 +90,8 @@ public static <T> List<T> asList(Iterable<T> receiver) {
}
return list;
}
/** Counts the number of occurrences which satisfy the given predicate from inside this Iterable. */

/** Counts the number of occurrences which satisfy the given predicate from inside this Iterable. */
public static <T> int count(Iterable<T> receiver, Predicate<T> predicate) {
int count = 0;
for (T t : receiver) {
Expand All @@ -101,7 +101,7 @@ public static <T> int count(Iterable<T> receiver, Predicate<T> predicate) {
}
return count;
}

// instead of covariant overrides for every possibility, we just return receiver as 'def' for now
// that way if someone chains the calls, everything works.

Expand All @@ -110,9 +110,9 @@ public static <T> Object each(Iterable<T> receiver, Consumer<T> consumer) {
receiver.forEach(consumer);
return receiver;
}
/**
* Iterates through an iterable type, passing each item and the item's index

/**
* Iterates through an iterable type, passing each item and the item's index
* (a counter starting at zero) to the given consumer.
*/
public static <T> Object eachWithIndex(Iterable<T> receiver, ObjIntConsumer<T> consumer) {
Expand All @@ -122,7 +122,7 @@ public static <T> Object eachWithIndex(Iterable<T> receiver, ObjIntConsumer<T> c
}
return receiver;
}

/**
* Used to determine if the given predicate is valid (i.e. returns true for all items in this iterable).
*/
Expand All @@ -134,10 +134,10 @@ public static <T> boolean every(Iterable<T> receiver, Predicate<T> predicate) {
}
return true;
}

/**
* Iterates through the Iterable transforming items using the supplied function and
* collecting any non-null results.
* Iterates through the Iterable transforming items using the supplied function and
* collecting any non-null results.
*/
public static <T,U> List<U> findResults(Iterable<T> receiver, Function<T,U> filter) {
List<U> list = new ArrayList<>();
Expand All @@ -149,9 +149,9 @@ public static <T,U> List<U> findResults(Iterable<T> receiver, Function<T,U> filt
}
return list;
}

/**
* Sorts all Iterable members into groups determined by the supplied mapping function.
* Sorts all Iterable members into groups determined by the supplied mapping function.
*/
public static <T,U> Map<U,List<T>> groupBy(Iterable<T> receiver, Function<T,U> mapper) {
Map<U,List<T>> map = new LinkedHashMap<>();
Expand All @@ -166,10 +166,10 @@ public static <T,U> Map<U,List<T>> groupBy(Iterable<T> receiver, Function<T,U> m
}
return map;
}

/**
* Concatenates the toString() representation of each item in this Iterable,
* with the given String as a separator between each item.
* Concatenates the toString() representation of each item in this Iterable,
* with the given String as a separator between each item.
*/
public static <T> String join(Iterable<T> receiver, String separator) {
StringBuilder sb = new StringBuilder();
Expand All @@ -181,7 +181,7 @@ public static <T> String join(Iterable<T> receiver, String separator) {
}
return sb.toString();
}

/**
* Sums the result of an Iterable
*/
Expand All @@ -192,9 +192,9 @@ public static <T extends Number> double sum(Iterable<T> receiver) {
}
return sum;
}

/**
* Sums the result of applying a function to each item of an Iterable.
* Sums the result of applying a function to each item of an Iterable.
*/
public static <T> double sum(Iterable<T> receiver, ToDoubleFunction<T> function) {
double sum = 0;
Expand All @@ -203,13 +203,13 @@ public static <T> double sum(Iterable<T> receiver, ToDoubleFunction<T> function)
}
return sum;
}

// some groovy methods on collection
// see http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Collection.html

/**
* Iterates through this collection transforming each entry into a new value using
* the function, returning a list of transformed values.
* Iterates through this collection transforming each entry into a new value using
* the function, returning a list of transformed values.
*/
public static <T,U> List<U> collect(Collection<T> receiver, Function<T,U> function) {
List<U> list = new ArrayList<>();
Expand All @@ -218,9 +218,9 @@ public static <T,U> List<U> collect(Collection<T> receiver, Function<T,U> functi
}
return list;
}

/**
* Iterates through this collection transforming each entry into a new value using
* Iterates through this collection transforming each entry into a new value using
* the function, adding the values to the specified collection.
*/
public static <T,U> Object collect(Collection<T> receiver, Collection<U> collection, Function<T,U> function) {
Expand All @@ -229,7 +229,7 @@ public static <T,U> Object collect(Collection<T> receiver, Collection<U> collect
}
return collection;
}

/**
* Finds the first value matching the predicate, or returns null.
*/
Expand All @@ -241,7 +241,7 @@ public static <T> T find(Collection<T> receiver, Predicate<T> predicate) {
}
return null;
}

/**
* Finds all values matching the predicate, returns as a list
*/
Expand All @@ -254,19 +254,19 @@ public static <T> List<T> findAll(Collection<T> receiver, Predicate<T> predicate
}
return list;
}

/**
* Iterates through the collection calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, null is returned.
* Iterates through the collection calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, null is returned.
*/
public static <T,U> Object findResult(Collection<T> receiver, Function<T,U> function) {
return findResult(receiver, null, function);
}

/**
* Iterates through the collection calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* Iterates through the collection calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, defaultResult is returned.
*/
public static <T,U> Object findResult(Collection<T> receiver, Object defaultResult, Function<T,U> function) {
Expand All @@ -278,10 +278,10 @@ public static <T,U> Object findResult(Collection<T> receiver, Object defaultResu
}
return defaultResult;
}

/**
* Splits all items into two collections based on the predicate.
* The first list contains all items which match the closure expression. The second list all those that don't.
* Splits all items into two collections based on the predicate.
* The first list contains all items which match the closure expression. The second list all those that don't.
*/
public static <T> List<List<T>> split(Collection<T> receiver, Predicate<T> predicate) {
List<T> matched = new ArrayList<>();
Expand All @@ -298,13 +298,13 @@ public static <T> List<List<T>> split(Collection<T> receiver, Predicate<T> predi
}
return result;
}

// some groovy methods on map
// see http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html

/**
* Iterates through this map transforming each entry into a new value using
* the function, returning a list of transformed values.
* Iterates through this map transforming each entry into a new value using
* the function, returning a list of transformed values.
*/
public static <K,V,T> List<T> collect(Map<K,V> receiver, BiFunction<K,V,T> function) {
List<T> list = new ArrayList<>();
Expand All @@ -313,9 +313,9 @@ public static <K,V,T> List<T> collect(Map<K,V> receiver, BiFunction<K,V,T> funct
}
return list;
}

/**
* Iterates through this map transforming each entry into a new value using
* Iterates through this map transforming each entry into a new value using
* the function, adding the values to the specified collection.
*/
public static <K,V,T> Object collect(Map<K,V> receiver, Collection<T> collection, BiFunction<K,V,T> function) {
Expand All @@ -324,8 +324,8 @@ public static <K,V,T> Object collect(Map<K,V> receiver, Collection<T> collection
}
return collection;
}
/** Counts the number of occurrences which satisfy the given predicate from inside this Map */

/** Counts the number of occurrences which satisfy the given predicate from inside this Map */
public static <K,V> int count(Map<K,V> receiver, BiPredicate<K,V> predicate) {
int count = 0;
for (Map.Entry<K,V> kvPair : receiver.entrySet()) {
Expand All @@ -335,13 +335,13 @@ public static <K,V> int count(Map<K,V> receiver, BiPredicate<K,V> predicate) {
}
return count;
}

/** Iterates through a Map, passing each item to the given consumer. */
public static <K,V> Object each(Map<K,V> receiver, BiConsumer<K,V> consumer) {
receiver.forEach(consumer);
return receiver;
}

/**
* Used to determine if the given predicate is valid (i.e. returns true for all items in this map).
*/
Expand All @@ -353,7 +353,7 @@ public static <K,V> boolean every(Map<K,V> receiver, BiPredicate<K,V> predicate)
}
return true;
}

/**
* Finds the first entry matching the predicate, or returns null.
*/
Expand All @@ -365,7 +365,7 @@ public static <K,V> Map.Entry<K,V> find(Map<K,V> receiver, BiPredicate<K,V> pred
}
return null;
}

/**
* Finds all values matching the predicate, returns as a map.
*/
Expand All @@ -384,19 +384,19 @@ public static <K,V> Map<K,V> findAll(Map<K,V> receiver, BiPredicate<K,V> predica
}
return map;
}

/**
* Iterates through the map calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, null is returned.
* Iterates through the map calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, null is returned.
*/
public static <K,V,T> Object findResult(Map<K,V> receiver, BiFunction<K,V,T> function) {
return findResult(receiver, null, function);
}

/**
* Iterates through the map calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* Iterates through the map calling the given function for each item
* but stopping once the first non-null result is found and returning that result.
* If all results are null, defaultResult is returned.
*/
public static <K,V,T> Object findResult(Map<K,V> receiver, Object defaultResult, BiFunction<K,V,T> function) {
Expand All @@ -408,10 +408,10 @@ public static <K,V,T> Object findResult(Map<K,V> receiver, Object defaultResult,
}
return defaultResult;
}

/**
* Iterates through the map transforming items using the supplied function and
* collecting any non-null results.
* Iterates through the map transforming items using the supplied function and
* collecting any non-null results.
*/
public static <K,V,T> List<T> findResults(Map<K,V> receiver, BiFunction<K,V,T> filter) {
List<T> list = new ArrayList<>();
Expand All @@ -423,9 +423,9 @@ public static <K,V,T> List<T> findResults(Map<K,V> receiver, BiFunction<K,V,T> f
}
return list;
}

/**
* Sorts all Map members into groups determined by the supplied mapping function.
* Sorts all Map members into groups determined by the supplied mapping function.
*/
public static <K,V,T> Map<T,Map<K,V>> groupBy(Map<K,V> receiver, BiFunction<K,V,T> mapper) {
Map<T,Map<K,V>> map = new LinkedHashMap<>();
Expand Down Expand Up @@ -456,7 +456,7 @@ public static String replaceAll(CharSequence receiver, Pattern pattern, Function
// CharSequqence's toString is *supposed* to always return the characters in the sequence as a String
return receiver.toString();
}
StringBuffer result = new StringBuffer(initialBufferForReplaceWith(receiver));
StringBuilder result = new StringBuilder(initialBufferForReplaceWith(receiver));
do {
m.appendReplacement(result, Matcher.quoteReplacement(replacementBuilder.apply(m)));
} while (m.find());
Expand All @@ -474,7 +474,7 @@ public static String replaceFirst(CharSequence receiver, Pattern pattern, Functi
// CharSequqence's toString is *supposed* to always return the characters in the sequence as a String
return receiver.toString();
}
StringBuffer result = new StringBuffer(initialBufferForReplaceWith(receiver));
StringBuilder result = new StringBuilder(initialBufferForReplaceWith(receiver));
m.appendReplacement(result, Matcher.quoteReplacement(replacementBuilder.apply(m)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was added in Java 9 so I don't think we can use it here until Java 8 isn't a thing for master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it is worth when I wrote this the first time around I didn't see any real performance penalty for using StringBuffer. I believe the JVM can remove the uncontended synchronization fairly effectively in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, didn't know about the method. And yes, I don't see any real issue here as well, might as well keep it.

m.appendTail(result);
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public abstract class AnalysisFactoryTestCase extends ESTestCase {

private static String toCamelCase(String s) {
Matcher m = UNDERSCORE_THEN_ANYTHING.matcher(s);
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has the same problem with java 8.

}
Expand Down
Loading