summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/List.java
blob: 4ee00eaa4d031968db2eda54896ed1660b9f801f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * List.java 1.0
 *
 * Jun 11, 1997 by mich
 * Aug 20, 1997 by mich
 * Sep 28, 1997 by mich
 *
 * @version 1.0 last updated: Sep 28, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import java.util.Enumeration;
import java.util.Vector;

import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;
import io.devnulllabs.openjava.tools.DebugOut;

/**
 * The List class presents for the list of parse trees.
 *
 */
public abstract class List extends ParseTreeObject implements ParseTree {

    protected final void replaceChildWith(
        ParseTree dist,
        ParseTree replacement)
        throws ParseTreeException {
        DebugOut.println(
            "List.replaceChildWith() " + dist + " with " + replacement);
        for (int i = 0, size = contents.size(); i < size; ++i) {
            if (contents_elementAt(i) == dist) {
                contents_setElementAt(replacement, i);
                return;
            }
        }
        throw new ParseTreeException("no replacing target");
    }

    /** The ptreelist  the list of parse-tree nodes */
    private Vector contents = new Vector();
    protected void contents_addElement(Object obj) {
        contents.addElement(obj);
        if (obj instanceof ParseTreeObject) {
            ((ParseTreeObject) obj).setParent(this);
        }
    }
    protected void contents_insertElementAt(Object obj, int index) {
        contents.insertElementAt(obj, index);
        if (obj instanceof ParseTreeObject) {
            ((ParseTreeObject) obj).setParent(this);
        }
    }
    protected void contents_setElementAt(Object obj, int index) {
        contents.setElementAt(obj, index);
        if (obj instanceof ParseTreeObject) {
            ((ParseTreeObject) obj).setParent(this);
        }
    }
    protected Object contents_elementAt(int index) {
        return contents.elementAt(index);
    }
    protected void contents_removeElementAt(int index) {
        contents.removeElementAt(index);
    }
    protected int contents_size() {
        return contents.size();
    }

    private String delimiter = ParseTreeObject.LN;

    /**
     * Allocates this List
     *
     */
    protected List() {
        contents = new Vector();
    }

    /**
     * Allocates this List
     *
     * @param p0  list's element
     */
    protected List(Object p) {
        this();
        contents_addElement(p);
    }

    /**
     * Allocates this List
     *
     */
    protected List(String delimiter) {
        this();
        this.delimiter = delimiter;
    }

    /**
     * Allocates this List
     *
     * @param p0  list's element
     */
    protected List(String delimiter, Object p) {
        this(delimiter);
        contents_addElement(p);
    }

    /**
     * Get contents
     */
    public Enumeration elements() {
        return contents.elements();
    }

    /**
     * Returns the length of this list.
     *
     * @return  the length of this list
     */
    public int size() {
        return contents.size();
    }

    /**
     * Tests if this list is empty.
     *
     * @return  true if this list is empty
     */
    public boolean isEmpty() {
        return contents.isEmpty();
    }

    /**
     * Removes an element at the specified point from this list
     *
     * @param  n  where to remove element.
     */
    public void removeAll() {
        contents.removeAllElements();
    }

    /**
     * Tests if any element representing the specified string is exist
     * or not.
     *
     * @param  str  a string to test.
     * @return  true if any element representing the specified string
     *        is exist.
     */
    public boolean contains(String str) {
        Enumeration it = contents.elements();
        while (it.hasMoreElements()) {
            Object elem = it.nextElement();
            if (elem != null && elem.toString().equals(str))
                return true;
        }
        return false;
    }

    /**
     * Tests if this list-node's value equals to the specified
     * ptree-node's.
     *
     * @return  true if two values are same.
     */
    public boolean equals(ParseTree p) {
        if (p == null)
            return false;
        if (this == p)
            return true;
        if (this.getClass() != p.getClass())
            return false;

        List nlp = (List) p;
        int length = this.size();
        if (nlp.size() != length)
            return false;
        for (int i = 0; i < length; ++i) {
            Object a = contents.elementAt(i);
            Object b = nlp.contents.elementAt(i);
            if (a != null && b == null)
                return false;
            if (a == null && b != null)
                return false;
            if (a == null && b == null)
                continue;
            if (!a.equals(b))
                return false;
        }
        return true;
    }

    /**
     * Makes a new copy (another object) of this list-node.
     * The objects contained by this object will also be copied
     *
     * @return  the copy of this nonleaf-node as a ptree-node.
     */
    public ParseTree makeRecursiveCopy() {
        List result = (List) clone();
        result.contents = new Vector();

        Enumeration it = contents.elements();
        while (it.hasMoreElements()) {
            Object elem = it.nextElement();
            if (elem instanceof ParseTree) {
                elem = ((ParseTree) elem).makeRecursiveCopy();
            }
            result.contents_addElement(elem);
        }

        return result;
    }

    /**
     * Accepts a <code>ParseTreeVisitor</code> object as the role of a
     * Visitor in the Visitor pattern, as the role of an Element in the
     * Visitor pattern.<p>
     *
     * This invoke an appropriate <code>visit()</code> method on each
     * child <code>ParseTree</code> object with this visitor.
     *
     * @param visitor a visitor
     **/
    public void childrenAccept(ParseTreeVisitor visitor)
        throws ParseTreeException {
        if (contents == null)
            return;
        int length = contents.size();
        for (int i = 0; i < length; ++i) {
            Object obj = contents.elementAt(i);
            if (obj instanceof ParseTree) {
                ParseTree ptree = (ParseTree) obj;
                ptree.accept(visitor);
            }
        }
    }

}