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

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

/*
 * 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.AbstractMap;
import com.github.andrewoma.dexx.collection.internal.builder.AbstractSelfBuilder;
import com.github.andrewoma.dexx.collection.internal.hashmap.CompactHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

/**
 * {@code HashMap} is an implementation of {@code Map} based on a hash trie.
 *
 * 

The underlying implementation is a port of Scala's HashMap which is an implementation of a * hash array mapped trie. */ public class HashMap extends AbstractMap { private static final HashMap EMPTY = new HashMap(); private static final KeyFunction keyFunction = new KeyFunction() { @NotNull public Object key(@NotNull Pair value) { return value.component1(); } }; @SuppressWarnings("unchecked") @NotNull public static HashMap empty() { return EMPTY; } @NotNull public static BuilderFactory, HashMap> factory() { return new BuilderFactory, HashMap>() { @NotNull @Override public Builder, HashMap> newBuilder() { return new AbstractSelfBuilder, HashMap>(HashMap.empty()) { @NotNull @Override public Builder, HashMap> add(Pair element) { result = result.put(element.component1(), element.component2()); return this; } }; } }; } private final CompactHashMap> compactHashMap; @SuppressWarnings("unchecked") private KeyFunction> keyFunction() { return keyFunction; } @Override public boolean containsKey(@NotNull K key) { return compactHashMap.get(key, keyFunction()) != null; } public HashMap() { this(CompactHashMap.>empty()); } private HashMap(CompactHashMap> compactHashMap) { this.compactHashMap = compactHashMap; } @NotNull @Override public HashMap put(@NotNull K key, V value) { return new HashMap(compactHashMap.put(key, new Pair(key, value), keyFunction())); } @Nullable @Override public V get(@NotNull K key) { Pair pair = compactHashMap.get(key, keyFunction()); return pair == null ? null : pair.component2(); } @NotNull @Override public HashMap remove(@NotNull K key) { return new HashMap(compactHashMap.remove(key, keyFunction())); } @Override public int size() { return compactHashMap.size(); } @Override public void forEach(@NotNull final Function, U> f) { compactHashMap.forEach(new Function>, Object>() { @Override public Object invoke(Pair> pair) { f.invoke(pair.component2()); return null; } }, keyFunction()); } @NotNull @Override public Iterator> iterator() { return new Itr(compactHashMap.iterator(keyFunction())); } static class Itr implements Iterator> { final Iterator>> iterator; public Itr(Iterator>> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Pair next() { Pair> next = iterator.next(); return next.component2(); } @Override public void remove() { throw new UnsupportedOperationException(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy