1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
27
28
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
38
39
40 public void add(Pointer pointer) {
41 if (pointers.add(pointer)) {
42 clearCacheLists();
43 }
44 }
45
46
47
48
49
50 public void add(NodeSet nodeSet) {
51 if (pointers.addAll(nodeSet.getPointers())) {
52 clearCacheLists();
53 }
54 }
55
56
57
58
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
103
104 private synchronized void clearCacheLists() {
105 readOnlyPointers = null;
106 nodes = null;
107 values = null;
108 }
109
110 }