001 package com.saelist.stx;
002
003 import java.io.*;
004 import java.util.*;
005
006 public class PairList {
007 List list = new LinkedList();
008
009 public void addPair(Pair pair) {
010 list.add(pair);
011 }
012
013 public void addPair(int index, Pair pair) {
014 list.add(index, pair);
015 }
016
017 public Pair getPair(int index) {
018 return (Pair) list.get(index);
019 }
020
021 public void remove(Pair pair) {
022 list.remove(pair);
023 }
024
025 public Iterator getTexts() {
026 return new TextIterator(list);
027 }
028
029 public Iterator getPairs() {
030 return list.iterator();
031 }
032
033 /* public Iterator getPairs(int nodeType) {
034 return new NodeTypeIterator(list.iterator(), nodeType);
035 } */
036
037 public String toString() {
038 StringBuffer buffer = new StringBuffer();
039 for(Iterator it = list.iterator(); it.hasNext(); ) {
040 buffer.append(it.next());
041 if(it.hasNext())
042 buffer.append(" ");
043 }
044 return buffer.toString();
045 }
046
047 public Pair get(int index) {
048 return (Pair) list.get(index);
049 }
050
051 public int size() {
052 return list.size();
053 }
054
055 public boolean isEmpty() {
056 return list.isEmpty();
057 }
058
059 public int hashCode() {
060 return list.hashCode();
061 }
062
063 public boolean equals(Object object) {
064 if(object == null)
065 return false;
066 if(! (object instanceof PairList))
067 return false;
068 return list.equals(((PairList) object).list);
069 }
070
071 /** Iterates through the text of the given list of @{link Pair}s.*/
072 public static class TextIterator implements Iterator {
073 Iterator it;
074
075 TextIterator(List list) {
076 it = list.iterator();
077 }
078
079 public Object next() {
080 return ((Pair) it.next()).getText();
081 }
082
083 public boolean hasNext() {
084 return it.hasNext();
085 }
086
087 public void remove() {
088 throw new UnsupportedOperationException();
089 }
090
091 }
092
093
094 /* public static class NodeTypeIterator implements Iterator {
095 private Iterator it;
096 private int nodeType;
097 private Pair front;
098
099 NodeTypeIterator(Iterator it, int nodeType) {
100 this.it = it;
101 this.nodeType = nodeType;
102 }
103
104 public Object next() {
105 Pair result;
106 if(front != null) {
107 result = front;
108 front = null;
109 return result;
110 }
111 while(true) {
112 result = (Pair) it.next(); // may throw NSEE!
113 if(result.getNodeType() == nodeType)
114 return result;
115 }
116 }
117
118 public boolean hasNext() {
119 if(front != null)
120 return true;
121 while(it.hasNext()) {
122 front = (Pair) it.next();
123 if(front.getNodeType() == nodeType)
124 return true;
125 }
126 front = null;
127 return false;
128 }
129
130 public void remove() {
131 throw new UnsupportedOperationException();
132 }
133
134 } */
135
136
137 }