ool.astcreator.astcreator.1.6.8.source-code.NodeList Maven / Gradle / Ivy
//COPYRIGHT
package %generated.node%;
import java.util.*;
/** A list of AST nodes where all operations preserve the
* single-parent property of the AST.
* A node list is always a child list of some parent node.
* When a node is added to the list (through the collection constructor,
* the add
, addFirst
, addLast
,
* addAll
or set
methods of the list or
* the add
or set
methods of the iterator),
* it is removed from its original parent (if it has one) and its parent
* is set to the node containing the node list.
* When a node is removed from the list (through the remove
,
* removeFirst
, removeLast
, clear
or
* set
methods of the list or the remove
or
* set
methods of the iterator), its parent is set to
* null
.
* Beware that if the add
or set
method of the
* iterator is called with a node which is already in the list (except for a
* set
call replacing a node by itself), the iterator
* will be invalidated, so any subsequent iterator operation will throw a
* ConcurrentModificationException
.
*
*/
@SuppressWarnings("all")
public class %NodeList% extends LinkedList {
%INode% parent;
protected void setParent(%INode% n) {
%INode% p = n.parent();
if (p != null) {
p.removeChild(n);
}
n.parent(parent);
}
private %NodeList%() {
super();
this.parent = null;
}
public %NodeList%(%INode% parent) {
super();
this.parent = parent;
}
public %NodeList%(%INode% parent, Collection extends E> c) {
this(parent);
addAll(c);
}
public @Override boolean add(E o) {
setParent(o);
return super.add(o);
}
public @Override void addFirst(E o) {
setParent(o);
super.addFirst(o);
}
public @Override void addLast(E o) {
setParent(o);
super.addLast(o);
}
public @Override boolean remove(Object o) {
if (super.remove(o)) {
if(((%INode%) o).parent()==parent)
{
((%INode%)o).parent(null);
return true;
}
}
return false;
}
public @Override E removeFirst() {
E o = super.removeFirst();
if(o.parent()==parent)
{
o.parent(null);
}
return o;
}
public @Override E removeLast() {
E o = super.removeLast();
if(o.parent()==parent)
{
o.parent(null);
}
return o;
}
public @Override void clear() {
for (E o : this) {
if(o.parent()==parent)
{
o.parent(null);
}
}
super.clear();
}
@SuppressWarnings("unchecked")
public @Override Object clone() {
LinkedList clone = new LinkedList();
for (E n : this)
{
clone.add((E) n.clone());
}
return clone;
}
public @Override E remove(int index) {
E old = super.remove(index);
if(old.parent()==parent)
{
old.parent(null);
}
return old;
}
// We assume the the one-arg addAll method of LinkedList
// calls the two-arg version
public @Override boolean addAll(int index, Collection extends E> c) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size());
}
if (c == this) {
return false;
}
// Adjust index if some of the nodes were already in the list
// before the insertion position
int i = 0;
for (E elem : this) {
if (i >= index) break;
if (c.contains(elem)) index--;
i++;
}
ArrayList copy = new ArrayList(c);
for (E o : copy) {
setParent(o);
}
return super.addAll(index, copy);
}
public @Override void add(int index, E o) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size());
}
// Adjust index if the node was already in the list
// before the insertion position
int i = 0;
for (E elem : this) {
if (i >= index) break;
if (elem == o) index--;
i++;
}
setParent(o);
super.add(index, o);
}
public @Override E set(int index, E o) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size());
}
// Adjust index if the node was already in the list
// before the insertion position
int i = 0;
for (E elem : this) {
if (i == index && elem == o) return o;
if (i >= index) break;
if (elem == o) index--;
i++;
}
setParent(o);
E old = super.set(index, o);
if(old.parent()==parent)
{
old.parent(null);
}
return old;
}
public @Override ListIterator listIterator(int index) {
return new NodeListIterator(super.listIterator(index));
}
private class NodeListIterator implements ListIterator {
ListIterator iterator;
E last_returned;
// boolean previous;
NodeListIterator(ListIterator iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public E next() {
// previous = false;
return last_returned = iterator.next();
}
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public E previous() {
// previous = true;
return last_returned = iterator.previous();
}
public int nextIndex() {
return iterator.nextIndex();
}
public int previousIndex() {
return iterator.previousIndex();
}
public void remove() {
iterator.remove();
if(last_returned.parent()==parent)
{
last_returned.parent(null);
}
}
public void set(E o) {
// This works but invalidates the iterator if the node was in
// the list already.
iterator.set(o);
if (o != last_returned) {
setParent(o);
if(last_returned.parent()==parent)
{
last_returned.parent(null);
}
last_returned = o;
}
}
public void add(E o) {
// This works but invalidates the iterator if the node was in
// the list already.
iterator.add(o);
setParent(o);
}
}
}