slistiterator.java
来自「java 的源代码」· Java 代码 · 共 314 行
JAVA
314 行
package com.reddragon2046.base.utilities.data;
import java.io.*;
import java.util.*;
// Referenced classes of package com.reddragon2046.base.utilities.data:
// Opaque, ForwardIterator, SList, InputIterator,
// Container
public final class SListIterator
implements ForwardIterator, Serializable
{
SListIterator(SList list, SList.SListNode node)
{
lastNode = null;
myList = list;
myNode = node;
}
public SListIterator()
{
this(null, null);
}
public SListIterator(SListIterator iterator)
{
this(iterator.myList, iterator.myNode);
}
public Object clone()
{
return new SListIterator(this);
}
public boolean equals(Object object)
{
if(myList != null)
{
if(object instanceof SListIterator)
{
SListIterator iter = (SListIterator)object;
return myNode == iter.myNode && myList == iter.myList;
}
if(object instanceof Opaque)
{
Opaque op = (Opaque)object;
return myNode == op.opaqueData() && opaqueId() == op.opaqueId();
}
}
return false;
}
public int hashCode()
{
return System.identityHashCode(myNode);
}
public boolean atBegin()
{
if(myList == null || myNode != null && !myNode.isActive())
throw new NoSuchElementException("SListIterator");
else
return myNode == myList.getHead();
}
public boolean atEnd()
{
if(myList == null || myNode != null && !myNode.isActive())
throw new NoSuchElementException("SListIterator");
else
return myNode == null;
}
public boolean hasNext()
{
if(myList == null || myNode != null && !myNode.isActive())
throw new NoSuchElementException("SListIterator at an invalid position: list null? " + (myList == null) + ", node null? " + (myNode == null) + ", node active? " + (myNode != null ? myNode.isActive() : false));
else
return myNode != null;
}
public void advance()
{
if(myList == null || myNode == null)
{
throw new NoSuchElementException("Attempt to advance SListIterator when at an invalid position.");
} else
{
myNode = myNode.getNext();
return;
}
}
public void advance(int n)
{
if(n < 0)
throw new IllegalArgumentException("Attempt to advance a ForwardIterator in the wrong direction.");
if(myList == null)
throw new NoSuchElementException("SListIterator");
while(n-- > 0)
{
if(myNode == null)
throw new NoSuchElementException("SListIterator");
myNode = myNode.getNext();
}
}
public Object next()
{
if(myList == null || myNode == null || !myNode.isActive())
{
throw new NoSuchElementException("SListIterator");
} else
{
lastNode = myNode;
myNode = myNode.getNext();
return lastNode.getObject();
}
}
public Object get()
{
if(myList == null || myNode == null || !myNode.isActive())
{
throw new NoSuchElementException("SListIterator");
} else
{
lastNode = myNode;
return lastNode.getObject();
}
}
public Object get(int offset)
{
SListIterator i = new SListIterator(this);
i.advance(offset);
lastNode = i.myNode;
return i.get();
}
public void put(Object object)
{
if(myList == null || myNode == null || !myNode.isActive())
{
throw new NoSuchElementException("SListIterator");
} else
{
myList.set(myNode, object);
return;
}
}
public void put(int offset, Object object)
{
SListIterator i = new SListIterator(this);
i.advance(offset);
i.put(object);
}
public int distance(InputIterator iterator)
{
if(!isCompatibleWith(iterator))
throw new IllegalArgumentException("iterators not compatible");
SList.SListNode hisNode = (iterator instanceof SListIterator) ? ((SListIterator)iterator).myNode : (SList.SListNode)iterator.opaqueData();
if(myNode != null && !myNode.isActive() || hisNode != null && !hisNode.isActive())
throw new NoSuchElementException("SListIterator");
else
return distance(myNode, hisNode, 0);
}
public int index()
{
if(myList == null || myNode == null || !myNode.isActive())
throw new NoSuchElementException("SListIterator");
else
return distance(myList.getHead(), myNode, 0);
}
public void add(Object object)
{
if(myList == null || !myNode.isActive())
{
throw new NoSuchElementException("SListIterator");
} else
{
myNode = myList.insert(myNode, object);
return;
}
}
public void set(Object object)
{
if(lastNode == null || !lastNode.isActive())
{
throw new NoSuchElementException("SListIterator");
} else
{
myList.set(lastNode, object);
return;
}
}
public void remove()
{
if(lastNode == null || !lastNode.isActive())
throw new IllegalStateException();
myList.removeNode(lastNode);
if(lastNode == myNode)
myNode = myNode.getNext();
lastNode = null;
}
public Container getContainer()
{
return myList;
}
public Collection getCollection()
{
return myList;
}
public boolean isCompatibleWith(InputIterator iterator)
{
return myList != null && (myNode == null || myNode.isActive()) && opaqueId() == iterator.opaqueId();
}
public Object opaqueData()
{
return myNode;
}
public int opaqueId()
{
return System.identityHashCode(myNode != null && !myNode.isActive() ? null : ((Object) (myList)));
}
private int distance(SList.SListNode from, SList.SListNode to, int check)
{
if(check > 1)
throw new ConcurrentModificationException("SList");
SList.SListNode origfrom = from;
SList.SListNode origto = to;
int n = 0;
for(; from != to; from = from.getNext())
{
if(from == null)
return -distance(origto, origfrom, check + 1);
n++;
}
return n;
}
private void writeObject(ObjectOutputStream stream)
throws IOException
{
stream.defaultWriteObject();
if(myList != null && myNode != null)
stream.writeObject(new Integer(index()));
else
stream.writeObject(null);
}
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
myNode = null;
lastNode = null;
if(myList != null)
{
Integer myNodeIndex = (Integer)stream.readObject();
if(myNodeIndex != null)
myNode = myList.nodeAt(myNodeIndex.intValue());
}
}
public void dump()
{
System.out.print("\tid==" + System.identityHashCode(myNode));
if(myNode == null)
{
System.out.print("\tnull");
} else
{
if(myNode.getNext() == null)
System.out.print("\tnext=null");
else
System.out.print("\tnext=" + System.identityHashCode(myNode.getNext()));
System.out.print("\tactive=" + myNode.isActive());
if(myNode.isActive())
System.out.print("\t" + myNode.getObject());
}
System.out.println();
}
protected SList.SListNode getNode()
{
return myNode;
}
protected SList getList()
{
return myList;
}
private SList myList;
private transient SList.SListNode myNode;
private transient SList.SListNode lastNode;
static final long serialVersionUID = 0x7c626d59c7e674b1L;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?