144 lines
3.6 KiB
Java
144 lines
3.6 KiB
Java
package dictionary;
|
|
|
|
import exceptions.InvalidEntryException;
|
|
import position.NodePositionList;
|
|
import position.Position;
|
|
import priorityqueue.Entry;
|
|
import priorityqueue.MyEntry;
|
|
|
|
import exceptions.InvalidKeyException;
|
|
|
|
/**
|
|
* Created with MONSTER.
|
|
* User: xgiovio
|
|
* Date: 11/05/2014
|
|
* Time: 19:04
|
|
*/
|
|
public class LogFile<K,V> implements Dictionary<K,V> {
|
|
|
|
NodePositionList<Entry<K,V>> list = new NodePositionList<Entry<K,V>>();
|
|
|
|
@Override
|
|
public int size() {
|
|
return list.size();
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return list.isEmpty();
|
|
}
|
|
|
|
@Override
|
|
public Entry<K, V> find(K key) throws InvalidKeyException {
|
|
checkKey(key);
|
|
for(Position<Entry<K,V>> p: list.positions()){
|
|
Entry<K,V> e= p.element();
|
|
if(e.getKey().equals(key)){
|
|
return e;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
|
checkKey(key);
|
|
NodePositionList<Entry<K,V>> to_return = new NodePositionList<Entry<K,V>>();
|
|
for(Position<Entry<K,V>> p: list.positions()){
|
|
Entry<K,V> e= p.element();
|
|
if(e.getKey().equals(key)){
|
|
to_return.addLast(e);
|
|
}
|
|
}
|
|
return to_return;
|
|
}
|
|
|
|
@Override
|
|
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
|
|
checkKey(key);
|
|
LocationAwareEntry<K,V> t = new LocationAwareEntry<K, V>(key,value);
|
|
list.addLast(t);
|
|
t.setLocation(list.last());
|
|
return t;
|
|
}
|
|
|
|
protected Entry<K, V> insert(Entry<K,V> in_e) throws InvalidKeyException {
|
|
//in_e not used because used internally
|
|
LocationAwareEntry<K,V> t = (LocationAwareEntry<K,V>)in_e;
|
|
list.addLast(t);
|
|
t.setLocation(list.last());
|
|
return t;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
|
LocationAwareEntry<K,V> t = checkEntry(e);
|
|
list.remove(t.location());
|
|
t.setLocation(null);
|
|
return t;
|
|
}
|
|
|
|
@Override
|
|
public Iterable<Entry<K, V>> entries() {
|
|
return list;
|
|
}
|
|
|
|
//aux
|
|
protected void checkKey(K key) throws InvalidKeyException {
|
|
if (key == null )
|
|
throw new InvalidKeyException("Invalid key");
|
|
}
|
|
|
|
// inner class
|
|
protected static class LocationAwareEntry<K,V> extends MyEntry<K,V> {
|
|
protected Position<Entry<K, V>> loc = null;
|
|
|
|
public LocationAwareEntry(K k, V v) {
|
|
super(k, v);
|
|
}
|
|
|
|
public LocationAwareEntry(K k, V v, Position pos) {
|
|
super(k, v);
|
|
loc = pos;
|
|
}
|
|
|
|
|
|
|
|
protected Position < Entry<K,V>> location() {
|
|
return loc;
|
|
}
|
|
protected Position < Entry<K,V> >setLocation(Position< Entry<K,V>> pos) {
|
|
Position <Entry<K,V>> oldPosition = location();
|
|
loc = pos;
|
|
return oldPosition;
|
|
}
|
|
protected K setKey(K k) {
|
|
K oldKey = getKey();
|
|
key = k;
|
|
return oldKey;
|
|
}
|
|
protected V setValue(V v) {
|
|
V oldValue = getValue();
|
|
value = v;
|
|
return oldValue;
|
|
}
|
|
|
|
public String toString() { return "(" + key + "," + value + ")"; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
protected LocationAwareEntry<K,V> checkEntry(Entry<K,V> e) throws InvalidEntryException
|
|
{
|
|
if(e == null || !(e instanceof LocationAwareEntry) || isEmpty() )
|
|
throw new InvalidEntryException("entrata non valida");
|
|
return (LocationAwareEntry) e;
|
|
}
|
|
}
|