diff options
Diffstat (limited to 'project/JavaCommon/src/com/modulus/common')
11 files changed, 636 insertions, 0 deletions
diff --git a/project/JavaCommon/src/com/modulus/common/TwoTypeBean.java b/project/JavaCommon/src/com/modulus/common/TwoTypeBean.java new file mode 100644 index 0000000..28e1e41 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/TwoTypeBean.java @@ -0,0 +1,44 @@ +package com.modulus.common; + +/** + * Class that simply holds two different (or same) object + * types. It is a rough implementation of what a tuple can be + * considered + * + * @author jrahm + * + * @param <T> type of object 1 + * @param <E> type of object 2 + */ +public class TwoTypeBean<T, E> { + private T obj1; + private E obj2; + + /** + * Creates a new TwoTypeBean from the two + * objects. + * + * @param obj1 object 1 + * @param obj2 object 2 + */ + public TwoTypeBean(T obj1, E obj2){ + this.obj1 = obj1; + this.obj2 = obj2; + } + + /** + * Returns the first object + * @return the first object + */ + public T getObject1(){ + return obj1; + } + + /** + * Returns the second object + * @return the second object + */ + public E getObject2(){ + return obj2; + } +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/ArrayStack.java b/project/JavaCommon/src/com/modulus/common/collections/ArrayStack.java new file mode 100644 index 0000000..17dfcb1 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/ArrayStack.java @@ -0,0 +1,101 @@ +package com.modulus.common.collections; + +import java.io.Serializable; + +/** + * Class which uses an array to be used to + * implement a stack. + * + * This class is most efficient for + * nearly every purpose. + * + * @author jrahm + * + * @param <T> the type this stack holds + */ +public class ArrayStack<T> implements Stack<T>, Serializable{ + + private Object[] arr; + private int pointer = -1; + + /** + * Creates a new ArrayStack with a + * default starting size of 10 + */ + public ArrayStack(){ + this(10); + } + + /** + * Creates a new ArrayStack with + * a default starting size of <code>size</code> + * @param size + */ + public ArrayStack( int size ){ + arr = new Object[size]; + } + + /** + * Ensures that this ArrayStack has enough space + * to store <code>size</code> number of elements. + * + * @param size the number of elements needed to store + */ + public void ensureSize( int size ){ + + if(arr.length > size) + return; + + Object[] tmp = new Object[size*2]; + for ( int i = 0;i < arr.length; i ++ ){ + tmp[i] = arr[i]; + } + + arr = tmp; + } + + @Override + public boolean isEmpty() { + return pointer == -1; + } + + @SuppressWarnings("unchecked") + @Override + public T peek() { + return (T)arr[pointer]; + } + + @SuppressWarnings("unchecked") + @Override + public T pop() { + return (T) arr[ pointer -- ]; + } + + @Override + public void push(T obj) { + pointer ++; + this.ensureSize(pointer); + arr[pointer] = obj; + } + + @Override + public int size() { + return pointer + 1; + } + + @Override + public void clear(){ + pointer = 0; + } + + @Override + public String toString(){ + StringBuffer buf = new StringBuffer("["); + + for( int i = pointer; i >= 1; i --) + buf.append(arr[i] + ", "); + + buf.append(arr[0] + "]"); + return buf.toString(); + } +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/BaseModelData.java b/project/JavaCommon/src/com/modulus/common/collections/BaseModelData.java new file mode 100644 index 0000000..250f980 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/BaseModelData.java @@ -0,0 +1,29 @@ +package com.modulus.common.collections; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class BaseModelData<T> implements ModelData<T>{ + private Map<Object, T> map = new HashMap<Object, T>(); + + @Override + public T get(Object key) { + return map.get(key); + } + + @Override + public Object[] keys() { + Set<Object> keys = map.keySet(); + + return keys.toArray( new Object[keys.size()] ); + } + + @Override + public void set(Object key, Object obj) { + // TODO Auto-generated method stub + + } + + +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/Collections2.java b/project/JavaCommon/src/com/modulus/common/collections/Collections2.java new file mode 100644 index 0000000..65bcc60 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/Collections2.java @@ -0,0 +1,28 @@ +package com.modulus.common.collections; + +public class Collections2 { + + static public Integer[] rangeAsObjects(int start, int stop) + { + Integer[] result = new Integer[stop-start]; + + for(int i=0;i<stop-start;i++) + result[i] = start+i; + + return result; + } + + static public int[] range(int start, int stop) + { + int[] result = new int[stop-start]; + + for(int i=0;i<stop-start;i++) + result[i] = start+i; + + return result; + } + + static public int[] range(int stop){ + return range(0, stop); + } +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/Grid.java b/project/JavaCommon/src/com/modulus/common/collections/Grid.java new file mode 100644 index 0000000..817a36c --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/Grid.java @@ -0,0 +1,35 @@ +package com.modulus.common.collections; + +/** + * Interface describing an immutable grid interface. + * + * @author jrahm + * + * @param <T> the type of object this grid holds + */ +public interface Grid<T> { + /** + * Returns the object stored in + * row <code>row</code> and column + * <code>col</code> + * + * @param row the row of the object + * @param col the column of the object + * @return the object stored in that row and column + */ + public T get(int row, int col); + + /** + * Returns the number of columns in this grid + * + * @return the number of columns in this grid + */ + public int numberOfColumns(); + + /** + * Returns the number of columns in this grid. + * + * @return the number of columns in this grid + */ + public int numberOfRows(); +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/IntMap.java b/project/JavaCommon/src/com/modulus/common/collections/IntMap.java new file mode 100644 index 0000000..558cd9f --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/IntMap.java @@ -0,0 +1,84 @@ +package com.modulus.common.collections; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class IntMap<T> implements Map<Integer, T> { + private List<T> list = new ArrayList<T>(); + + @Override + public void clear() { + this.list.clear(); + } + + @Override + public boolean containsKey(Object arg0) { + if( arg0 instanceof Integer) + return ((Integer) arg0) < this.list.size(); + + return false; + } + + @Override + public boolean containsValue(Object arg0) { + return list.contains(arg0); + } + + @Override + public Set<java.util.Map.Entry<Integer, T>> entrySet() { + throw new RuntimeException("Not Implemented (At The Moment)"); + } + + @Override + public T get(Object arg0) { + try{ + return list.get( (Integer)arg0 ); + } catch(ClassCastException e){ + return null; + } + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public Set<Integer> keySet() { + return new HashSet<Integer>( Arrays.asList( Collections2.rangeAsObjects(0, list.size()) ) ); + } + + @Override + public T put(Integer arg0, T arg1) { + return list.set(arg0, arg1); + } + + @Override + public void putAll(Map<? extends Integer, ? extends T> arg0) { + throw new RuntimeException("Not Implemented At the Moment"); + } + + @Override + public T remove(Object arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public Collection<T> values() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/MArrays.java b/project/JavaCommon/src/com/modulus/common/collections/MArrays.java new file mode 100644 index 0000000..08d9540 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/MArrays.java @@ -0,0 +1,180 @@ +package com.modulus.common.collections; + +import java.lang.reflect.Array; +import java.util.AbstractList; +import java.util.List; + +public class MArrays { + public static int indexOf( Object[] objs , Object obj, int start ){ + for ( int i = start; i < objs.length; i ++) + if(objs[i].equals(obj)) + return i; + return -1; + } + + public static int lastIndexOf( Object[] objs , Object obj, int end ){ + for ( int i = end; i >= 0; i --) + if(objs[i].equals(obj)) + return i; + return -1; + } + + public static int indexOf( Object[] objs, Object obj ){ + return indexOf( objs, obj, 0 ); + } + + public static int lastIndexOf( Object[] objs, Object obj ){ + return lastIndexOf( objs, obj, objs.length - 1 ); + } + + public static String concat(String[] args, int off, int len, String sep){ + StringBuffer buf = new StringBuffer(); + int lastDex = len + off; + + for(int i = off;i < lastDex-1;i++) + buf.append(args[i] + sep); + buf.append(args[lastDex-1]); + + return buf.toString(); + } + + public static String concat(String[] args, int off, String sep){ + return concat(args, off, args.length-off, sep); + } + + public static String concat(String[] args, String sep){ + return concat(args, 0, args.length, sep); + } + + public static String concat(String[] args, int off){ + return concat(args, off, ""); + } + + public static String concat(String[] args){ + return concat(args, ""); + } + + public static String concat(String[] args, int off, int len){ + return concat(args, off, len, ""); + } + + public static List<Byte> asList( final byte[] bytes ){ + return new AbstractList<Byte>() { + + @Override + public Byte get(int arg0) { + return bytes[arg0]; + } + + @Override + public int size() { + return bytes.length; + } + }; + } + + public static List<Character> asList( final char[] chars ){ + return new AbstractList<Character>() { + + @Override + public Character get(int arg0) { + return chars[arg0]; + } + + @Override + public int size() { + return chars.length; + } + }; + } + + public static List<Integer> asList( final int[] ints ){ + return new AbstractList<Integer>() { + + @Override + public Integer get(int arg0) { + return ints[arg0]; + } + + @Override + public int size() { + return ints.length; + } + }; + } + + public static List<Long> asList( final long[] longs ){ + return new AbstractList<Long>() { + + @Override + public Long get(int arg0) { + return longs[arg0]; + } + + @Override + public int size() { + return longs.length; + } + }; + } + + public static List<Double> asList( final double[] doubles ){ + return new AbstractList<Double>() { + + @Override + public Double get(int arg0) { + return doubles[arg0]; + } + + @Override + public int size() { + return doubles.length; + } + }; + } + + public static List<Float> asList( final float[] floats ){ + return new AbstractList<Float>() { + + @Override + public Float get(int arg0) { + return floats[arg0]; + } + + @Override + public int size() { + return floats.length; + } + }; + } + + public static List<Short> asList( final short[] shorts ){ + return new AbstractList<Short>() { + + @Override + public Short get(int arg0) { + return shorts[arg0]; + } + + @Override + public int size() { + return shorts.length; + } + }; + } + + public static List<Boolean> asList( final boolean[] bools ){ + return new AbstractList<Boolean>() { + + @Override + public Boolean get(int arg0) { + return bools[arg0]; + } + + @Override + public int size() { + return bools.length; + } + }; + } +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/ModelData.java b/project/JavaCommon/src/com/modulus/common/collections/ModelData.java new file mode 100644 index 0000000..245015b --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/ModelData.java @@ -0,0 +1,9 @@ +package com.modulus.common.collections; + +public interface ModelData<T> { + public T get(Object key); + + public void set(Object key, T obj); + + public Object[] keys(); +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/Stack.java b/project/JavaCommon/src/com/modulus/common/collections/Stack.java new file mode 100644 index 0000000..1d2c36c --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/Stack.java @@ -0,0 +1,57 @@ +package com.modulus.common.collections; + +/** + * Class defines a better used stack than what is + * in the java api. This stack is an interface + * with many different implementations. + * + * @author jrahm + * + * @param <T> the type this stack stores + */ +public interface Stack<T> { + /** + * Removes and returns the element that is on + * the top of the stack. + * + * @return the element which is on the top of the stack. + */ + public T pop(); + + /** + * Places object <code>obj</code> on the top of + * the stack + * + * @param obj the object to push on the stack. + */ + public void push(T obj); + + /** + * Returns the object on the top of the stack + * but does <b>not</b> remove it. + * + * @return the object on the top of the stack + */ + public T peek(); + + /** + * Returns the number of elements in this + * stack. + * + * @return the number of elements in this stack. + */ + public int size(); + + /** + * Returns true of this stack has no elements, + * returns false otherwise. + * + * @return true it this stack has no elements, false otherwise + */ + public boolean isEmpty(); + + /** + * Empties the stack. + */ + public void clear(); +} diff --git a/project/JavaCommon/src/com/modulus/common/collections/UpdateableGrid.java b/project/JavaCommon/src/com/modulus/common/collections/UpdateableGrid.java new file mode 100644 index 0000000..a48ee31 --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/collections/UpdateableGrid.java @@ -0,0 +1,21 @@ +package com.modulus.common.collections; + +/** + * Interface that extends grid to make a grid + * mutable. + * + * @author jrahm + * + * @param <T> the type this grid holds + */ +public interface UpdateableGrid<T> extends Grid<T>{ + /** + * sets the reference in row <code>row</code> and column <code>col</code> + * to <code>obj</code> + * + * @param row the row + * @param col the column + * @param obj the object + */ + public void set(int row, int col, T obj); +} diff --git a/project/JavaCommon/src/com/modulus/common/strings/Tokenizer.java b/project/JavaCommon/src/com/modulus/common/strings/Tokenizer.java new file mode 100644 index 0000000..80363cd --- /dev/null +++ b/project/JavaCommon/src/com/modulus/common/strings/Tokenizer.java @@ -0,0 +1,48 @@ +package com.modulus.common.strings; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Class that tokenizes strings based on which groups + * the characters fit into. + * + * If the group is -1, then that tells the tokenizer to + * not include those tokens and instead delete the characters + * that belong to that group in the process of splitting. + * + * @author jrahm + * + */ +public abstract class Tokenizer { + + public String[] tokenize(String str){ + if(str.length() == 0) + return new String[]{}; + + List<String> tokens = new ArrayList<String>(); + StringBuffer buffer = new StringBuffer(); + + int curGroup = groupOf(str.charAt(0)); + for(int i = 0;i < str.length();i++){ + char ch = str.charAt(i); + + int temp = groupOf(ch); + if(temp != curGroup && curGroup != -1){ + curGroup = temp; + tokens.add(buffer.toString()); + + buffer = new StringBuffer(); + } + + if(temp != -1) + buffer.append(ch); + } + tokens.add(buffer.toString()); + + return tokens.toArray(new String[tokens.size()]); + } + + public abstract int groupOf(char ch); +} |