Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Esercizi;
- import java.util.Comparator;
- import Comparator.DefaultComparator;
- import Exception.InvalidKeyException;
- import Position.NodePositionList;
- import Position.Position;
- import PriorityQueue.*;
- public class EntryContainer<K,V> {
- private NodePositionList<MyEntry<K,V>> contenitore;
- private Comparator<K> c;
- protected class MyEntry<K,V> extends Entry<K,V>{
- private Position<MyEntry<K,V>> locazione;
- public MyEntry(K key, V elem) {
- super(key, elem);
- }
- public void setPosition(Position<MyEntry<K,V>> p){
- locazione=p;
- }
- public Position<MyEntry<K,V>> getPosition(){
- return locazione;
- }
- }
- public EntryContainer(){
- contenitore=new NodePositionList<MyEntry<K,V>>();
- c=new DefaultComparator<K>();
- }
- public EntryContainer(Comparator<K> c){
- contenitore=new NodePositionList<MyEntry<K,V>>();
- this.c=c;
- }
- public boolean checkKey(K key) throws InvalidKeyException{
- boolean result;
- try{
- result = (c.compare(key, key)==0);
- } catch (ClassCastException e)
- { throw new InvalidKeyException();}
- return result;
- }
- public MyEntry<K,V> add(K key,V value){
- checkKey(key);
- if(contenitore.isEmpty()){
- MyEntry<K,V> nuova=new MyEntry(key, value);
- contenitore.addLast(nuova);
- nuova.setPosition(contenitore.first());
- return nuova;
- }
- for(MyEntry<K,V> a : contenitore){
- if(a.getKey().equals(K))
- return null;
- }
- MyEntry<K,V> nuova=new MyEntry(key, value);
- for(MyEntry<K,V> a: contenitore){
- if(c.compare(a.getKey(), nuova.getKey())>0)
- continue;
- else{
- contenitore.addBefore(a.getPosition(), nuova);
- }
- }
- for(Position<MyEntry<K,V>> pos : contenitore.positions()){
- if(c.compare(pos.element().getKey(), nuova.getKey())==0)
- nuova.setPosition(pos);
- }
- }
- public V remove(Entry<K,V> e){
- try{
- MyEntry<K,V> test=(MyEntry<K, V>) e;
- return contenitore.remove(test).getEntry();
- }catch(Exception x){
- return null;
- }
- }
- public V remove(){
- return contenitore.remove(contenitore.last()).getEntry();
- }
- public int size(){
- return contenitore.size();
- }
- public boolean isEmpty(){
- return contenitore.isEmpty();
- }
- }
Add Comment
Please, Sign In to add comment