List of utility methods to do Collection Unique
void | addUnique(C collection, T item) add Unique if (!collection.add(item)) { throw new IllegalStateException("Encountered an already added item:" + item + " when adding items uniquely to a collection:" + collection); |
boolean | addUnique(final Collection collection, final Object value) add a unique value to a collection. if (collection.contains(value)) { return false; return collection.add(value); |
String | generateUniqueName(String aName, Collection Generates a unique name for the child. return generateUniqueName(aName, aStringCollection, false);
|
T | getUnique(Collection get Unique if (c.isEmpty()) { return null; } else { return c.iterator().next(); |
T | getUnique(Collection Returns the only element in the given collection. checkNull("Collection", collection); if (collection.size() != 1) { throw new IllegalArgumentException( "Collection " + collection + " is expected to contain only one element"); return collection.iterator().next(); |
String | getUniqueName(String name, Collection collection) this gets a new, unique name given a string and an existing collection The name will be incremented by adding "x", where x is an integer, until a unique name is found if (collection == null) { collection = Collections.EMPTY_SET; String result = name; int incr = 1; boolean nameIsInCollection = false; do { nameIsInCollection = false; ... |
String | getUniqueNameWithNumbers(Collection get Unique Name With Numbers if (names == null) { return baseName; String name = baseName; int i = 1; while (names.contains(name)) { name = baseName + i; i++; ... |
String | getUniqueValue(Collection values, String initValue) Makes the value passed in initValue unique among the String values contained in values by suffixing it with a decimal digit suffix.
if (!values.contains(initValue)) { return initValue; } else { StringBuffer unqVal = new StringBuffer(initValue); int beg = unqVal.length(), cur, end; while (Character.isDigit(unqVal.charAt(beg - 1))) { beg--; if (beg == unqVal.length()) { unqVal.append('1'); cur = end = unqVal.length() - 1; while (values.contains(unqVal.toString())) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); } else { while (cur-- > beg) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); break; if (cur < beg) { unqVal.insert(++cur, '1'); end++; while (cur < end) { unqVal.setCharAt(++cur, '0'); return unqVal.toString(); |
boolean | hasUniqueObject(Collection collection) Determine whether the given Collection only contains a single unique object. if (isEmpty(collection)) { return false; boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; ... |
boolean | hasUniqueObject(Collection collection) has Unique Object if (isEmpty(collection)) { return false; boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; ... |