📄 abstractsequence.java
字号:
package com.reddragon2046.base.utilities.data;
import com.reddragon2046.base.utilities.data.algorithms.Comparing;
import com.reddragon2046.base.utilities.data.algorithms.Printing;
import java.io.PrintStream;
import java.util.*;
// Referenced classes of package com.reddragon2046.base.utilities.data:
// ForwardIterator, Sequence, Algos, AbstractContainer
public abstract class AbstractSequence extends AbstractList
implements Sequence
{
public AbstractSequence()
{
}
public abstract Object clone();
public boolean equals(Object object)
{
if(object == null)
return false;
if(object == this)
return true;
if(object.getClass() != getClass())
return false;
else
return Comparing.equal(this, (Collection)object);
}
public int hashCode()
{
return 157;
}
public Object back()
{
if(size() == 0)
throw new NoSuchElementException("Sequence is empty");
else
return get(size() - 1);
}
public Object front()
{
if(size() == 0)
throw new NoSuchElementException("Sequence is empty");
else
return get(0);
}
public int count(Object object)
{
return count(0, size(), object);
}
public int count(int first, int last, Object object)
{
if(last < first)
return 0;
if(first < 0 || last >= size())
{
throw new IndexOutOfBoundsException("valid range is 0.." + (size() - 1));
} else
{
ForwardIterator begin = start();
begin.advance(first);
ForwardIterator end = (ForwardIterator)begin.clone();
end.advance((last - first) + 1);
return Algos.count(begin, end, object);
}
}
public int indexOf(Object object, int first, int last)
{
if(last < first)
return 0;
if(first < 0 || last >= size())
throw new IndexOutOfBoundsException("valid range is 0.." + (size() - 1));
ForwardIterator begin = start();
begin.advance(first);
for(; first <= last; first++)
{
Object current = begin.next();
if(object != null ? object.equals(current) : current == null)
return first;
}
return -1;
}
public void pushBack(Object object)
{
add(size(), object);
}
public Object popBack()
{
if(size() == 0)
throw new NoSuchElementException("Sequence is empty");
else
return remove(size() - 1);
}
public void pushFront(Object object)
{
add(0, object);
}
public Object popFront()
{
if(size() == 0)
throw new NoSuchElementException("Sequence is empty");
else
return remove(0);
}
public void put(int index, Object object)
{
set(index, object);
}
public boolean remove(Object object)
{
return remove(object, 1) > 0;
}
public int remove(Object object, int maximum)
{
return remove(0, size() - 1, object, maximum);
}
public int removeRange(int first, int last, Object object)
{
return remove(first, last, object, size());
}
public int replace(Object oldValue, Object newValue)
{
return Algos.replace(start(), finish(), oldValue, newValue);
}
public int replace(int first, int last, Object oldValue, Object newValue)
{
if(last < first)
return 0;
if(first < 0 || last >= size())
{
throw new IndexOutOfBoundsException("valid range is 0.." + (size() - 1));
} else
{
ForwardIterator begin = start();
begin.advance(first);
ForwardIterator end = (ForwardIterator)begin.clone();
end.advance((last - first) + 1);
return Algos.replace(begin, end, oldValue, newValue);
}
}
public int maxSize()
{
return 0x7fffffff;
}
public Iterator iterator()
{
return start();
}
public String toString()
{
return Algos.toString(this, "Sequence");
}
protected int remove(int first, int last, Object object, int maximum)
{
System.err.println("\nremoving from " + first + " through " + last + " matching " + object + " up to max of " + maximum);
Printing.println(this);
if(maximum <= 0 || last < first)
return 0;
if(first < 0 || last >= size())
throw new IndexOutOfBoundsException("valid range is 0.." + (size() - 1));
int n = 0;
int visits = 0;
ListIterator iter = listIterator(first);
while(maximum > 0 && first <= last)
{
visits++;
Object current = iter.next();
System.err.print(current + ", ");
first++;
if(current != null ? current.equals(object) : object == null)
{
iter.remove();
n++;
maximum--;
}
}
Printing.println(this);
System.err.println("removed " + n + " of " + visits);
return n;
}
public abstract ForwardIterator finish();
public abstract ForwardIterator start();
static
{
AbstractContainer.internalInit();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -