1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.model;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
25
26
27
28 class PathHelper
29 {
30 protected PathHelper()
31 {
32 _path = Collections.emptyList();
33 _rowKey = null;
34 }
35
36
37
38
39
40 public final void setRowKey(String rowKey)
41 {
42 _rowKey = rowKey;
43 }
44
45
46
47
48 public final String getRowKey()
49 {
50 return _rowKey;
51 }
52
53
54
55
56 public final int getPathSize()
57 {
58 return _path.size();
59 }
60
61
62
63
64
65 public final List<String> getPath()
66 {
67 int sz = _path.size();
68 List<String> path = new ArrayList<String>(sz+1);
69 if (sz > 0)
70 {
71 for(int i=0; i<sz; i++)
72 {
73 path.add(getPath(i));
74 }
75 }
76 path.add(getRowKey());
77 return path;
78 }
79
80
81
82
83
84
85 public final String getPath(int index)
86 {
87 return _getPathElement(index).rowKey;
88 }
89
90
91
92
93
94 public final void setPath(List<String> path)
95 {
96 int sz = (path==null) ? 0 : path.size();
97
98 if (sz > 0)
99 {
100 _path = new ArrayList<PathElement>(sz);
101 int lastIndex = sz - 1;
102
103 for(int i=0; i<lastIndex; i++)
104 {
105 setRowKey(path.get(i));
106 pushPath();
107 }
108 setRowKey(path.get(lastIndex));
109 }
110 else
111 {
112 _path = Collections.emptyList();
113 setRowKey(null);
114 }
115 }
116
117
118
119
120
121
122
123 protected final Object getPathData(int index)
124 {
125 return _getPathElement(index).node;
126 }
127
128
129
130
131 protected Object getLastPathData()
132 {
133 int sz = getPathSize();
134 return (sz > 0) ? getPathData(sz - 1) : null;
135 }
136
137
138
139
140 public final void pushPath()
141 {
142 if (_rowKey == null)
143 throw new IllegalStateException(_LOG.getMessage(
144 "NULL_ROWKEY"));
145
146 Object parentData = getLastPathData();
147 Object data = pushPath(parentData, _rowKey);
148
149 List<PathElement> comparant = Collections.emptyList();
150 if (_path == comparant)
151 _path = new ArrayList<PathElement>(5);
152
153 _path.add(new PathHelper.PathElement(_rowKey, data));
154
155 _rowKey = null;
156 }
157
158
159
160
161
162
163
164
165 protected Object pushPath(Object parentData, String rowKey)
166 {
167 return null;
168 }
169
170
171
172
173
174 public final void popPath()
175 {
176 int sz = _path.size();
177 if (sz > 0)
178 {
179 PathHelper.PathElement lastPath = _path.remove(sz-1);
180 _rowKey = lastPath.rowKey;
181 }
182 else
183 throw new IllegalStateException(_LOG.getMessage(
184 "NO_PATH_ELEMENT_TO_POP"));
185 }
186
187 private PathHelper.PathElement _getPathElement(int index)
188 {
189 return _path.get(index);
190 }
191
192 private String _rowKey;
193 private List<PathElement> _path;
194
195 private static final class PathElement
196 {
197 public final Object node;
198 public final String rowKey;
199
200 public PathElement(String rowKey, Object data)
201 {
202 this.rowKey = rowKey;
203 this.node = data;
204 }
205 }
206 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
207 PathHelper.class);
208 }