1 package org.apache.maven.plugin.surefire.util;
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.Collections;
24 import java.util.LinkedHashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28
29
30
31
32
33
34
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 }