All Downloads are FREE. Search and download functionalities are using the official Maven repository.

objectos.way.UtilUnmodifiableSet Maven / Gradle / Ivy

Go to download

Objectos Way allows you to build full-stack web applications using only Java.

The newest version!
/*
 * Copyright (C) 2022-2023 Objectos Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package objectos.way;

import java.util.Iterator;
import java.util.Set;

/**
 * An unmodifiable implementation of the {@link Set} interface.
 *
 * 

* After creation, instances of this class do not permit adding nor removing of * elements. Any mutator method will throw an * {@link UnsupportedOperationException} when called. This is true for mutator * methods present in this class and for mutator methods present in the returned * iterators. * * @param type of the elements in this set */ abstract sealed class UtilUnmodifiableSet extends UtilUnmodifiableCollection implements Set permits UtilUnmodifiableSetN { private static final UtilUnmodifiableSet EMPTY = new UtilUnmodifiableSetN( Util.EMPTY_OBJECT_ARRAY, 0 ); /** * Sole constructor. */ UtilUnmodifiableSet() {} /** * Returns a new {@code UnmodifiableSet} containing all of the distinct * elements * in the specified array. * *

* Two non-null elements {@code e1} and {@code e2} are distinct if the * expression {@code e1.equals(e2)} evaluates to {@code false}. * *

* This method returns a copy in the sense that modifications in the * specified array are not propagated to the returned set. * * @param * type of the elements in this set * * @param array * an array for which an immutable set copy will be created * * @return a new {@code UnmodifiableSet} containing all of the distinct * elements in the specified array * * @throws NullPointerException * if the array is {@code null} or any element in the array is * {@code null} */ public static UtilUnmodifiableSet copyOf(E[] array) { Check.notNull(array, "array == null"); var set = new UtilSet(); E element; for (int i = 0, size = array.length; i < size; i++) { element = array[i]; set.addUnchecked( Check.notNull(element, "array[", i, "] == null") ); } return set.toUnmodifiableSet(); } /** * Returns the specified iterable if it is an {@code UnmodifiableSet}; returns * a new {@code UnmodifiableSet} containing all of the distinct elements * returned * by the specified iterable's iterator. * *

* Two non-null elements {@code e1} and {@code e2} are distinct if the * expression {@code e1.equals(e2)} evaluates to {@code false}. * * @param * type of the elements in this set * @param elements * an {@link Iterable} for which an immutable set copy will be created * * @return the specified iterable if it is an {@code UnmodifiableSet} or a new * {@code UnmodifiableSet} containing all of the elements returned by * the * specified iterable's iterator * * @throws NullPointerException * if the iterable is {@code null} or if any element returned by the * iterable's iterator is {@code null} */ @SuppressWarnings("unchecked") public static UtilUnmodifiableSet copyOf(Iterable elements) { Check.notNull(elements, "elements == null"); if (elements instanceof UtilUnmodifiableSet) { return (UtilUnmodifiableSet) elements; } if (elements instanceof UtilSet set) { return (UtilUnmodifiableSet) set.toUnmodifiableSet(); } var set = new UtilSet(); set.addAll0(elements, "elements["); return set.toUnmodifiableSet(); } /** * Returns a new {@code UnmodifiableSet} instance containing all of the * distinct * elements returned by the specified iterator. * *

* Two non-null elements {@code e1} and {@code e2} are distinct if the * expression {@code e1.equals(e2)} evaluates to {@code false}. * * @param * type of the elements in this set * @param iterator * an {@link Iterator} for which an immutable set copy will be created * * @return a new {@code UnmodifiableSet} instance containing all of the * distinct * elements produced by the specified iterator * * @throws NullPointerException * if the iterator is {@code null} or any element produced by the * iterator is {@code null} */ public static UtilUnmodifiableSet copyOf(Iterator iterator) { Check.notNull(iterator, "iterator == null"); if (!iterator.hasNext()) { return UtilUnmodifiableSet.of(); } var set = new UtilSet(); int i = 0; var element = iterator.next(); if (element == null) { throw new NullPointerException("iterator[" + i + "] == null"); } i++; set.addUnchecked(element); while (iterator.hasNext()) { element = iterator.next(); if (element == null) { throw new NullPointerException("iterator[" + i + "] == null"); } i++; set.addUnchecked(element); } return set.toUnmodifiableSet(); } /** * Returns the empty {@code UnmodifiableSet}. * * @param * type of the elements in this set * * @return the empty {@code UnmodifiableSet} */ @SuppressWarnings("unchecked") public static UtilUnmodifiableSet of() { return (UtilUnmodifiableSet) EMPTY; } /** * Returns a new {@code UnmodifiableSet} containing one element. * * @param * type of the elements in this set * @param element * the single element * * @return a new {@code UnmodifiableSet} containing one element * * @throws NullPointerException * if the specified element is {@code null} */ public static UtilUnmodifiableSet of(E element) { Check.notNull(element, "element == null"); var set = new UtilSet(); set.add(element); return set.toUnmodifiableSet(); } /** * Returns a new {@code UnmodifiableSet} containing all of the distinct * specified elements. * *

* Two non-null elements {@code e1} and {@code e2} are distinct if the * expression {@code e1.equals(e2)} evaluates to {@code false}. * * @param * type of the elements in this set * @param first * the first element * @param more * the additional elements * * @return a new {@code UnmodifiableSet} containing all of the distinct * specified * elements * * @throws NullPointerException * if any of the specified elements is {@code null} */ @SuppressWarnings("unchecked") public static UtilUnmodifiableSet of(E first, E... more) { Check.notNull(first, "first == null"); Check.notNull(more, "more == null"); var set = new UtilSet(); set.add(first); for (int i = 0; i < more.length; i++) { var e = more[i]; set.addWithNullMessage(e, "more[", i, "] == null"); } return set.toUnmodifiableSet(); } /** * Returns {@code true} if this set contains the specified element. More * formally, returns {@code true} if and only if this set contains at least * one element {@code e} such that {@code e.equals(o)}. * * @param o * an element to check for presence in this set * * @return {@code true} if this set contains the specified value */ @Override public abstract boolean contains(Object o); /** *

* Compares the specified object with this set for equality. Returns * {@code true} if and only if * *

    *
  • the specified object is also a {@link Set};
  • *
  • both sets have same size; and
  • *
  • each element in this set is also present in the specified set.
  • *
* * @param obj * the object to be compared for equality with this set * * @return {@code true} if the specified object is equal to this set */ @Override public abstract boolean equals(Object obj); /** * Returns the hash code value of this set. * * @return the hash code value of this set */ @Override public abstract int hashCode(); /** * Returns an iterator over the elements in this set. The elements * are returned in no particular order. * * @return an iterator over the elements in this set */ @Override public abstract Util.UnmodifiableIterator iterator(); /** * Returns the size of this set. The size of a set is equal to the number of * elements it contains. * * @return the size of this set */ @Override public abstract int size(); /** * Returns a new array instance containing all of the elements in this set. * The returned array length is equal to the size of this set. * * @return a new array instance containing all of the elements in this set */ @Override public abstract Object[] toArray(); /** * Returns an array, either the specified array or a new array instance, * containing all of the elements in this set. * *

* The specified array is used as the return value if it is large enough to * hold all of the elements in this set. Additionally, if the specified * array is such that {@code a.length > size()} then the position after the * last element is set to {@code null}. * *

* If the specified array is not large enough, then a new array is created, * with the same runtime type of the specified array, and used as the return * value. * * @param a * the array into which the elements of the set are to be stored, if * it is big enough; otherwise, a new array of the same runtime type is * allocated for this purpose. * * @return an array containing the elements of the set */ @Override public abstract T[] toArray(T[] a); @Override protected final Object toStringTypeName() { return "UnmodifiableSet"; } }