View Javadoc
1   package org.apache.maven.surefire.util.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.AbstractMap;
23  import java.util.LinkedHashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import static java.util.Collections.unmodifiableSet;
28  
29  /**
30   * Copies input map in {@link #ImmutableMap(Map) constructor}, and Entries are linked and thread-safe.
31   * The map is immutable with linear list of entries.
32   *
33   * @param <K> key
34   * @param <V> value
35   * @since 2.20
36   */
37  public final class ImmutableMap<K, V>
38          extends AbstractMap<K, V>
39  {
40      private final Node<K, V> first;
41  
42      public ImmutableMap( Map<K, V> map )
43      {
44          Node<K, V> first = null;
45          Node<K, V> previous = null;
46          for ( Entry<K, V> e : map.entrySet() )
47          {
48              Node<K, V> node = new Node<K, V>( e.getKey(), e.getValue() );
49              if ( first == null )
50              {
51                  first = node;
52              }
53              else
54              {
55                  previous.next = node;
56              }
57              previous = node;
58          }
59          this.first = first;
60      }
61  
62      @Override
63      public Set<Entry<K, V>> entrySet()
64      {
65          Set<Entry<K, V>> entries = new LinkedHashSet<Entry<K, V>>();
66          Node<K, V> node = first;
67          while ( node != null )
68          {
69              entries.add( node );
70              node = node.next;
71          }
72          return unmodifiableSet( entries );
73      }
74  
75      static final class Node<K, V>
76              implements Entry<K, V>
77      {
78          final K key;
79          final V value;
80          volatile Node<K, V> next;
81  
82          Node( K key, V value )
83          {
84              this.key = key;
85              this.value = value;
86          }
87  
88          @Override
89          public K getKey()
90          {
91              return key;
92          }
93  
94          @Override
95          public V getValue()
96          {
97              return value;
98          }
99  
100         @Override
101         public V setValue( V value )
102         {
103             throw new UnsupportedOperationException();
104         }
105 
106         @Override
107         public boolean equals( Object o )
108         {
109             if ( this == o )
110             {
111                 return true;
112             }
113 
114             if ( o == null || getClass() != o.getClass() )
115             {
116                 return false;
117             }
118 
119             Node<?, ?> node = (Node<?, ?>) o;
120 
121             return getKey() != null ? getKey().equals( node.getKey() ) : node.getKey() == null
122                            && getValue() != null ? getValue().equals( node.getValue() ) : node.getValue() == null;
123 
124         }
125 
126         @Override
127         public int hashCode()
128         {
129             int result = getKey() != null ? getKey().hashCode() : 0;
130             result = 31 * result + ( getValue() != null ? getValue().hashCode() : 0 );
131             return result;
132         }
133     }
134 }