com.ajjpj.afoundation.collection.immutable.MapAsSetWrapper Maven / Gradle / Ivy
The newest version!
package com.ajjpj.afoundation.collection.immutable;
import com.ajjpj.afoundation.collection.ACollectionHelper;
import com.ajjpj.afoundation.collection.AEquality;
import com.ajjpj.afoundation.function.*;
import java.io.Serializable;
import java.util.*;
/**
* @author arno
*/
abstract class MapAsSetWrapper> implements ASet, Serializable {
private final AMap inner;
@SuppressWarnings ("unchecked")
protected MapAsSetWrapper (AMap inner) {
this.inner = (AMap) inner;
}
@Override public C clear () {
return wrapAsSet (inner.clear ());
}
@Override public AEquality equalityForEquals () {
return inner.keyEquality();
}
@Override public int size () {
return inner.size ();
}
@Override public boolean isEmpty () {
return inner.isEmpty ();
}
@Override public boolean nonEmpty () {
return inner.nonEmpty ();
}
@SuppressWarnings ("unchecked")
@Override public C added (K el) {
return wrapAsSet (inner.updated (el, Boolean.TRUE));
}
@SuppressWarnings ("unchecked")
@Override public C removed (K el) {
return wrapAsSet (inner.removed (el));
}
@Override public boolean contains (K el) {
return inner.containsKey (el);
}
@Override public Iterator iterator () {
return new Iterator () {
final Iterator> iter = inner.iterator ();
@Override public boolean hasNext () {
return iter.hasNext ();
}
@Override public K next () {
return iter.next ().getKey ();
}
@Override public void remove () {
throw new UnsupportedOperationException ();
}
};
}
@Override public String mkString () {
return ACollectionHelper.mkString (this);
}
@Override public String mkString (String separator) {
return ACollectionHelper.mkString (this, separator);
}
@Override public String mkString (String prefix, String separator, String suffix) {
return ACollectionHelper.mkString (this, prefix, separator, suffix);
}
@Override public AList toList () {
return AList.create (this);
}
@Override public ASet toSet () {
return this;
}
@SuppressWarnings ("unchecked")
@Override public ASet toSet (AEquality equality) {
if (equality.equals (equalityForEquals ())) {
return this;
}
return AHashSet.create (equality, this);
}
@Override public Collection asJavaUtilCollection () {
return asJavaUtilSet ();
}
@Override public Set asJavaUtilSet () {
return inner.asJavaUtilMap ().keySet ();
}
@Override public String toString () {
return mkString ("[", ", ", "]");
}
@Override public int hashCode() {
return inner.hashCode();
}
@SuppressWarnings("SimplifiableIfStatement")
@Override
public boolean equals(Object o) {
if(o == this) {
return true;
}
if(! (o instanceof MapAsSetWrapper)) {
return false;
}
return inner.equals(((MapAsSetWrapper) o).inner);
}
//-------------------------------------- collection transformations
@SuppressWarnings ("unchecked")
protected ASet createInternal (Iterable elements) {
AMap result = inner.clear ();
for (X el: elements) {
result = result.updated (el, Boolean.TRUE);
}
return (ASet) wrapAsSet (result);
}
protected abstract C wrapAsSet (AMap inner);
@Override public ASet filter (APredicate super K, E> pred) throws E {
return createInternal (ACollectionHelper.filter (this, pred));
}
@SuppressWarnings ("unchecked")
@Override public ASet flatten () {
return (ASet) createInternal (ACollectionHelper.flatten ((Iterable extends Iterable