import java.util.List;
/**
- * Utility methods for lazily instantiated collections. These are useful for
- * situations when we start off with an empty collection (where Collections.empty()
- * can be reused), but need to add more things.
+ * Utility methods for lazily instantiated collections. These are useful for situations when we start off with an empty
+ * collection (where Collections.empty() * can be reused), but need to add more things.
*/
public final class LazyCollections {
+ private LazyCollections() {
+ // Hidden on purpose
+ }
/**
* Add an element to a list, potentially transforming the list.
final List<T> ret;
switch (list.size()) {
- case 0:
- return Collections.singletonList(obj);
- case 1:
- ret = new ArrayList<>();
- ret.addAll(list);
- break;
- default:
- ret = list;
+ case 0:
+ return Collections.singletonList(obj);
+ case 1:
+ ret = new ArrayList<>(2);
+ ret.addAll(list);
+ break;
+ default:
+ ret = list;
}
ret.add(obj);
return ret;
}
-
}