Mercurial > hg > Members > shoshi > TreeCMSv2
annotate src/treecms/tree/util/NodeChildrenImpl.java @ 22:fa784faafc78
commit
author | shoshi |
---|---|
date | Tue, 07 Jun 2011 16:42:49 +0900 |
parents | f3150b37f9be |
children | 77a894c0b919 |
rev | line source |
---|---|
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
1 package treecms.tree.util; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
2 |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
3 import java.util.Collections; |
20 | 4 import java.util.HashMap; |
5 import java.util.List; | |
6 import java.util.Map; | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
7 import java.util.Set; |
20 | 8 import java.util.concurrent.ConcurrentHashMap; |
22 | 9 import java.util.concurrent.ConcurrentMap; |
20 | 10 import java.util.concurrent.CopyOnWriteArrayList; |
22 | 11 |
12 import treecms.api.NodeAttributes; | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
13 import treecms.api.NodeChildren; |
20 | 14 import treecms.api.NodeContext; |
15 import treecms.api.NodeID; | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
16 |
22 | 17 public class NodeChildrenImpl<T extends NodeContext & NodeAttributes & NodeChildren<T>> implements NodeChildren<T> |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
18 { |
22 | 19 private final ConcurrentMap<String,T> m_map; |
20 | 20 private final List<T> m_list; |
21 private final List<T> m_readOnlyList; | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
22 |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
23 public NodeChildrenImpl() |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
24 { |
20 | 25 m_map = new ConcurrentHashMap<String,T>(); |
26 m_list = new CopyOnWriteArrayList<T>(); | |
27 m_readOnlyList = Collections.unmodifiableList(m_list); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
28 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
29 |
17 | 30 public NodeChildrenImpl(T... _args) |
31 { | |
32 this(); | |
33 for(T i : _args){ | |
20 | 34 String fid = i.getID().getFamilyID(); |
35 if(!m_map.containsKey(fid)){ | |
36 m_map.put(fid,i); | |
37 m_list.add(i); | |
17 | 38 } |
39 } | |
40 } | |
41 | |
15 | 42 public NodeChildrenImpl(NodeChildren<T> _list) |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
43 { |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
44 this(); |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
45 |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
46 if(_list != null){ |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
47 addAll(_list); |
20 | 48 }else{ |
49 throw new NullPointerException("_list is null"); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
50 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
51 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
52 |
20 | 53 @Override |
54 public synchronized List<T> getList() | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
55 { |
20 | 56 return m_readOnlyList; |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
57 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
58 |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
59 @Override |
22 | 60 public T create(NodeAttributes _attr) |
61 { | |
62 throw new UnsupportedOperationException(); | |
63 } | |
64 | |
20 | 65 public synchronized T replace(T _node) |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
66 { |
20 | 67 String fid = _node.getID().getFamilyID(); |
68 | |
69 T old = m_map.get(fid); | |
70 | |
71 if(old != null){ | |
72 m_map.put(fid,_node); | |
73 | |
74 //find index of _node , better put index with node in the map? | |
75 int size = m_list.size(); | |
76 for(int i = 0;i < size;i ++){ | |
77 T n = m_list.get(i); | |
78 NodeID nid = n.getID(); | |
79 if(nid.isFamily(nid)){ | |
80 m_list.set(i,_node); | |
81 return old; | |
82 } | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
83 } |
20 | 84 |
85 throw new IllegalStateException("FamilyID is found on the map but not found on the list"); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
86 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
87 return null; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
88 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
89 |
20 | 90 public synchronized boolean add(T _n) |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
91 { |
20 | 92 if(!m_map.containsKey(_n.getID().getFamilyID())){ |
93 m_map.put(_n.getID().getFamilyID(),_n); | |
94 return m_list.add(_n); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
95 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
96 return false; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
97 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
98 |
20 | 99 public synchronized boolean addAll(NodeChildren<T> _list) |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
100 { |
21 | 101 if(_list == this){ |
102 return false; | |
103 } | |
104 | |
20 | 105 if(Collections.disjoint(getFamilyIDSet(),_list.getFamilyIDSet())){ |
106 | |
107 HashMap<String,T> map = new HashMap<String,T>(); | |
108 for(T item : _list.getList()){ | |
109 NodeID id = item.getID(); | |
110 map.put(id.getFamilyID(),item); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
111 } |
20 | 112 |
113 return m_list.addAll(_list.getList()); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
114 } |
20 | 115 return false; |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
116 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
117 |
20 | 118 @Override |
119 public synchronized T get(int _index) | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
120 { |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
121 return m_list.get(_index); |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
122 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
123 |
20 | 124 @Override |
125 public synchronized T get(String _familyID) | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
126 { |
20 | 127 return m_map.get(_familyID); |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
128 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
129 |
20 | 130 @Override |
131 public synchronized T remove(int _index) | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
132 { |
20 | 133 T n = m_list.remove(_index); |
134 NodeID id = n.getID(); | |
135 if(m_map.remove(id.getFamilyID()) != null){ | |
136 return n; | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
137 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
138 |
20 | 139 throw new IllegalStateException(""); |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
140 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
141 |
20 | 142 @Override |
143 public synchronized boolean contains(NodeID _id) | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
144 { |
20 | 145 T n = m_map.get(_id); |
146 NodeID id = n.getID(); | |
147 return id.equals(_id); | |
148 } | |
149 | |
150 @Override | |
151 public synchronized void clearChildren() | |
152 { | |
153 m_map.clear(); | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
154 m_list.clear(); |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
155 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
156 |
16 | 157 @SuppressWarnings("unchecked") |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
158 @Override |
20 | 159 public synchronized boolean equals(Object _o) |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
160 { |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
161 NodeChildrenImpl<T> list = (NodeChildrenImpl<T>)_o; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
162 return m_list.equals(list.m_list); |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
163 } |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
164 |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
165 @Override |
20 | 166 public synchronized int hashCode() |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
167 { |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
168 int result = 17; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
169 result = 37*result + m_list.hashCode(); |
20 | 170 result = 37*result + m_map.hashCode(); |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
171 return result; |
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
172 } |
20 | 173 |
174 @Override | |
175 public synchronized Set<String> getFamilyIDSet() | |
176 { | |
177 return m_map.keySet(); | |
178 } | |
179 | |
180 @Override | |
181 public synchronized T remove(String _fid) | |
182 { | |
183 return m_map.remove(_fid); | |
184 } | |
185 | |
186 @Override | |
187 public synchronized boolean swap(String _fid1, String _fid2) | |
188 { | |
189 if(m_map.containsKey(_fid1) && m_map.containsKey(_fid2)){ | |
190 int index1,index2; | |
191 index1 = index2 = -1; | |
192 int size = m_list.size(); | |
193 for(int i = 0;i < size;i ++){ | |
194 T n = m_list.get(i); | |
195 String fid = n.getID().getFamilyID(); | |
196 | |
197 if(fid.equals(_fid1)){ | |
198 index1 = i; | |
199 }else if(fid.equals(_fid2)){ | |
200 index2 = i; | |
201 } | |
202 | |
203 if(index1 != -1 && index2 != -1){ | |
204 Collections.swap(m_list, index1, index2); | |
205 return true; | |
206 } | |
207 } | |
208 } | |
209 return false; | |
210 } | |
14
8bf59f161b23
separete Node methods to NodeContext , NodeAttribute , NodeChildren
misaka
parents:
diff
changeset
|
211 } |