
toxgene.util.Dictionary Maven / Gradle / Ivy
/**
* Implements a dictionary container for storing and retrieving objects
* based on a string key.
*
* @version 0.1
* @author Denilson Barbosa
*/
package toxgene.util;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Collections;
import java.lang.ArrayIndexOutOfBoundsException;
public class Dictionary {
private ArrayList> original;
private ArrayList> entries;
private int size=0;
public Dictionary(){
entries = new ArrayList>(10);
original = new ArrayList>(10);
}
public Dictionary(int capacity){
entries = new ArrayList>(capacity);
original = new ArrayList>(capacity);
}
public Dictionary(Dictionary another){
int size = another.size();
entries = new ArrayList>(size);
original = new ArrayList>(size);
this.addAll(another);
}
public Vector values(){
Vector result = new Vector(size);
for (int i=0; i temp = new StringObjectPair(key, value);
int index;
index = Collections.binarySearch(entries, key);
if (index < 0){
original.add(temp);
entries.add(-1-index, temp);
size++;
}
else{
throw new DuplicateKeyException("Key value \""+key+"\" already used!");
}
}
public void replace(String key, T value) throws KeyNotFoundException{
StringObjectPair temp = new StringObjectPair(key, value);
int index;
index = Collections.binarySearch(entries, key);
if (index <0){
throw new KeyNotFoundException("Key value \""+key+"\" not found!");
}
entries.set(index, temp);
}
public void addAll(Dictionary other){
entries.addAll(other.entries);
original.addAll(other.original);
}
public T get(String key) throws KeyNotFoundException{
int index;
index = Collections.binarySearch(entries, key);
if (index <0){
throw new KeyNotFoundException("Key value \""+key+"\" not found!");
}
return entries.get(index).value();
}
public void remove(String key) throws KeyNotFoundException{
StringObjectPair value;
int index;
index = Collections.binarySearch(entries, key);
if (index <0){
throw new KeyNotFoundException("Key value \""+key+"\" not found!");
}
value = entries.get(index);
entries.remove(index);
original.remove(value);
// original?
}
public int size(){
return size;
}
public boolean contains(String key){
int index = Collections.binarySearch(entries, key);
if (index <0){
return false;
}
return true;
}
public Object getValueAt(int i) throws ArrayIndexOutOfBoundsException{
return entries.get(i).value();
}
public Object getKeyAt(int i) throws ArrayIndexOutOfBoundsException{
return entries.get(i).key();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy