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

aQute.bnd.unmodifiable.Sets Maven / Gradle / Ivy

The newest version!
package aQute.bnd.unmodifiable;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collector;

@SuppressWarnings("unchecked")
public class Sets {

	private Sets() {}

	public static  Set of() {
		return (Set) ImmutableSet.EMPTY;
	}

	public static  Set of(E e1) {
		return new ImmutableSet<>(e1);
	}

	public static  Set of(E e1, E e2) {
		return new ImmutableSet<>(e1, e2);
	}

	public static  Set of(E e1, E e2, E e3) {
		return new ImmutableSet<>(e1, e2, e3);
	}

	public static  Set of(E e1, E e2, E e3, E e4) {
		return new ImmutableSet<>(e1, e2, e3, e4);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5, E e6) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5, e6);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5, e6, e7);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5, e6, e7, e8);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5, e6, e7, e8, e9);
	}

	public static  Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
		return new ImmutableSet<>(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
	}

	@SafeVarargs
	public static  Set of(E... elements) {
		int length = elements.length;
		if (length == 0) {
			return of();
		}
		return new ImmutableSet(Arrays.copyOf(elements, length, Object[].class));
	}

	public static  Set copyOf(Collection collection) {
		if (collection instanceof ImmutableSet) {
			return (Set) collection;
		}
		if (collection.isEmpty()) {
			return of();
		}
		if (collection instanceof Set) {
			return new ImmutableSet(collection.toArray());
		}
		return new ImmutableSet(collection.stream()
			.distinct()
			.toArray());
	}

	public static  Collector> toSet() {
		return Collector.of((Supplier>) LinkedHashSet::new, Set::add, (l, r) -> {
			l.addAll(r);
			return l;
		}, Sets::copyOf);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy