-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathAttributeKeyValueMap.java
More file actions
183 lines (147 loc) · 3.73 KB
/
AttributeKeyValueMap.java
File metadata and controls
183 lines (147 loc) · 3.73 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.attribute;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import com.attribute.interfaces.IAttributeKeyValueMap;
import com.exceptions.DataFormatIncorrectException;
// TODO: Auto-generated Javadoc
/**
* The Class AttributeKeyValueMap.
*
* @param <T> the generic type
* @param <Key> the generic type
* @param <Value> the generic type
*/
public abstract class AttributeKeyValueMap<T extends Object, Key extends Object, Value extends Object> implements IAttributeKeyValueMap<Key, Value>
{
/** The key value map. */
private HashMap<Key, T> keyValueMap;
/** The value key map. */
private HashMap<T, HashSet<Key>> valueKeyMap;
/** The t value map. */
private HashMap<T, Value> tValueMap;
/**
* Instantiates a new attribute key value map.
*/
public AttributeKeyValueMap() {
this.keyValueMap = new HashMap<>();
this.valueKeyMap = new HashMap<T, HashSet<Key>>();
this.tValueMap = new HashMap<T, Value>();
}
/**
* Gets the value as T.
*
* @param value the value
* @return the value as T
* @throws DataFormatIncorrectException the data format incorrect exception
*/
protected abstract T getValueAsT(Value value) throws DataFormatIncorrectException;
/**
* Checks if is valid type.
*
* @param value the value
* @return true, if is valid type
*/
@Override
public abstract boolean isValidType(Value value);
/**
* Gets the.
*
* @param key the key
* @return the value
*/
@Override
public Value get(Key key) {
if (!keyValueMap.containsKey(key)) {
}
return this.getValue(this.keyValueMap.get(key));
}
/**
* Put.
*
* @param key the key
* @param value the value
* @throws DataFormatIncorrectException the data format incorrect exception
*/
@Override
public void put(Key key, Value value) throws DataFormatIncorrectException {
if (!isValidType(value)) {
throw new DataFormatIncorrectException();
}
if (this.keyValueMap.containsKey(key)) {
this.valueKeyMap.remove(this.keyValueMap.get(key));
}
this.keyValueMap.put(key, this.getValueAsT(value));
if (!this.valueKeyMap.containsKey(this.getValueAsT(value))) {
this.valueKeyMap.put(this.getValueAsT(value), new HashSet<Key>());
}
this.valueKeyMap.get(this.getValueAsT(value)).add(key);
this.tValueMap.put(this.getValueAsT(value), value);
}
/**
* Contains key.
*
* @param key the key
* @return true, if successful
*/
@Override
public boolean containsKey(Key key) {
if (this.keyValueMap.containsKey(key)) {
return true;
}
return false;
}
/**
* Gets the keys with values.
*
* @param value the value
* @return the keys with values
* @throws DataFormatIncorrectException the data format incorrect exception
*/
@Override
public List<Key> getkeysWithValues(Value value) throws DataFormatIncorrectException {
if (!this.isValidType(value)) {
return new ArrayList<Key>();
}
List<Key> result = new ArrayList<Key>();
if (!this.valueKeyMap.containsKey(this.getValueAsT(value))) {
return new ArrayList<Key>();
}
Iterator<Key> it = this.valueKeyMap.get(getValueAsT(value)).iterator();
while (it.hasNext()) {
result.add(it.next());
}
return result;
}
/**
* Delete key.
*
* @param key the key
*/
@Override
public void deleteKey(Key key) {
if (!this.keyValueMap.containsKey(key)) {
return;
}
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
return "AttributeKeyValueMap [keyValueMap=" + keyValueMap + ", valueKeyMap=" + valueKeyMap + "]";
}
/**
* Gets the value.
*
* @param obj the obj
* @return the value
*/
protected Value getValue(T obj) {
return this.tValueMap.get(obj);
}
}