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

io.gravitee.common.util.Maps Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/*
 * Copyright © 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.common.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public class Maps {

    private Maps() {}

    public static  MapBuilder builder() {
        return builder(HashMap::new);
    }

    public static  MapBuilder builder(Supplier> mapSupplier) {
        return new MapBuilder<>(mapSupplier.get());
    }

    public static  ConcurrentMapBuilder concurrentBuilder() {
        return concurrentBuilder(ConcurrentHashMap::new);
    }

    public static  ConcurrentMapBuilder concurrentBuilder(Supplier> mapSupplier) {
        return new ConcurrentMapBuilder<>(mapSupplier.get());
    }

    private static class BaseBuilder, K, V> {

        protected final M map;

        public BaseBuilder(M map) {
            this.map = map;
        }

        public BaseBuilder put(K key, V value) {
            map.put(key, value);
            return this;
        }

        public M build() {
            return map;
        }
    }

    public static class MapBuilder extends BaseBuilder, K, V> {

        private boolean unmodifiable;

        public MapBuilder(Map map) {
            super(map);
        }

        @Override
        public MapBuilder put(K key, V value) {
            super.put(key, value);
            return this;
        }

        public MapBuilder unmodifiable(boolean unmodifiable) {
            this.unmodifiable = unmodifiable;
            return this;
        }

        @Override
        public Map build() {
            if (unmodifiable) {
                return Collections.unmodifiableMap(super.build());
            } else {
                return super.build();
            }
        }
    }

    public static class ConcurrentMapBuilder extends BaseBuilder, K, V> {

        public ConcurrentMapBuilder(ConcurrentMap map) {
            super(map);
        }

        @Override
        public ConcurrentMapBuilder put(K key, V value) {
            super.put(key, value);
            return this;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy