Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/util/ReorderableMap.java

Diff revisions: vs.
  @@ -1,92 +1,91 @@
1 1 package com.temp.client.foundation.util;
2 2
3 - import java.util.ArrayList;
4 - import java.util.HashMap;
5 - import java.util.LinkedHashMap;
6 - import java.util.LinkedList;
7 - import java.util.List;
8 - import java.util.Map;
3 + import java.util.*;
4 +
9 5 /**
6 + * @param <T> This object will allow storing objects in the order of insertion.
7 + * In addition , this object will also allow for reordering the elements in the list
8 + * The map stores the actual objects and prevents duplicates
10 9 *
11 10 * @author palla
12 - *
13 - * @param <T>
14 - *
15 - * This object will allow storing objects in the order of insertion.
16 - * In addition , this object will also allow for reordering the elements in the list
17 - * The map stores the actual objects and prevents duplicates
18 11 */
19 12
20 13 public class ReorderableMap<T> {
21 14 private Map<String, T> map;
22 15 private List<String> order;
23 16 private long sequence;
17 +
24 18 public ReorderableMap() {
25 19 map = new HashMap<String, T>();
26 20 order = new ArrayList<String>();
27 21 sequence = 1;
28 22 }
23 +
29 24 public void clear() {
30 25 map.clear();
31 26 order.clear();
32 27 }
28 +
33 29 public boolean isEmpty() {
34 - return(map.isEmpty());
30 + return (map.isEmpty());
35 31 }
32 +
36 33 public List<T> getOrderedElements() {
37 34 List<T> orderedElements = new LinkedList<T>();
38 - for (String name : order) {
39 - if (map.containsKey(name)) {
40 - orderedElements.add(map.get(name));
35 + for ( String name : order ) {
36 + if ( map.containsKey( name ) ) {
37 + orderedElements.add( map.get( name ) );
41 38 }
42 39 }
43 40 return orderedElements;
44 41 }
45 42
46 43 public List<String> getKeys() {
47 - List<String> keys = new ArrayList<String>(order);
44 + List<String> keys = new ArrayList<String>( order );
48 45 return keys;
49 46 }
50 - public LinkedHashMap<String,T> getOrderedMap() {
51 - LinkedHashMap<String,T> orderedElements = new LinkedHashMap<String,T>();
52 - for (String key : order) {
53 - orderedElements.put(key, map.get(key));
47 +
48 + public LinkedHashMap<String, T> getOrderedMap() {
49 + LinkedHashMap<String, T> orderedElements = new LinkedHashMap<String, T>();
50 + for ( String key : order ) {
51 + orderedElements.put( key, map.get( key ) );
54 52 }
55 53 return orderedElements;
56 54 }
57 55
58 - public boolean addElement(String key, T value){
56 + public boolean addElement( String key, T value ) {
59 57 boolean added = false;
60 - if(!map.containsKey(key)){
61 - map.put(key, value);
62 - order.add(key);
58 + if ( !map.containsKey( key ) ) {
59 + map.put( key, value );
60 + order.add( key );
63 61 added = true;
64 62 }
65 63 return added;
66 64 }
67 - public void removeElement(String key){
68 - order.remove(key);
69 - map.remove(key);
65 +
66 + public void removeElement( String key ) {
67 + order.remove( key );
68 + map.remove( key );
70 69 }
71 - public void reOrder(String key, boolean up){
72 - int index = order.indexOf(key);
70 +
71 + public void reOrder( String key, boolean up ) {
72 + int index = order.indexOf( key );
73 73 int swapIndex = -1;
74 - if(index >= 0) {
75 - if(up) {
74 + if ( index >= 0 ) {
75 + if ( up ) {
76 76 swapIndex = index - 1;
77 77 } else {
78 78 swapIndex = index + 1;
79 79 }
80 -
81 80 }
82 - if(swapIndex >= 0 && swapIndex < order.size()) {
83 - String swapAttribute = order.get(swapIndex);
84 - order.set(swapIndex, key);
85 - order.set(index, swapAttribute);
81 + if ( swapIndex >= 0 && swapIndex < order.size() ) {
82 + String swapAttribute = order.get( swapIndex );
83 + order.set( swapIndex, key );
84 + order.set( index, swapAttribute );
86 85 }
87 -
88 86 }
87 +
89 88 public String getNextSequence() {
90 - return(String.valueOf(sequence++));
89 + return (String.valueOf( sequence++ ));
91 90 }
92 91 }