comparison src/treecms/tree/util/NodeChildrenImpl.java @ 14:8bf59f161b23

separete Node methods to NodeContext , NodeAttribute , NodeChildren
author misaka
date Tue, 17 May 2011 18:44:14 +0900
parents
children 22cd920986c5
comparison
equal deleted inserted replaced
13:c8601b0fa8a3 14:8bf59f161b23
1 package treecms.tree.util;
2
3 import java.nio.ByteBuffer;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.List;
10 import treecms.api.Node;
11 import treecms.api.NodeAttributes;
12 import treecms.api.NodeChildren;
13
14 /**
15 * 子供ノードを格納するため専用のリストです.java.util.List<T>は継承しておりません.
16 * 子供ノードのリストには リスト内に同じUUIDを持つNodeが存在 してはいけません.
17 * @author shoshi
18 */
19 public class NodeChildrenImpl<T extends Node<T>> implements NodeAttributes , NodeChildren<T>
20 {
21 private List<T> m_list;
22 private Set<String> m_set;
23
24 public NodeChildrenImpl()
25 {
26 m_list = new ArrayList<T>();
27 m_set = new HashSet<String>();
28 }
29
30 public NodeChildrenImpl(NodeChildrenImpl<T> _list)
31 {
32 this();
33
34 if(_list != null){
35 addAll(_list);
36 }
37 }
38
39 public List<T> getList()
40 {
41 return Collections.unmodifiableList(m_list);
42 }
43
44 public Set<String> getUUIDSet()
45 {
46 return Collections.unmodifiableSet(m_set);
47 }
48
49 @Override
50 public T replace(T _newChild)
51 {
52 String uuid = _newChild.getID().getUUID();
53 int size = m_list.size();
54 for(int i = 0;i < size;i ++){
55 T n = m_list.get(i);
56 if(uuid.equals(n.getID().getUUID())){
57 //_newChildと同一なUUIDを見つけた
58 return m_list.set(i,_newChild);
59 }
60 }
61 //見つからなかった
62 return null;
63 }
64
65 /**
66 * このリストに新しく子供を追加します.
67 * @param _n
68 * @return 追加に成功した場合true
69 */
70 public boolean add(T _n)
71 {
72 if(m_set.contains(_n.getID().getUUID())){
73 return false;
74 }
75
76 m_set.add(_n.getID().getUUID());
77 m_list.add(_n);
78 return true;
79 }
80
81 /**
82 * _listに含まれている子供がすべてこのリストに含まれない場合にのみ、要素をすべて追加します。
83 * @param _list
84 * @return 追加に成功した場合true
85 */
86 public boolean addAll(NodeChildren<T> _list)
87 {
88 if(Collections.disjoint(m_set,_list.getUUIDSet())){
89 //共通要素がない
90 m_set.addAll(_list.getUUIDSet());
91 m_list.addAll(_list.getList());
92 }
93 return false;
94 }
95
96 /**
97 * 指定されたNodeID
98 * @param _id
99 * @return 子供ノード
100 */
101 public T get(String _uuid)
102 {
103 for(T n : m_list){
104 String uuid = n.getID().getUUID();
105 if(uuid.equals(_uuid)){
106 return n;
107 }
108 }
109
110 return null;
111 }
112
113 /**
114 * 指定された_indexの場所に位置する子供を削除します
115 * @param _index
116 * @return 消される子供ノード
117 */
118 public T get(int _index)
119 {
120 return m_list.get(_index);
121 }
122
123 /**
124 * 指定されたUUIDを持つ子どもを削除します
125 * @param _id
126 * @return 削除される子供ノード
127 */
128 public T remove(String _uuid)
129 {
130 int size = m_list.size();
131
132 for(int i = 0;i < size;i ++){
133 String uuid = m_list.get(i).getID().getUUID();
134 if(uuid.equals(_uuid)){
135 //NodeIDのUUIDが一致した
136 return m_list.remove(i);
137 }
138 }
139
140 return null;
141 }
142
143 /**
144 * 指定された場所の子供ノードを削除します
145 * @param _index
146 * @return 削除された子供ノード
147 */
148 public T remove(int _index)
149 {
150 return m_list.remove(_index);
151 }
152
153 /**
154 * このリストに指定されたUUIDを持つNodeがあるか確認します
155 * @param _id
156 * @return 存在する場合true
157 */
158 public boolean contains(String _uuid)
159 {
160 return m_set.contains(_uuid);
161 }
162
163 /**
164 * 指定された二つのUUID(_uuid1,_uuid2)がリスト上に存在する場合、その二つの順番を入れ替えます
165 * @param _uuid1 String
166 * @param _uuid2 String
167 * @return 成功した場合はtrue,NodeIDが見つからなかった場合はfalse
168 */
169 public boolean swap(String _uuid1,String _uuid2)
170 {
171 /*
172 * 二つのNodeIDの位置を求める
173 */
174 int index1 = -1;
175 int index2 = -1;
176
177 int size = m_list.size();
178 for(int i = 0;i < size && (index1 == -1 || index2 == 1);i ++){
179 String uuid = m_list.get(i).getID().getUUID();
180 if(uuid.equals(_uuid1)){
181 index1 = i;
182 continue;
183 }
184
185 if(uuid.equals(_uuid2)){
186 index2 = i;
187 continue;
188 }
189 }
190
191 /*
192 * Collection.swapを使って入れ替える
193 */
194 if(index1 != -1 && index2 != -1){
195 Collections.swap(m_list,index1,index2);
196 return true;
197 }
198
199 //NodeIDがリスト上になかった
200 return false;
201 }
202
203 /**
204 * 子供ノードのリストをクリアします
205 */
206 public void clearChildren()
207 {
208 m_set.clear();
209 m_list.clear();
210 }
211
212 @Override
213 public boolean equals(Object _o)
214 {
215 NodeChildrenImpl<T> list = (NodeChildrenImpl<T>)_o;
216 return m_list.equals(list.m_list);
217 }
218
219 @Override
220 public int hashCode()
221 {
222 int result = 17;
223 result = 37*result + m_list.hashCode();
224 result = 37*result + m_set.hashCode();
225 return result;
226 }
227
228 @Override
229 public Map<ByteBuffer, ByteBuffer> asMap() {
230 // TODO Auto-generated method stub
231 return null;
232 }
233
234 @Override
235 public Set<ByteBuffer> getKeySet() {
236 // TODO Auto-generated method stub
237 return null;
238 }
239
240 @Override
241 public void put(ByteBuffer _name, ByteBuffer _value) {
242 // TODO Auto-generated method stub
243
244 }
245
246 @Override
247 public void putAll(NodeAttributes _attrs) {
248 // TODO Auto-generated method stub
249
250 }
251
252 @Override
253 public ByteBuffer get(ByteBuffer _name) {
254 // TODO Auto-generated method stub
255 return null;
256 }
257
258 @Override
259 public NodeAttributes getAll() {
260 // TODO Auto-generated method stub
261 return null;
262 }
263
264 @Override
265 public void remove(ByteBuffer _name) {
266 // TODO Auto-generated method stub
267
268 }
269
270 @Override
271 public void removeAll(Set<ByteBuffer> _keySet) {
272 // TODO Auto-generated method stub
273
274 }
275
276 @Override
277 public void clearAttributes() {
278 // TODO Auto-generated method stub
279
280 }
281
282 }