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

org.jboss.weld.util.collections.ImmutableMap Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.util.collections;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import org.jboss.weld.util.Preconditions;

/**
 * Weld's immutable map implementation.
 *
 * @author Jozef Hartinger
 *
 */
public abstract class ImmutableMap extends AbstractImmutableMap {

    ImmutableMap() {
    }

    /**
     * Creates an immutable map. A copy of the given map is used. As a result, it is safe to modify the source map afterwards.
     *
     * @param map the given map
     * @return an immutable map
     */
    public static  Map copyOf(Map map) {
        Preconditions.checkNotNull(map);
        return ImmutableMap. builder().putAll(map).build();
    }

    /**
     * Creates an immutable singleton instance.
     *
     * @param key
     * @param value
     * @return
     */
    public static  Map of(K key, V value) {
        return new ImmutableMapEntry(key, value);
    }

    /**
     * Returns a collector that accumulates elements into an immutable map.
     * 

* Duplicate mappings are not merged - the old value is replaced. * * @param keyMapper * @param valueMapper * @return collector */ public static ImmutableMapCollector collector(Function keyMapper, Function valueMapper) { return new ImmutableMapCollector<>(keyMapper, valueMapper); } /** * Creates a new empty builder for building immutable map. * * @return a new empty builder */ public static Builder builder() { return new HashMapBuilder(); } public interface Builder { Builder put(K key, V value); Builder putAll(Map items); Map build(); } private static class HashMapBuilder implements Builder { private static final int DEFAULT_INITIAL_CAPACITY = 4; private static final float LOAD_FACTOR = 1.2f; private Map map; private HashMapBuilder() { this.map = new HashMap(DEFAULT_INITIAL_CAPACITY, LOAD_FACTOR); } @Override public Builder put(K key, V value) { map.put(key, value); return this; } @Override public Builder putAll(Map items) { map.putAll(items); return this; } HashMapBuilder putAll(HashMapBuilder items) { map.putAll(items.map); return this; } @Override public Map build() { if (map.isEmpty()) { return Collections.emptyMap(); } if (map.size() == 1) { return new ImmutableMapEntry(map.entrySet().iterator().next()); } return Collections.unmodifiableMap(map); } } private static class ImmutableMapCollector implements Collector, Map> { private static final Set CHARACTERISTICS = ImmutableSet.of(); private final Function keyMapper; private final Function valueMapper; private ImmutableMapCollector(Function keyMapper, Function valueMapper) { this.keyMapper = keyMapper; this.valueMapper = valueMapper; } @Override public Supplier> supplier() { return HashMapBuilder::new; } @Override public BiConsumer, T> accumulator() { return (b, i) -> b.put(keyMapper.apply(i), valueMapper.apply(i)); } @Override public BinaryOperator> combiner() { return (builder1, builder2) -> builder1.putAll(builder2); } @Override public Function, Map> finisher() { return HashMapBuilder::build; } @Override public Set characteristics() { return CHARACTERISTICS; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy