import java.util.Arrays;
import java.util.ArrayList;
public class ArrayBag<T> implements BagInterface<T> {
/**
* The bag which holds all the items added to it.
*
* @var array
* @access private
**/
private T[] bag;
/**
* The default capacity. This value is a constant, it cannot be
* altered. To change the value of DEFAULT_CAPACITY, simply instantiate
* this object by passing an integer parameter to it.
*
* @var final integer
* @access private
**/
private int DEFAULT_CAPACITY;
/**
* The number of entries contain in the bag.
*
* @var integer
* @access private
**/
private int numberOfEntries = 0;
/**
* Constructor method. This method initialize DEFAULT_CAPACITY to 10.
* When the capacity is full, we'll never add a new item to it.
*
**/
public ArrayBag(){
DEFAULT_CAPACITY = 10;
bag = init();
}
/**
* Another constructor method that accepts a parameter. We'll then set the
* value to it.
*
* @param c
*/
public ArrayBag(int c){
DEFAULT_CAPACITY = c;
bag = init();
}
public final T[] init(){
return (T[]) new Object[DEFAULT_CAPACITY];
}
@Override
public int getCurrentSize(){
return numberOfEntries;
}
@Override
public boolean isFull() {
int capacity = DEFAULT_CAPACITY;
return (getCurrentSize() >= capacity || getCurrentSize() == capacity);
}
@Override
public boolean isEmpty() {
return ( getCurrentSize() <= 0 );
}
@Override
public boolean add(T newEntry) {
if ( isFull() || newEntry == null )
return false;
bag[ numberOfEntries ] = newEntry;
numberOfEntries++;
return true;
}
@Override
public boolean add_distinct(T newEntry){
if ( isFull() || newEntry == null || this.contains(newEntry) )
return false;
bag[ numberOfEntries ] = newEntry;
numberOfEntries++;
return true;
}
@Override
public boolean add(T... newEntries){
T[] original_bag = bag;
boolean flag = false;
for ( T entry : newEntries ){
flag = add(entry);
// one of the entry cannot be added, reverse to the original bag.
if ( ! flag ){
bag = original_bag;
return flag;
}
}
return flag;
}
@Override
public T remove() {
int n = numberOfEntries;
T target = bag[n-1];
T[] new_bag = init();
System.arraycopy(bag, 0, new_bag, 0, numberOfEntries-1);
if ( new_bag.length >= 0 ){
bag = new_bag;
numberOfEntries--;
}
return numberOfEntries < n ? target : null;
}
@Override
public boolean remove(T anEntry) {
if ( !contains(anEntry) )
return false;
T[] new_bag = init();
int j = 0;
for ( int i=0; i<getCurrentSize(); i++ ){
if ( ! anEntry.equals(bag[i]) ){
new_bag[j] = bag[i];
j++;
}
}
if ( new_bag.length >= 0 ){
bag = new_bag;
numberOfEntries--;
}
return true;
}
@Override
public void clear() {
bag = init();
numberOfEntries = 0;
}
@Override
public int getFrequencyOf(T anEntry) {
int fq = 0;
for ( T x : bag )
if ( anEntry.equals(x) )
fq++;
return fq;
}
@Override
public boolean contains(T anEntry) {
for ( int i = 0; i<numberOfEntries; i++ )
if ( anEntry.equals(bag[i]) )
return true;
return false;
}
@Override
public T[] toArray() {
T[] return_bag = init();
for ( int i=0, j=0; i < getCurrentSize(); i++, j++ )
if ( get(i) != null )
return_bag[j] = bag[i];
else
break;
return return_bag;
}
@Override
public BagInterface<T> union(BagInterface<T> object){
BagInterface<T> bags = new ArrayBag(DEFAULT_CAPACITY + object.toArray().length);
bags.add(bag);
bags.add(object.toArray());
return bags;
}
@Override
public BagInterface<T> intersect(BagInterface<T> object){
T[] bag1 = bag, bag2 = object.toArray();
int size = DEFAULT_CAPACITY + bag1.length;
BagInterface<T> bags = new ArrayBag(DEFAULT_CAPACITY + bag1.length);
ArrayList<T> c1 = new ArrayList<T>( Arrays.asList(bag1) );
ArrayList<T> c2 = new ArrayList<T>( Arrays.asList(bag2) );
c1.retainAll(c2);
for ( int i=0; i < c1.size(); i++ )
bags.add( c1.get(i) );
return bags;
}
@Override
public BagInterface<T> difference(BagInterface<T> object){
T[] bag1 = bag, bag2 = object.toArray();
BagInterface<T> bags = new ArrayBag(DEFAULT_CAPACITY + object.getCurrentSize());
for ( int i=0; i<getCurrentSize(); i++ )
for ( int j=0; j<object.getCurrentSize(); j++ ){
if ( ! bag1[i].equals(bag2[j]) ){
bags.add(bag1[i]);
break;
}
}
return bags;
}
@Override
public T get(int index){
return bag[index];
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.