/** * Returns the specified set, or a new one if {@code c} is null. * This is a convenience method for implementation of {@code getFoo()} methods. * * @param <E> the type represented by the {@code Class} argument. * @param c the existing set, or {@code null} if the set has not yet been created. * @param elementType the element type (used only if {@code c} is null). * @return {@code c}, or a new set if {@code c} is null. */ protected final <E> Set<E> nonNullSet(final Set<E> c, final Class<E> elementType) { if (c != null) { return c.isEmpty() && emptyCollectionAsNull() ? null : c; } if (emptyCollectionAsNull()) { return null; } if (isModifiable()) { return createSet(elementType, INITIAL_CAPACITY); } return Collections.emptySet(); }
/** * Returns the specified list, or a new one if {@code c} is null. * This is a convenience method for implementation of {@code getFoo()} methods. * * @param <E> the type represented by the {@code Class} argument. * @param c the existing list, or {@code null} if the list has not yet been created. * @param elementType the element type (used only if {@code c} is null). * @return {@code c}, or a new list if {@code c} is null. */ protected final <E> List<E> nonNullList(final List<E> c, final Class<E> elementType) { if (c != null) { return c.isEmpty() && emptyCollectionAsNull() ? null : c; } if (emptyCollectionAsNull()) { return null; } if (isModifiable()) { /* * Do not specify an initial capacity, because the list will stay empty in a majority of cases * (i.e. the users will want to iterate over the list elements more often than they will want * to add elements). JDK implementation of ArrayList has a lazy instantiation mechanism for * initially empty lists, but as of JDK8 this lazy instantiation works only for list having * the default capacity. */ return new CheckedArrayList<>(elementType); } return Collections.emptyList(); }
if (!isModifiable()) { unmodifiable = this; assert !unmodifiable.isModifiable(); return unmodifiable;
return null; final boolean isModifiable = isModifiable(); if (useSet(elementType)) { if (isModifiable) {
if (isModifiable()) { ModifiableMetadata success = null;
assert !unmodifiable.isModifiable(); return unmodifiable;