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

com.artclod.common.collect.GuavaImFSet Maven / Gradle / Ivy

package com.artclod.common.collect;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.function.Function;

import com.artclod.common.collect.base.BaseFSet;
import com.artclod.common.collect.base.ImFCollectionMixIn;
import com.artclod.common.collect.builder.CollectionBuilder;
import com.artclod.common.collect.builder.GuavaImFSetBuilder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Sets;

public class GuavaImFSet extends BaseFSet> implements ImFCollectionMixIn, ImFSet, Serializable {
	private static final long serialVersionUID = 1L;

	public static  GuavaImFSet wrap(ImmutableSet inner) {
		return new GuavaImFSet(inner);
	}

	public static  GuavaImFSet create() {
		return new GuavaImFSet(ImmutableSet.of());
	}

	public static  GuavaImFSet create(@SuppressWarnings("unchecked") E... elements) {
		return new GuavaImFSet(ImmutableSet.copyOf(elements));
	}

	public static  GuavaImFSet create(Collection c) {
		return new GuavaImFSet(ImmutableSet.copyOf(c));
	}

	public static  GuavaImFSet create(Iterable elements) {
		return new GuavaImFSet(ImmutableSet.copyOf(elements));
	}

	public static  GuavaImFSet create(Iterator elements) {
		return new GuavaImFSet(ImmutableSet.copyOf(elements));
	}

	public GuavaImFSet(ImmutableSet inner) {
		super(inner);
	}

	@Override
	protected CollectionBuilder> builder() {
		return new GuavaImFSetBuilder(ImmutableSet. builder());
	}

	@Override
	protected Iterator reverseIterator() {
		return iterator(); // No sensible reverse
	}

	@Override
	public GuavaImFSet addCp(E e) {
		LinkedHashSet set = Sets.newLinkedHashSet(this);
		set.add(e);
		return new GuavaImFSet(ImmutableSet.copyOf(set));
	}

	@Override
	public GuavaImFSet addAllCp(Collection c) {
		LinkedHashSet set = Sets.newLinkedHashSet(this);
		set.addAll(c);
		return new GuavaImFSet(ImmutableSet.copyOf(set));
	}

	@Override
	public GuavaImFSet removeCp(Object o) {
		LinkedHashSet set = Sets.newLinkedHashSet(this);
		set.remove(o);
		return new GuavaImFSet(ImmutableSet.copyOf(set));
	}

	@Override
	public GuavaImFSet removeAllCp(Collection c) {
		LinkedHashSet set = Sets.newLinkedHashSet(this);
		set.removeAll(c);
		return new GuavaImFSet(ImmutableSet.copyOf(set));
	}

	@Override
	public GuavaImFSet retainAllCp(Collection c) {
		LinkedHashSet set = Sets.newLinkedHashSet(this);
		set.retainAll(c);
		return new GuavaImFSet(ImmutableSet.copyOf(set));
	}

	@Override
	public  GuavaImFSet map(Function f) {
		Builder builder = ImmutableSet. builder();
		for (E e : this) {
			builder.add(f.apply(e));
		}
		return new GuavaImFSet(builder.build());
	}

	@Override
	public  GuavaImFSet flatMap(Function> mapper) {
		Builder builder = ImmutableSet. builder();
		for (E e : this) {
			builder.addAll(mapper.apply(e));
		}
		return new GuavaImFSet(builder.build());
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy