Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf471f4bb87a38b26b47fd9ca7e835ad04c27216 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 *  Copyright (c) 2010  Oracle. All rights reserved.
 *  This program and the accompanying materials are made available under the
 *  terms of the Eclipse Public License v1.0, which accompanies this distribution
 *  and is available at http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.common.utility.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * This class maintains a {@link Set} of items, and a {@link Map} of keys to those items.
 * An item may have multiple keys, but an item may have no keys and remain in the set.  Once an 
 * item's last key is removed, the item is also removed.
 */
public class KeyedSet<K, V> {
	
	private final Set<V> itemSet;
	private final Set<V> unmodifiableItemSet;
	private final Map<K,V> map;
	
	
	public KeyedSet() {
		this.itemSet = new HashSet<V>();
		this.unmodifiableItemSet = Collections.unmodifiableSet(this.itemSet);
		this.map = new HashMap<K,V>();
	}
	
	/**
	 * Return an unmodifiable representation of the set of items.
	 */
	public Set<V> getItemSet() {
		return this.unmodifiableItemSet;
	}
	
	/**
	 * Return the item stored under the given key.
	 */
	public V getItem(K key) {
		return this.map.get(key);
	}
	
	/**
	 * Return whether an item is stored under the given key.
	 */
	public boolean containsKey(K key) {
		return this.map.containsKey(key);
	}
	
	/**
	 * Return whether the item is stored under *any* key.
	 */
	public boolean containsItem(V item) {
		return this.itemSet.contains(item);
	}
	
	/**
	 * Add an item to be stored under the given key.  
	 * The item must not already be stored.
	 */
	public void addItem(K key, V item) {
		addItem(item);
		addKey(key, item);
	}
	
	private void addItem(V item) {
		if (item == null) {
			throw new IllegalArgumentException();
		}
		this.itemSet.add(item);
	}
	
	/** 
	 * Add an additional key to an item already stored under an alternate key.
	 */
	public void addKey(K key, V item) {
		if (key == null || item == null) {
			throw new IllegalArgumentException();
		}
		if (! this.itemSet.contains(item)) {
			throw new IllegalArgumentException();
		}
		this.map.put(key, item);
	}
	
	/**
	 * Remove the given item and remove any key-to-item mapping it may have.
	 */
	public boolean removeItem(V item) {
		if (this.itemSet.remove(item)) {
			for (Map.Entry<K,V> entry : CollectionTools.collection(this.map.entrySet())) {
				if (entry.getValue() == item) {
					map.remove(entry.getKey());
				}
			}
			return true;
		}
		return false;
	}
	
	/**
	 * Remove the key-to-item mapping for the given key.
	 * If it is the last key to the item, also remove the item.
	 */
	public boolean removeKey(K key) {
		final V item = this.map.get(key);
		if (item != null) {
			this.map.remove(key);
			boolean otherKey = false;
			for (Map.Entry<K,V> entry : CollectionTools.collection(this.map.entrySet())) {
				if (otherKey | entry.getValue() == item) {
					otherKey = true;
				}
			}
			if (! otherKey) {
				removeItem(item);
			}
			return true;
		}
		return false;
	}
}

Back to the top