Skip to content

Commit

Permalink
make NodeList implement List (#556)
Browse files Browse the repository at this point in the history
* make NodeList implement List
  • Loading branch information
DeepSnowNeeL authored and matozoid committed Nov 11, 2016
1 parent dc0b8c6 commit f1cce0a
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 19 deletions.
269 changes: 257 additions & 12 deletions javaparser-core/src/main/java/com/github/javaparser/ast/NodeList.java
Expand Up @@ -6,7 +6,12 @@
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Optional; import java.util.Optional;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream; import java.util.stream.Stream;


import com.github.javaparser.HasParentNode; import com.github.javaparser.HasParentNode;
Expand All @@ -19,7 +24,7 @@
* *
* @param <N> the type of nodes contained. * @param <N> the type of nodes contained.
*/ */
public class NodeList<N extends Node> implements Iterable<N>, HasParentNode<NodeList<N>>, Visitable { public class NodeList<N extends Node> implements List<N>, Iterable<N>, HasParentNode<NodeList<N>>, Visitable {
private List<N> innerList = new ArrayList<>(0); private List<N> innerList = new ArrayList<>(0);


private Node parentNode; private Node parentNode;
Expand All @@ -32,10 +37,10 @@ public NodeList(Node parent) {
setParentNode(parent); setParentNode(parent);
} }


public NodeList<N> add(N node) { @Override
public boolean add(N node) {
own(node); own(node);
innerList.add(node); return innerList.add(node);
return this;
} }


private void own(N node) { private void own(N node) {
Expand Down Expand Up @@ -79,14 +84,17 @@ public boolean contains(N node) {
return innerList.contains(node); return innerList.contains(node);
} }


@Override
public Stream<N> stream() { public Stream<N> stream() {
return innerList.stream(); return innerList.stream();
} }


@Override
public int size() { public int size() {
return innerList.size(); return innerList.size();
} }


@Override
public N get(int i) { public N get(int i) {
return innerList.get(i); return innerList.get(i);
} }
Expand All @@ -97,21 +105,26 @@ public Iterator<N> iterator() {
return innerList.iterator(); return innerList.iterator();
} }


public NodeList<N> set(int index, N element) { @Override
public N set(int index, N element) {
setAsParentNodeOf(element); setAsParentNodeOf(element);
innerList.set(index, element); return innerList.set(index, element);
return this;
} }


public NodeList<N> remove(int index) { @Override
innerList.remove(index); public N remove(int index) {
return this; N remove = innerList.remove(index);
if (remove != null)
remove.setParentNode(null);
return remove;
} }


@Override
public boolean isEmpty() { public boolean isEmpty() {
return innerList.isEmpty(); return innerList.isEmpty();
} }


@Override
public void sort(Comparator<? super N> comparator) { public void sort(Comparator<? super N> comparator) {
Collections.sort(innerList, comparator); Collections.sort(innerList, comparator);
} }
Expand All @@ -122,10 +135,10 @@ public void addAll(NodeList<N> otherList) {
} }
} }


public NodeList<N> add(int index, N node) { @Override
public void add(int index, N node) {
own(node); own(node);
innerList.add(index, node); innerList.add(index, node);
return this;
} }


@Override @Override
Expand Down Expand Up @@ -161,4 +174,236 @@ public <A> void accept(final VoidVisitor<A> v, final A arg) {
v.visit(this, arg); v.visit(this, arg);
} }


/**
* @param action
* @see java.lang.Iterable#forEach(java.util.function.Consumer)
*/
@Override
public void forEach(Consumer<? super N> action) {
innerList.forEach(action);
}

/**
* @param o
* @return
* @see java.util.List#contains(java.lang.Object)
*/
@Override
public boolean contains(Object o) {
return innerList.contains(o);
}

/**
* @return
* @see java.util.List#toArray()
*/
@Override
public Object[] toArray() {
return innerList.toArray();
}

/**
* @param a
* @return
* @see java.util.List#toArray(java.lang.Object[])
*/
@Override
public <T> T[] toArray(T[] a) {
return innerList.toArray(a);
}

/**
* @param o
* @return
* @see java.util.List#remove(java.lang.Object)
*/
@Override
public boolean remove(Object o) {
boolean remove = innerList.remove(o);
if (o != null && o instanceof Node)
((Node) o).setParentNode(null);
return remove;
}

/**
* @param c
* @return
* @see java.util.List#containsAll(java.util.Collection)
*/
@Override
public boolean containsAll(Collection<?> c) {
return innerList.containsAll(c);
}

/**
* @param c
* @return
* @see java.util.List#addAll(java.util.Collection)
*/
@Override
public boolean addAll(Collection<? extends N> c) {
for (N n : c)
own(n);
return innerList.addAll(c);
}

/**
* @param index
* @param c
* @return
* @see java.util.List#addAll(int, java.util.Collection)
*/
@Override
public boolean addAll(int index, Collection<? extends N> c) {
for (N n : c)
own(n);
return innerList.addAll(index, c);
}

/**
* @param c
* @return
* @see java.util.List#removeAll(java.util.Collection)
*/
@Override
public boolean removeAll(Collection<?> c) {
boolean removeAll = innerList.removeAll(c);
for (Object o : c) {
if (o != null && o instanceof Node)
((Node) o).setParentNode(null);
}
return removeAll;
}

/**
* @param c
* @return
* @see java.util.List#retainAll(java.util.Collection)
*/
@Override
public boolean retainAll(Collection<?> c) {
return innerList.retainAll(c);
}

/**
* @param operator
* @see java.util.List#replaceAll(java.util.function.UnaryOperator)
*/
@Override
public void replaceAll(UnaryOperator<N> operator) {
innerList.replaceAll(operator);
}

/**
* @param filter
* @return
* @see java.util.Collection#removeIf(java.util.function.Predicate)
*/
@Override
public boolean removeIf(Predicate<? super N> filter) {
innerList.stream().filter(filter).forEach(n -> {
if (n != null)
n.setParentNode(null);
});
return innerList.removeIf(filter);
}

/**
*
* @see java.util.List#clear()
*/
@Override
public void clear() {
for (Node n : innerList)
n.setParentNode(null);
innerList.clear();
}

/**
* @param o
* @return
* @see java.util.List#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
return innerList.equals(o);
}

/**
* @return
* @see java.util.List#hashCode()
*/
@Override
public int hashCode() {
return innerList.hashCode();
}

/**
* @param o
* @return
* @see java.util.List#indexOf(java.lang.Object)
*/
@Override
public int indexOf(Object o) {
return innerList.indexOf(o);
}

/**
* @param o
* @return
* @see java.util.List#lastIndexOf(java.lang.Object)
*/
@Override
public int lastIndexOf(Object o) {
return innerList.lastIndexOf(o);
}

/**
* @return
* @see java.util.List#listIterator()
*/
@Override
public ListIterator<N> listIterator() {
return innerList.listIterator();
}

/**
* @param index
* @return
* @see java.util.List#listIterator(int)
*/
@Override
public ListIterator<N> listIterator(int index) {
return innerList.listIterator(index);
}

/**
* @return
* @see java.util.Collection#parallelStream()
*/
@Override
public Stream<N> parallelStream() {
return innerList.parallelStream();
}

/**
* @param fromIndex
* @param toIndex
* @return
* @see java.util.List#subList(int, int)
*/
@Override
public List<N> subList(int fromIndex, int toIndex) {
return innerList.subList(fromIndex, toIndex);
}

/**
* @return
* @see java.util.List#spliterator()
*/
@Override
public Spliterator<N> spliterator() {
return innerList.spliterator();
}

} }
Expand Up @@ -21,11 +21,13 @@


package com.github.javaparser.utils; package com.github.javaparser.utils;


import com.github.javaparser.ast.NodeList;

import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


/** /**
* Any kind of utility. * Any kind of utility.
Expand All @@ -35,16 +37,13 @@
public class Utils { public class Utils {
public static final String EOL = System.getProperty("line.separator"); public static final String EOL = System.getProperty("line.separator");
public static <T> List<T> ensureNotNull(List<T> list) { public static <T> List<T> ensureNotNull(List<T> list) {
return list == null ? new ArrayList<T>() : list; return list == null ? new ArrayList<>() : list;
} }


public static <E> boolean isNullOrEmpty(Collection<E> collection) { public static <E> boolean isNullOrEmpty(Collection<E> collection) {
return collection == null || collection.isEmpty(); return collection == null || collection.isEmpty();
} }


public static boolean isNullOrEmpty(NodeList<?> collection) {
return collection == null || collection.isEmpty();
}


public static <T> T assertNotNull(T o) { public static <T> T assertNotNull(T o) {
if (o == null) { if (o == null) {
Expand Down

0 comments on commit f1cce0a

Please sign in to comment.