View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jxpath;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  /***
23   * A simple implementation of NodeSet that behaves as a collection of pointers. 
24   * @author Dmitri Plotnikov
25   * @version $Revision: 1.3 $ $Date: 2004/02/29 14:17:42 $
26   */
27  public class BasicNodeSet implements NodeSet {
28      private List pointers = new ArrayList();
29      private List readOnlyPointers;
30      private List nodes;
31      private List values;
32  
33      public void add(Pointer pointer) {
34          pointers.add(pointer);
35          readOnlyPointers = null;
36      }
37      
38      public void remove(Pointer pointer) {
39          pointers.remove(pointer);
40          readOnlyPointers = null;
41      }
42      
43      public List getPointers() {
44          if (readOnlyPointers == null) {
45              readOnlyPointers = Collections.unmodifiableList(pointers);
46          }
47          return readOnlyPointers;
48      }
49  
50      public List getNodes() {
51          if (nodes == null) {
52              nodes = new ArrayList();
53              for (int i = 0; i < pointers.size(); i++) {
54                  Pointer pointer = (Pointer) pointers.get(i);
55                  nodes.add(pointer.getValue());
56              }
57              nodes = Collections.unmodifiableList(nodes);
58          }
59          return nodes;
60      }
61  
62      public List getValues() {
63          if (values == null) {
64              values = new ArrayList();
65              for (int i = 0; i < pointers.size(); i++) {
66                  Pointer pointer = (Pointer) pointers.get(i);
67                  values.add(pointer.getValue());
68              }
69              values = Collections.unmodifiableList(values);
70          }
71          return values;
72      }
73      
74      public String toString() {
75          return pointers.toString();
76      }
77  }