View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  /**
24   * A simple implementation of {@link NodeSet} that behaves as a collection
25   * of pointers.
26   *
27   * @author Dmitri Plotnikov
28   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
29   */
30  public class BasicNodeSet implements NodeSet {
31      private List pointers = new ArrayList();
32      private List readOnlyPointers;
33      private List nodes;
34      private List values;
35  
36      /**
37       * Add a pointer to this NodeSet.
38       * @param pointer to add
39       */
40      public void add(Pointer pointer) {
41          if (pointers.add(pointer)) {
42              clearCacheLists();
43          }
44      }
45  
46      /**
47       * Add the specified NodeSet to this NodeSet.
48       * @param nodeSet to add
49       */
50      public void add(NodeSet nodeSet) {
51          if (pointers.addAll(nodeSet.getPointers())) {
52              clearCacheLists();
53          }
54      }
55  
56      /**
57       * Remove a pointer from this NodeSet.
58       * @param pointer to remove
59       */
60      public void remove(Pointer pointer) {
61          if (pointers.remove(pointer)) {
62              clearCacheLists();
63          }
64      }
65  
66      public synchronized List getPointers() {
67          if (readOnlyPointers == null) {
68              readOnlyPointers = Collections.unmodifiableList(pointers);
69          }
70          return readOnlyPointers;
71      }
72  
73      public synchronized List getNodes() {
74          if (nodes == null) {
75              nodes = new ArrayList();
76              for (int i = 0; i < pointers.size(); i++) {
77                  Pointer pointer = (Pointer) pointers.get(i);
78                  nodes.add(pointer.getNode());
79              }
80              nodes = Collections.unmodifiableList(nodes);
81          }
82          return nodes;
83      }
84  
85      public synchronized List getValues() {
86          if (values == null) {
87              values = new ArrayList();
88              for (int i = 0; i < pointers.size(); i++) {
89                  Pointer pointer = (Pointer) pointers.get(i);
90                  values.add(pointer.getValue());
91              }
92              values = Collections.unmodifiableList(values);
93          }
94          return values;
95      }
96  
97      public String toString() {
98          return pointers.toString();
99      }
100 
101     /**
102      * Clear cache list members.
103      */
104     private synchronized void clearCacheLists() {
105         readOnlyPointers = null;
106         nodes = null;
107         values = null;
108     }
109 
110 }