001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.graph.manager;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.HashMap;
024
025/**
026 * Warning: this is a special map-like construct that suits only and should be used only in this package!
027 * It has the following properties:
028 * <ul>
029 *     <li>memorizes once calculated hashCode</li>
030 *     <li>once hashCode calculated, goes into "read only" mode (put method will fail)</li>
031 *     <li>otherwise all the rest is same as for {@link HashMap}</li>
032 * </ul>
033 *
034 * This class is not a generic map class; only those methods are "protected" that are in use in this very
035 * package.
036 *
037 * @param <K>
038 * @param <V>
039 */
040public class MMap<K, V> {
041    private static final MMap<?, ?> EMPTY_MAP = new MMap<>(new HashMap<>(0)).done();
042
043    /**
044     * Returns empty "done" (immutable) MMap.
045     */
046    @SuppressWarnings("unchecked")
047    public static <K, V> MMap<K, V> empty() {
048        return (MMap<K, V>) MMap.EMPTY_MAP;
049    }
050
051    /**
052     * Returns empty "not done" (mutable) MMap.
053     */
054    public static <K, V> MMap<K, V> emptyNotDone() {
055        return new MMap<>(new HashMap<>());
056    }
057
058    public static <K, V> MMap<K, V> copy(MMap<K, V> orig) {
059        return new MMap<>(new HashMap<>(orig.delegate));
060    }
061
062    public static <K, V> MMap<K, Collection<V>> copyWithKey(K key, MMap<K, Collection<V>> orig) {
063        HashMap<K, Collection<V>> delegateLocal = new HashMap<>(orig.delegate);
064        delegateLocal.computeIfPresent(key, (k, v) -> new ArrayList<>(v));
065        return new MMap<>(delegateLocal);
066    }
067
068    protected final HashMap<K, V> delegate;
069
070    private MMap(HashMap<K, V> delegate) {
071        this.delegate = delegate;
072    }
073
074    public boolean containsKey(K key) {
075        return delegate.containsKey(key);
076    }
077
078    public V get(K key) {
079        return delegate.get(key);
080    }
081
082    public V put(K key, V value) {
083        return delegate.put(key, value);
084    }
085
086    public MMap<K, V> done() {
087        return new DoneMMap<>(delegate);
088    }
089
090    @Override
091    public int hashCode() {
092        throw new IllegalStateException("MMap is not done yet");
093    }
094
095    @Override
096    public boolean equals(Object o) {
097        throw new IllegalStateException("MMap is not done yet");
098    }
099
100    private static class DoneMMap<K, V> extends MMap<K, V> {
101        private final int hashCode;
102
103        private DoneMMap(HashMap<K, V> delegate) {
104            super(delegate);
105            this.hashCode = delegate.hashCode();
106        }
107
108        @Override
109        public V put(K key, V value) {
110            throw new IllegalStateException("Done MMap is immutable");
111        }
112
113        @Override
114        public MMap<K, V> done() {
115            return this;
116        }
117
118        @Override
119        public int hashCode() {
120            return hashCode;
121        }
122
123        @Override
124        public boolean equals(Object o) {
125            if (!(o instanceof MMap)) {
126                return false;
127            }
128            MMap<?, ?> other = (MMap<?, ?>) o;
129            return delegate.equals(other.delegate);
130        }
131    }
132}