|
Lines 14-19
Link Here
|
| 14 |
import java.util.Collections; |
14 |
import java.util.Collections; |
| 15 |
import java.util.HashMap; |
15 |
import java.util.HashMap; |
| 16 |
import java.util.HashSet; |
16 |
import java.util.HashSet; |
|
|
17 |
import java.util.Iterator; |
| 17 |
import java.util.Map; |
18 |
import java.util.Map; |
| 18 |
import java.util.Set; |
19 |
import java.util.Set; |
| 19 |
|
20 |
|
|
Lines 22-77
Link Here
|
| 22 |
*/ |
23 |
*/ |
| 23 |
|
24 |
|
| 24 |
public final class IndexedSet<K,V> |
25 |
public final class IndexedSet<K,V> |
| 25 |
|
|
|
| 26 |
extends HashSet<V> |
| 27 |
|
| 28 |
{ |
26 |
{ |
| 29 |
private static final long serialVersionUID = 1L; |
27 |
private final Set<V> set; |
| 30 |
private final Set<V> unmodifiable; |
28 |
private final Set<V> unmodifiable; |
| 31 |
private final Map<K,V> index; |
29 |
private final Map<K,V> index; |
| 32 |
|
30 |
|
| 33 |
public IndexedSet() |
31 |
public IndexedSet() |
| 34 |
{ |
32 |
{ |
| 35 |
this.unmodifiable = Collections.unmodifiableSet( this ); |
33 |
this.set = new HashSet<V>(); |
|
|
34 |
this.unmodifiable = Collections.unmodifiableSet( this.set ); |
| 36 |
this.index = new HashMap<K,V>(); |
35 |
this.index = new HashMap<K,V>(); |
| 37 |
} |
36 |
} |
| 38 |
|
37 |
|
| 39 |
public void add( final K key, |
38 |
public Set<V> getItemSet() |
| 40 |
final V value ) |
|
|
| 41 |
{ |
39 |
{ |
| 42 |
remove( this.index.get( key ) ); |
40 |
return this.unmodifiable; |
| 43 |
add( value ); |
41 |
} |
| 44 |
this.index.put( key, value ); |
42 |
|
|
|
43 |
public V getItemByKey( final K key ) |
| 44 |
{ |
| 45 |
return this.index.get( key ); |
| 45 |
} |
46 |
} |
| 46 |
|
47 |
|
| 47 |
public boolean delete( final K key ) |
48 |
public boolean containsKey( final K key ) |
| 48 |
{ |
49 |
{ |
| 49 |
final Object value = this.index.get( key ); |
50 |
return this.index.containsKey( key ); |
| 50 |
|
51 |
} |
| 51 |
if( value == null ) |
52 |
|
| 52 |
{ |
53 |
public boolean containsItem( final V item ) |
| 53 |
return false; |
54 |
{ |
| 54 |
} |
55 |
return this.set.contains( item ); |
| 55 |
else |
56 |
} |
|
|
57 |
|
| 58 |
public void addItem( final V item ) |
| 59 |
{ |
| 60 |
if( item == null ) |
| 56 |
{ |
61 |
{ |
| 57 |
remove( value ); |
62 |
throw new IllegalArgumentException(); |
| 58 |
this.index.remove( key ); |
|
|
| 59 |
return true; |
| 60 |
} |
63 |
} |
|
|
64 |
|
| 65 |
this.set.add( item ); |
| 61 |
} |
66 |
} |
| 62 |
|
67 |
|
| 63 |
public V get( final K key ) |
68 |
public void addItemWithKey( final K key, |
|
|
69 |
final V item ) |
| 64 |
{ |
70 |
{ |
| 65 |
return this.index.get( key ); |
71 |
addItem( item ); |
|
|
72 |
addKey( key, item ); |
| 66 |
} |
73 |
} |
| 67 |
|
74 |
|
| 68 |
public boolean containsKey( final K key ) |
75 |
public void addKey( final K key, |
|
|
76 |
final V item ) |
| 69 |
{ |
77 |
{ |
| 70 |
return this.index.containsKey( key ); |
78 |
if( key == null || item == null ) |
|
|
79 |
{ |
| 80 |
throw new IllegalArgumentException(); |
| 81 |
} |
| 82 |
|
| 83 |
if( ! this.set.contains( item ) ) |
| 84 |
{ |
| 85 |
throw new IllegalArgumentException(); |
| 86 |
} |
| 87 |
|
| 88 |
this.index.put( key, item ); |
| 71 |
} |
89 |
} |
| 72 |
|
90 |
|
| 73 |
public Set<V> getUnmodifiable() |
91 |
public boolean removeItem( final V item ) |
| 74 |
{ |
92 |
{ |
| 75 |
return this.unmodifiable; |
93 |
if( this.set.remove( item ) ) |
|
|
94 |
{ |
| 95 |
for( Iterator<Map.Entry<K,V>> itr = this.index.entrySet().iterator(); itr.hasNext(); ) |
| 96 |
{ |
| 97 |
final Map.Entry<K,V> entry = itr.next(); |
| 98 |
|
| 99 |
if( entry.getValue() == item ) |
| 100 |
{ |
| 101 |
itr.remove(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
public boolean removeItemByKey( final K key ) |
| 112 |
{ |
| 113 |
final V item = this.index.get( key ); |
| 114 |
|
| 115 |
if( item != null ) |
| 116 |
{ |
| 117 |
return removeItem( item ); |
| 118 |
} |
| 119 |
|
| 120 |
return false; |
| 76 |
} |
121 |
} |
|
|
122 |
|
| 77 |
} |
123 |
} |