Files
Mekanism-Community-Edition/src/main/java/mekanism/common/HashList.java
T

124 lines
1.7 KiB
Java
Raw Normal View History

2013-10-26 13:25:51 -04:00
package mekanism.common;
import java.util.ArrayList;
import java.util.Iterator;
public class HashList<T> implements Iterable<T>
{
2013-11-02 12:00:29 -04:00
private ArrayList<T> list = new ArrayList<T>(256);
2013-10-26 13:25:51 -04:00
public boolean contains(T obj)
{
return list.contains(obj);
}
2013-10-26 13:25:51 -04:00
public void clear()
{
list.clear();
}
2013-10-26 13:25:51 -04:00
public T get(int index)
{
if(index > size()-1)
{
return null;
}
2013-10-26 13:25:51 -04:00
return list.get(index);
}
2013-10-26 13:25:51 -04:00
public void add(T obj)
{
if(!list.contains(obj))
{
list.add(obj);
}
}
2013-10-26 13:25:51 -04:00
public void add(int index, T obj)
{
if(!list.contains(obj))
{
if(index > size())
{
for(int i = size(); i <= index-1; i++)
{
list.add(i, null);
}
}
2013-10-26 13:25:51 -04:00
list.add(index, obj);
}
}
2013-10-29 18:54:36 -04:00
public boolean isEmpty()
2013-10-27 17:47:17 -04:00
{
2013-10-29 18:54:36 -04:00
return list.isEmpty();
2013-10-27 17:47:17 -04:00
}
2013-10-29 18:54:36 -04:00
public void remove(int index)
{
2013-10-29 18:54:36 -04:00
if(isEmpty() || index > size()-1)
{
2013-10-29 18:54:36 -04:00
return;
}
2013-10-29 18:54:36 -04:00
list.remove(index);
}
2013-10-29 18:54:36 -04:00
public void replace(int index, T obj)
{
if(get(index) != null)
{
remove(index);
}
add(index, obj);
}
2013-10-27 17:47:17 -04:00
public void remove(T obj)
{
list.remove(obj);
}
2013-10-26 13:25:51 -04:00
public int indexOf(T obj)
{
return list.indexOf(obj);
}
2013-10-26 13:25:51 -04:00
public int size()
{
return list.size();
}
2015-04-05 23:03:56 +10:00
public void swap( int source, int target )
{
// Make sure both source and target ar legal values
if( source == target ) return;
if( source < 0 || target < 0 ) return;
if( source >= list.size() || target >= list.size() ) return;
// Perform swap
T temp = list.get( source );
list.set( source, list.get( target ) );
list.set( target, temp );
}
@Override
public int hashCode()
{
return list.hashCode();
}
@Override
public boolean equals(Object obj)
{
return list.equals(obj);
}
2013-10-26 13:25:51 -04:00
@Override
public Iterator iterator()
2013-10-26 13:25:51 -04:00
{
return list.iterator();
}
}