View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.manager;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  
25  /**
26   * Warning: this is a special map-like construct that suits only and should be used only in this package!
27   * It has the following properties:
28   * <ul>
29   *     <li>memorizes once calculated hashCode</li>
30   *     <li>once hashCode calculated, goes into "read only" mode (put method will fail)</li>
31   *     <li>otherwise all the rest is same as for {@link HashMap}</li>
32   * </ul>
33   *
34   * This class is not a generic map class; only those methods are "protected" that are in use in this very
35   * package.
36   *
37   * @param <K>
38   * @param <V>
39   */
40  public class MMap<K, V> {
41      private static final MMap<?, ?> EMPTY_MAP = new MMap<>(new HashMap<>(0)).done();
42  
43      @SuppressWarnings("unchecked")
44      public static <K, V> MMap<K, V> empty() {
45          return (MMap<K, V>) MMap.EMPTY_MAP;
46      }
47  
48      public static <K, V> MMap<K, V> copy(MMap<K, V> orig) {
49          return new MMap<>(new HashMap<>(orig.delegate));
50      }
51  
52      public static <K, V> MMap<K, Collection<V>> copyWithKey(K key, MMap<K, Collection<V>> orig) {
53          HashMap<K, Collection<V>> delegateLocal = new HashMap<>(orig.delegate);
54          delegateLocal.computeIfPresent(key, (k, v) -> new ArrayList<>(v));
55          return new MMap<>(delegateLocal);
56      }
57  
58      protected final HashMap<K, V> delegate;
59  
60      private MMap(HashMap<K, V> delegate) {
61          this.delegate = delegate;
62      }
63  
64      public boolean containsKey(K key) {
65          return delegate.containsKey(key);
66      }
67  
68      public V get(K key) {
69          return delegate.get(key);
70      }
71  
72      public V put(K key, V value) {
73          return delegate.put(key, value);
74      }
75  
76      public MMap<K, V> done() {
77          return new DoneMMap<>(delegate);
78      }
79  
80      @Override
81      public int hashCode() {
82          throw new IllegalStateException("MMap is not done yet");
83      }
84  
85      @Override
86      public boolean equals(Object o) {
87          throw new IllegalStateException("MMap is not done yet");
88      }
89  
90      private static class DoneMMap<K, V> extends MMap<K, V> {
91          private final int hashCode;
92  
93          private DoneMMap(HashMap<K, V> delegate) {
94              super(delegate);
95              this.hashCode = delegate.hashCode();
96          }
97  
98          @Override
99          public V put(K key, V value) {
100             throw new IllegalStateException("Done MMap is immutable");
101         }
102 
103         @Override
104         public MMap<K, V> done() {
105             return this;
106         }
107 
108         @Override
109         public int hashCode() {
110             return hashCode;
111         }
112 
113         @Override
114         public boolean equals(Object o) {
115             if (!(o instanceof MMap)) {
116                 return false;
117             }
118             MMap<?, ?> other = (MMap<?, ?>) o;
119             return delegate.equals(other.delegate);
120         }
121     }
122 }