litesoft
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
|
package com.temp.client.foundation.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
*
* @author palla
*
* @param <T>
*
* This object will allow storing objects in the order of insertion.
* In addition , this object will also allow for reordering the elements in the list
* The map stores the actual objects and prevents duplicates
*/
public class ReorderableMap<T> {
private Map<String, T> map;
private List<String> order;
private long sequence;
public ReorderableMap() {
map = new HashMap<String, T>();
order = new ArrayList<String>();
sequence = 1;
}
public void clear() {
map.clear();
order.clear();
}
public boolean isEmpty() {
return(map.isEmpty());
}
public List<T> getOrderedElements() {
List<T> orderedElements = new LinkedList<T>();
for (String name : order) {
if (map.containsKey(name)) {
orderedElements.add(map.get(name));
}
}
return orderedElements;
}
public List<String> getKeys() {
List<String> keys = new ArrayList<String>(order);
return keys;
}
public LinkedHashMap<String,T> getOrderedMap() {
LinkedHashMap<String,T> orderedElements = new LinkedHashMap<String,T>();
for (String key : order) {
orderedElements.put(key, map.get(key));
}
return orderedElements;
}
public boolean addElement(String key, T value){
boolean added = false;
if(!map.containsKey(key)){
map.put(key, value);
order.add(key);
added = true;
}
return added;
}
public void removeElement(String key){
order.remove(key);
map.remove(key);
}
public void reOrder(String key, boolean up){
int index = order.indexOf(key);
int swapIndex = -1;
if(index >= 0) {
if(up) {
swapIndex = index - 1;
} else {
swapIndex = index + 1;
}
}
if(swapIndex >= 0 && swapIndex < order.size()) {
String swapAttribute = order.get(swapIndex);
order.set(swapIndex, key);
order.set(index, swapAttribute);
}
}
public String getNextSequence() {
return(String.valueOf(sequence++));
}
}
|
Revision |
Author |
Commited |
Message |
626
|
GeorgeS
|
Wed 11 Apr, 2012 19:39:41 +0000 |
|