Skip to content

Commit

Permalink
First major move with Generics.
Browse files Browse the repository at this point in the history
Filter get Generified, and all the core JDOM classes get fixed as the
errors/warnings come up.

org.jdom is warning-free in eclipse.
  • Loading branch information
rolfl committed Sep 25, 2011
1 parent 9188e72 commit 3217942
Show file tree
Hide file tree
Showing 23 changed files with 1,816 additions and 1,727 deletions.
5 changes: 5 additions & 0 deletions core/src/java/org/jdom2/Comment.java
Expand Up @@ -118,6 +118,11 @@ public Comment setText(String text) {
this.text = text;
return this;
}

@Override
public Comment clone() {
return (Comment)super.clone();
}

/**
* This returns a <code>String</code> representation of the
Expand Down
4 changes: 3 additions & 1 deletion core/src/java/org/jdom2/Content.java
Expand Up @@ -151,11 +151,13 @@ public Document getDocument() {
/**
* Returns a deep, unattached copy of this child and its descendants
* detached from any parent or document.
* <p>
* Use co-variant return type to give back a Content.
*
* @return a detached deep copy of this child and descendants
*/
@Override
public Object clone() {
public Content clone() {
try {
Content c = (Content)super.clone();
c.parent = null;
Expand Down
79 changes: 39 additions & 40 deletions core/src/java/org/jdom2/ContentList.java
Expand Up @@ -363,8 +363,8 @@ public Content get(int index) {
* @param filter <code>Filter</code> for this view.
* @return a list representing the rules of the <code>Filter</code>.
*/
List<Content> getView(Filter filter) {
return new FilterList(filter);
<E extends Content> List<E> getView(Filter<E> filter) {
return new FilterList<E>(filter);
}

/**
Expand Down Expand Up @@ -510,10 +510,10 @@ private int getModCount() {
* for <code>Document</code>s or <code>Element</code>s.
*/

class FilterList extends AbstractList<Content> implements java.io.Serializable {
class FilterList<F extends Content> extends AbstractList<F> implements java.io.Serializable {

/** The Filter */
Filter filter;
Filter<F> filter;

/** Current number of items in this view */
int count = 0;
Expand All @@ -531,7 +531,7 @@ class FilterList extends AbstractList<Content> implements java.io.Serializable {
/**
* Create a new instance of the FilterList with the specified Filter.
*/
FilterList(Filter filter) {
FilterList(Filter<F> filter) {
this.filter = filter;
}

Expand Down Expand Up @@ -564,24 +564,24 @@ else throw new ClassCastException("Filter won't allow the " +
* @return The Object which was returned.
*/
@Override
public Content get(int index) {
public F get(int index) {
int adjusted = getAdjustedIndex(index);
return ContentList.this.get(adjusted);
return filter.filter(ContentList.this.get(adjusted));
}

@Override
public Iterator<Content> iterator() {
return new FilterListIterator(filter, 0);
public Iterator<F> iterator() {
return new FilterListIterator<F>(filter, 0);
}

@Override
public ListIterator<Content> listIterator() {
return new FilterListIterator(filter, 0);
public ListIterator<F> listIterator() {
return new FilterListIterator<F>(filter, 0);
}

@Override
public ListIterator<Content> listIterator(int index) {
return new FilterListIterator(filter, index);
public ListIterator<F> listIterator(int index) {
return new FilterListIterator<F>(filter, index);
}

/**
Expand All @@ -591,21 +591,20 @@ public ListIterator<Content> listIterator(int index) {
* @return The Object which was removed.
*/
@Override
public Content remove(int index) {
public F remove(int index) {
int adjusted = getAdjustedIndex(index);
Content old = ContentList.this.get(adjusted);
if (filter.matches(old)) {
old = ContentList.this.remove(adjusted);
Content oldc = ContentList.this.get(adjusted);
F old = filter.filter(oldc);
if (old != null) {
ContentList.this.remove(adjusted);
expected++;
count--;
return old;
}
else {
throw new IllegalAddException("Filter won't allow the " +
(old.getClass()).getName() +
" '" + old + "' (index " + index +
") to be removed");
}
return old;
throw new IllegalAddException("Filter won't allow the " +
(oldc.getClass()).getName() +
" '" + oldc + "' (index " + index +
") to be removed");
}

/**
Expand All @@ -618,18 +617,18 @@ public Content remove(int index) {
* throws IndexOutOfBoundsException if index < 0 || index >= size()
*/
@Override
public Content set(int index, Content obj) {
Content old = null;
public F set(int index, F obj) {
F old = null;
if (filter.matches(obj)) {
int adjusted = getAdjustedIndex(index);
old = ContentList.this.get(adjusted);
if (!filter.matches(old)) {
Content oldc = filter.filter(ContentList.this.get(adjusted));
if (!filter.matches(oldc)) {
throw new IllegalAddException("Filter won't allow the " +
(old.getClass()).getName() +
" '" + old + "' (index " + index +
(oldc.getClass()).getName() +
" '" + oldc + "' (index " + index +
") to be removed");
}
old = ContentList.this.set(adjusted, obj);
old = filter.filter(ContentList.this.set(adjusted, obj));
expected += 2;
}
else {
Expand Down Expand Up @@ -660,7 +659,7 @@ public int size() {

count = 0;
for (int i = 0; i < ContentList.this.size(); i++) {
Object obj = ContentList.this.elementData[i];
Content obj = ContentList.this.elementData[i];
if (filter.matches(obj)) {
count++;
}
Expand All @@ -678,7 +677,7 @@ public int size() {
final private int getAdjustedIndex(int index) {
int adjusted = 0;
for (int i = 0; i < ContentList.this.size; i++) {
Object obj = ContentList.this.elementData[i];
Content obj = ContentList.this.elementData[i];
if (filter.matches(obj)) {
if (index == adjusted) {
return i;
Expand All @@ -698,10 +697,10 @@ final private int getAdjustedIndex(int index) {
/* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */
/* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */

class FilterListIterator implements ListIterator<Content> {
class FilterListIterator<F extends Content> implements ListIterator<F> {

/** The Filter that applies */
Filter filter;
Filter<F> filter;

/** Whether this iterator is in forward or reverse. */
private boolean forward = false;
Expand All @@ -726,7 +725,7 @@ class FilterListIterator implements ListIterator<Content> {
/**
* Default constructor
*/
FilterListIterator(Filter filter, int start) {
FilterListIterator(Filter<F> filter, int start) {
this.filter = filter;
expected = ContentList.this.getModCount();
// always start list iterators in backward mode ....
Expand Down Expand Up @@ -783,15 +782,15 @@ public boolean hasNext() {
* Returns the next element in the list.
*/
@Override
public Content next() {
public F next() {
if (!hasNext())
throw new NoSuchElementException("next() is beyond the end of the Iterator");
index = nextIndex();
cursor = tmpcursor;
forward = true;
canremove = true;
canset = true;
return ContentList.this.get(cursor);
return filter.filter(ContentList.this.get(cursor));
}

/**
Expand All @@ -807,15 +806,15 @@ public boolean hasPrevious() {
* Returns the previous element in the list.
*/
@Override
public Content previous() {
public F previous() {
if (!hasPrevious())
throw new NoSuchElementException("previous() is before the start of the Iterator");
index = previousIndex();
cursor = tmpcursor;
forward = false;
canremove = true;
canset = true;
return ContentList.this.get(cursor);
return filter.filter(ContentList.this.get(cursor));
}

/**
Expand Down
28 changes: 16 additions & 12 deletions core/src/java/org/jdom2/DescendantIterator.java
Expand Up @@ -55,6 +55,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
package org.jdom2;

import java.util.*;

import org.jdom2.Content;
import org.jdom2.Element;
import org.jdom2.Parent;
Expand All @@ -66,11 +67,11 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author Bradley S. Huffman
* @author Jason Hunter
*/
class DescendantIterator implements Iterator {
class DescendantIterator implements Iterator<Content> {

private Iterator iterator;
private Iterator nextIterator;
private List stack = new ArrayList();
private Iterator<Content> iterator;
private Iterator<Content> nextIterator;
private List<Iterator<Content>> stack = new ArrayList<Iterator<Content>>();

/**
* Iterator for the descendants of the supplied object.
Expand All @@ -89,7 +90,8 @@ class DescendantIterator implements Iterator {
*
* @return true is the iterator has more descendants
*/
public boolean hasNext() {
@Override
public boolean hasNext() {
if (iterator != null && iterator.hasNext()) return true;
if (nextIterator != null && nextIterator.hasNext()) return true;
if (stackHasAnyNext()) return true;
Expand All @@ -101,7 +103,8 @@ public boolean hasNext() {
*
* @return the next descendant
*/
public Object next() {
@Override
public Content next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Expand All @@ -125,7 +128,7 @@ public Object next() {
}
}

Content child = (Content) iterator.next();
Content child = iterator.next();
if (child instanceof Element) {
nextIterator = ((Element)child).getContent().iterator();
}
Expand All @@ -138,26 +141,27 @@ public Object next() {
* iteration and all children, siblings, and any node following the
* removed node (in document order) will be visited.
*/
public void remove() {
@Override
public void remove() {
iterator.remove();
}

private Iterator pop() {
private Iterator<Content> pop() {
int stackSize = stack.size();
if (stackSize == 0) {
throw new NoSuchElementException("empty stack");
}
return (Iterator) stack.remove(stackSize - 1);
return stack.remove(stackSize - 1);
}

private void push(Iterator itr) {
private void push(Iterator<Content> itr) {
stack.add(itr);
}

private boolean stackHasAnyNext() {
int size = stack.size();
for (int i = 0; i < size; i++) {
Iterator itr = (Iterator) stack.get(i);
Iterator<Content> itr = stack.get(i);
if (itr.hasNext()) {
return true;
}
Expand Down

0 comments on commit 3217942

Please sign in to comment.