Added SetArtilleriesGeneric
This commit is contained in:
parent
fb4c5f3416
commit
675c220c79
70
SetArtilleriesGeneric.java
Normal file
70
SetArtilleriesGeneric.java
Normal file
@ -0,0 +1,70 @@
|
||||
public class SetArtilleriesGeneric<T> {
|
||||
private final Object[] _places;
|
||||
|
||||
public int getCount() {
|
||||
return _places.length;
|
||||
}
|
||||
|
||||
public SetArtilleriesGeneric(int count) {
|
||||
_places = new Object[count];
|
||||
}
|
||||
|
||||
public int Insert(T artillery) {
|
||||
return Insert(artillery, 0);
|
||||
}
|
||||
|
||||
public int Insert(T artillery, int position) {
|
||||
if (position < 0 || position >= getCount()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_places[position] == null) {
|
||||
_places[position] = artillery;
|
||||
return position;
|
||||
}
|
||||
|
||||
int firstNull = -1;
|
||||
|
||||
for (int i = position + 1; i < getCount(); i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
firstNull = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstNull == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
System.arraycopy(_places, position, _places, position + 1, firstNull - position);
|
||||
|
||||
_places[position] = artillery;
|
||||
return position;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T Remove(int position) {
|
||||
if (position < 0 || position >= getCount())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = _places[position];
|
||||
|
||||
_places[position] = null;
|
||||
|
||||
return (T) result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T Get(int position) {
|
||||
if (position < 0 || position >= getCount()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (T) _places[position];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user