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. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. /**
  21. * A simple implementation of NodeSet that behaves as a collection of pointers.
  22. * @author Dmitri Plotnikov
  23. * @version $Revision: 1.3 $ $Date: 2004/02/29 14:17:42 $
  24. */
  25. public class BasicNodeSet implements NodeSet {
  26. private List pointers = new ArrayList();
  27. private List readOnlyPointers;
  28. private List nodes;
  29. private List values;
  30. public void add(Pointer pointer) {
  31. pointers.add(pointer);
  32. readOnlyPointers = null;
  33. }
  34. public void remove(Pointer pointer) {
  35. pointers.remove(pointer);
  36. readOnlyPointers = null;
  37. }
  38. public List getPointers() {
  39. if (readOnlyPointers == null) {
  40. readOnlyPointers = Collections.unmodifiableList(pointers);
  41. }
  42. return readOnlyPointers;
  43. }
  44. public List getNodes() {
  45. if (nodes == null) {
  46. nodes = new ArrayList();
  47. for (int i = 0; i < pointers.size(); i++) {
  48. Pointer pointer = (Pointer) pointers.get(i);
  49. nodes.add(pointer.getValue());
  50. }
  51. nodes = Collections.unmodifiableList(nodes);
  52. }
  53. return nodes;
  54. }
  55. public List getValues() {
  56. if (values == null) {
  57. values = new ArrayList();
  58. for (int i = 0; i < pointers.size(); i++) {
  59. Pointer pointer = (Pointer) pointers.get(i);
  60. values.add(pointer.getValue());
  61. }
  62. values = Collections.unmodifiableList(values);
  63. }
  64. return values;
  65. }
  66. public String toString() {
  67. return pointers.toString();
  68. }
  69. }