View Javadoc
1   package org.apache.maven.plugin.surefire.util;
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.Collections;
24  import java.util.LinkedHashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  /**
29   * Copies input map in {@link #ImmutableMap(Map) constructor}, and Entries are linked and thread-safe.
30   * The map is immutable.
31   *
32   * @param <K> key
33   * @param <V> value
34   * @since 2.20
35   */
36  public final class ImmutableMap<K, V>
37          extends AbstractMap<K, V>
38  {
39      private final Node<K, V> first;
40  
41      public ImmutableMap( Map<K, V> map )
42      {
43          Node<K, V> first = null;
44          Node<K, V> previous = null;
45          for ( Entry<K, V> e : map.entrySet() )
46          {
47              Node<K, V> node = new Node<K, V>( e.getKey(), e.getValue() );
48              if ( first == null )
49              {
50                  first = node;
51              }
52              else
53              {
54                  previous.next = node;
55              }
56              previous = node;
57          }
58          this.first = first;
59      }
60  
61      @Override
62      public Set<Entry<K, V>> entrySet()
63      {
64          Set<Entry<K, V>> entries = new LinkedHashSet<Entry<K, V>>();
65          Node<K, V> node = first;
66          while ( node != null )
67          {
68              entries.add( node );
69              node = node.next;
70          }
71          return Collections.<Entry<K, V>>unmodifiableSet( entries );
72      }
73  
74      static final class Node<K, V>
75              implements Entry<K, V>
76      {
77          final K key;
78          final V value;
79          volatile Node<K, V> next;
80  
81          Node( K key, V value )
82          {
83              this.key = key;
84              this.value = value;
85          }
86  
87          @Override
88          public K getKey()
89          {
90              return key;
91          }
92  
93          @Override
94          public V getValue()
95          {
96              return value;
97          }
98  
99          @Override
100         public V setValue( V value )
101         {
102             throw new UnsupportedOperationException();
103         }
104 
105         @Override
106         public boolean equals( Object o )
107         {
108             if ( this == o )
109             {
110                 return true;
111             }
112 
113             if ( o == null || getClass() != o.getClass() )
114             {
115                 return false;
116             }
117 
118             Node<?, ?> node = (Node<?, ?>) o;
119 
120             return getKey() != null ? getKey().equals( node.getKey() ) : node.getKey() == null
121                            && getValue() != null ? getValue().equals( node.getValue() ) : node.getValue() == null;
122 
123         }
124 
125         @Override
126         public int hashCode()
127         {
128             int result = getKey() != null ? getKey().hashCode() : 0;
129             result = 31 * result + ( getValue() != null ? getValue().hashCode() : 0 );
130             return result;
131         }
132     }
133 }