112 lines
2.8 KiB
Java
112 lines
2.8 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.NoSuchElementException;
|
|
|
|
public class SetGeneric<T extends Object>
|
|
{
|
|
private ArrayList<T> _places;
|
|
|
|
public int Count() {return _places.size();};
|
|
public int _maxCount;
|
|
|
|
public SetGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new ArrayList<T>(count);
|
|
}
|
|
|
|
public int Insert(T ship)
|
|
{
|
|
if(_places.size() >= _maxCount)
|
|
{
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
_places.add(0, ship);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public boolean Insert(T ship, int position)
|
|
{
|
|
|
|
if(_places.size() >= _maxCount)
|
|
{
|
|
return false;
|
|
}
|
|
if(position < 0 || position > _places.size())
|
|
{
|
|
return false;
|
|
}
|
|
if(position == _places.size())
|
|
{
|
|
_places.add(ship);
|
|
}
|
|
else
|
|
{
|
|
_places.add(position, ship);
|
|
}
|
|
return true;
|
|
|
|
}
|
|
|
|
public boolean Remove(int position)
|
|
{
|
|
if(position < _places.size() && _places.size() < _maxCount)
|
|
{
|
|
_places.remove(position);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public T Get(int position)
|
|
{
|
|
if(position < _places.size() && position >= 0)
|
|
{
|
|
return _places.get(position);
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
public Iterable<T> GetShips(final Integer maxShips) {
|
|
return new Iterable<T>() {
|
|
@Override
|
|
public Iterator<T> iterator() {
|
|
return new Iterator<T>() {
|
|
private int currentIndex = 0;
|
|
private int count = 0;
|
|
|
|
@Override
|
|
public boolean hasNext() {
|
|
return currentIndex < _places.size() && (maxShips == null || count < maxShips);
|
|
}
|
|
|
|
@Override
|
|
public T next() {
|
|
if (hasNext()) {
|
|
count++;
|
|
return _places.get(currentIndex++);
|
|
}
|
|
throw new NoSuchElementException();
|
|
}
|
|
|
|
@Override
|
|
public void remove() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|