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

com.github.andrewoma.dexx.collection.Sets Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/*
 * Copyright (c) 2014 Andrew O'Malley
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.github.andrewoma.dexx.collection;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

/**
 * {@code Sets} is the preferred method of constructing instances of {@code Set}.
 *
 * 

{@link com.github.andrewoma.dexx.collection.HashSet} is currently constructed in * all cases, however this may change in the future. * *

{@code Sets} is preferred for construction as: *

    *
  • It works better in languages that support type inference *
  • It allows future optimisations (e.g. small sets be dedicated classes which are then upgraded to {@code HashSets}) *
*/ public class Sets { private Sets() { } @NotNull @SuppressWarnings("unchecked") public static Set of() { return construct(); } @NotNull @SuppressWarnings("unchecked") public static Set of(E t) { return construct(t); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2) { return construct(e1, e2); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3) { return construct(e1, e2, e3); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4) { return construct(e1, e2, e3, e4); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5) { return construct(e1, e2, e3, e4, e5); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5, E e6) { return construct(e1, e2, e3, e4, e5, e6); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) { return construct(e1, e2, e3, e4, e5, e6, e7); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) { return construct(e1, e2, e3, e4, e5, e6, e7, e8); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) { return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9); } @NotNull @SuppressWarnings("unchecked") 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 construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); } @NotNull @SuppressWarnings("unchecked") public static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E... others) { Set set = construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); for (E e : others) { set = set.add(e); } return set; } private static Set construct(E... es) { Set set = HashSet.empty(); for (E e : es) { set = set.add(e); } return set; } @NotNull public static Set copyOf(java.lang.Iterable iterable) { HashSet result = HashSet.empty(); for (E e : iterable) { result = result.add(e); } return result; } @NotNull public static Set copyOf(Iterator iterator) { HashSet result = HashSet.empty(); while (iterator.hasNext()) { result = result.add(iterator.next()); } return result; } @NotNull public static Set copyOf(E[] es) { HashSet result = HashSet.empty(); for (E e : es) { result = result.add(e); } return result; } @NotNull public static Set copyOfTraversable(Traversable traversable) { @SuppressWarnings("unchecked") final HashSet[] result = new HashSet[1]; result[0] = HashSet.empty(); traversable.forEach(new Function() { @Override public Object invoke(E e) { result[0] = result[0].add(e); return null; } }); return result[0]; } @NotNull @SuppressWarnings("unchecked") public static BuilderFactory> factory() { return (BuilderFactory) HashSet.factory(); } @NotNull public static Builder> builder() { return Sets.factory().newBuilder(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy