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

com.github.andrewoma.dexx.collection.HashSet 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 com.github.andrewoma.dexx.collection.internal.base.AbstractSet;
import com.github.andrewoma.dexx.collection.internal.base.Iterables;
import com.github.andrewoma.dexx.collection.internal.builder.AbstractSelfBuilder;
import com.github.andrewoma.dexx.collection.internal.hashmap.CompactHashMap;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

/**
 * {@code HashSet} is an implementation of {@code Set} backed by a {@code HashMap}.
 */
public class HashSet extends AbstractSet {
    private static final HashSet EMPTY = new HashSet();

    private static final KeyFunction keyFunction = new KeyFunction() {
        @NotNull
        public Object key(@NotNull Object value) {
            return value;
        }
    };

    @NotNull
    public static  BuilderFactory> factory() {
        return new BuilderFactory>() {
            @NotNull
            @Override
            public Builder> newBuilder() {
                return new AbstractSelfBuilder>(HashSet.empty()) {
                    @NotNull
                    @Override
                    public Builder> add(E element) {
                        result = result.add(element);
                        return this;
                    }
                };
            }
        };
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static  HashSet empty() {
        return EMPTY;
    }

    private final CompactHashMap compactHashMap;

    private HashSet() {
        this(CompactHashMap.empty());
    }

    private HashSet(CompactHashMap compactHashMap) {
        this.compactHashMap = compactHashMap;
    }

    @SuppressWarnings("unchecked")
    private KeyFunction keyFunction() {
        return keyFunction;
    }

    @NotNull
    @Override
    public HashSet add(E value) {
        return new HashSet(compactHashMap.put(value, value, keyFunction()));
    }

    @NotNull
    @Override
    public HashSet remove(E value) {
        return new HashSet(compactHashMap.remove(value, keyFunction()));
    }

    @Override
    public boolean contains(E value) {
        return compactHashMap.get(value, keyFunction()) != null;
    }

    @Override
    public int size() {
        return compactHashMap.size();
    }

    @Override
    public  void forEach(@NotNull Function f) {
        Iterables.forEach(this, f);
    }

    @NotNull
    @Override
    public Iterator iterator() {
        final Iterator> iterator = compactHashMap.iterator(keyFunction());
        return new Iterator() {
            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public E next() {
                return iterator.next().component1();
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy