Commit 8a4a02eb authored by Tuukka Lehtonen's avatar Tuukka Lehtonen
Browse files

Document and fix reservedNames handling in NameUtils.findFreshName*

The functions were not documenting/enforcing the use of
`TreeSet(STRING_CHARBUFFER_COMPARATOR)` with `reservedNames`. This is
definitely somewhat of an internal optimization but necessary as it
works around the need for completely unnecessary String allocation
overhead.

gitlab #712
parent fabed70f
......@@ -203,12 +203,32 @@ public final class NameUtils {
return findFreshNameNumbered(proposition, reservedNames, numberSeparator);
}
private static Set<String> ensureTreeSetWithComparator(Set<String> s) {
if (!(s instanceof TreeSet) || ((TreeSet<?>) s).comparator() != STRING_CHARBUFFER_COMPARATOR) {
TreeSet<String> ts = new TreeSet<>(STRING_CHARBUFFER_COMPARATOR);
ts.addAll(s);
return ts;
}
return s;
}
/**
* @param proposition initial name proposition
* @param reservedNames reserved names as a {@link TreeSet} that uses
* {@link #STRING_CHARBUFFER_COMPARATOR} as comparator
* @param numberSeparator the separator string between the name and the running
* number suffix
* @return previously unreserved name
*/
@SuppressWarnings("unlikely-arg-type")
public static String findFreshNameNumbered(String proposition, Set<String> reservedNames, String numberSeparator) {
// Trying to optimize away unnecessary allocation of new String instances
// when looking for fresh names for objects.
if (!reservedNames.contains(proposition))
return proposition;
reservedNames = ensureTreeSetWithComparator(reservedNames);
CharBuffer cb = CharBuffer.allocate(proposition.length() + 10);
cb.append(proposition);
cb.append(numberSeparator);
......@@ -227,10 +247,21 @@ public final class NameUtils {
}
}
/**
* @param proposition initial name proposition
* @param reservedNames reserved names as a {@link TreeSet} that uses
* {@link #STRING_CHARBUFFER_COMPARATOR} as comparator
* @param nameFormat the format string to use. The first argument will be the
* name and the second the running integer index
* @return previously unreserved name
*/
@SuppressWarnings("unlikely-arg-type")
public static String findFreshNameFormatted(String proposition, Set<String> reservedNames, String nameFormat) {
if (!reservedNames.contains(proposition))
return proposition;
reservedNames = ensureTreeSetWithComparator(reservedNames);
// Trying to optimize away unnecessary allocation of new String instances
// when looking for fresh names for objects.
CharBuffer cb = CharBuffer.allocate(proposition.length() + 10);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment