1 package org.apache.maven.surefire.api.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
33
34
35
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<>( 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<>();
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 }