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

com.ajjpj.afoundation.collection.immutable.AHashSet Maven / Gradle / Ivy

The newest version!
package com.ajjpj.afoundation.collection.immutable;

import com.ajjpj.afoundation.collection.AEquality;

import java.util.Arrays;


/**
 * @author arno
 */
public class AHashSet extends MapAsSetWrapper> {
    @SuppressWarnings ("unchecked")
    private static final AHashSet EMPTY_IDENTITY = new AHashSet (AHashMap.empty (AEquality.IDENTITY));
    @SuppressWarnings ("unchecked")
    private static final AHashSet EMPTY_EQUALS   = new AHashSet (AHashMap.empty (AEquality.EQUALS));

    public static  AHashSet empty () {
        return empty (AEquality.EQUALS);
    }

    @SuppressWarnings ("unchecked")
    public static  AHashSet empty (AEquality equality) {
        if (equality == AEquality.EQUALS) {
            return EMPTY_EQUALS;
        }
        if (equality == AEquality.IDENTITY) {
            return EMPTY_IDENTITY;
        }

        return new AHashSet<> (AHashMap. empty (equality));
    }

    @SuppressWarnings ("unchecked")
    public static  AHashSet create (T... elements) {
        return create (AEquality.EQUALS, elements);
    }

    public static  AHashSet create (Iterable elements) {
        return create (AEquality.EQUALS, elements);
    }

    @SuppressWarnings ("unchecked")
    public static  AHashSet create (AEquality equality, T... elements) {
        return create (equality, Arrays.asList (elements));
    }

    public static  AHashSet create (AEquality equality, Iterable elements) {
        AHashSet result = empty (equality);

        for (T el: elements) {
            result = result.added (el);
        }
        return result;
    }

    public static  AHashSet fromMap (AHashMap inner) {
        return new AHashSet<> (inner);
    }

    private Object readResolve() {
        if (isEmpty ()) {
            if (equalityForEquals () == AEquality.EQUALS) return EMPTY_EQUALS;
            if (equalityForEquals () == AEquality.IDENTITY) return EMPTY_IDENTITY;
        }
        return this;
    }

    private AHashSet (AMap inner) {
        super (inner);
    }

    @Override protected AHashSet wrapAsSet (AMap inner) {
        return new AHashSet<> (inner);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy