View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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     * Sets the current rowKey.
38     * @param rowKey
39     */
40    public final void setRowKey(String rowKey)
41    {
42      _rowKey = rowKey;
43    }
44    
45    /**
46     * @return the current row key
47     */
48    public final String getRowKey()
49    {
50      return _rowKey;
51    }
52    
53    /**
54     * @return the current size of the path
55     */
56    public final int getPathSize()
57    {
58      return _path.size();
59    }
60    
61    /**
62     * Gets the current path.
63     * @return a List, with each element a rowKey String
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     * Gets a rowKey at the specified location in the path.
82     * @param index
83     * @return the rowKey String
84     */
85    public final String getPath(int index)
86    {
87      return _getPathElement(index).rowKey;
88    }
89    
90    /**
91     * Sets the path.
92     * @param path a List with each element a rowKey String
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    * Gets the path data at the specified index.
119    * @param index
120    * @return this is the path data that was set using 
121    * {@link #pushPath(Object,String)}
122    */
123   protected final Object getPathData(int index)
124   {
125     return _getPathElement(index).node;
126   }
127 
128   /**
129    * @return the path data for the last element in the path.
130    */
131   protected Object getLastPathData()
132   {
133     int sz = getPathSize();
134     return (sz > 0) ? getPathData(sz - 1) : null;
135   }
136   
137   /**
138    * Pushes the current rowKey onto the path.
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    * Gets the path data to use for a new path segment.
160    * @param parentData the path data of the previous path segment
161    * @param rowKey the path segment that is just about to be added.
162    * @return new path data, which can be retrieved using
163    * {@link #getPathData(int)}
164    */
165   protected Object pushPath(Object parentData, String rowKey)
166   {
167     return null;
168   }
169   
170   /**
171    * Removes the last rowKey from the end of this path.
172    * The current rowKey is set to be this last rowKey.
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 }