Guest User

Untitled

a guest
Oct 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package Esercizi;
  2. import java.util.Comparator;
  3.  
  4. import Comparator.DefaultComparator;
  5. import Exception.InvalidKeyException;
  6. import Position.NodePositionList;
  7. import Position.Position;
  8. import PriorityQueue.*;
  9.  
  10.  
  11. public class EntryContainer<K,V> {
  12.  
  13.     private NodePositionList<MyEntry<K,V>> contenitore;
  14.     private Comparator<K> c;
  15.    
  16.     protected class MyEntry<K,V> extends Entry<K,V>{
  17.  
  18.         private Position<MyEntry<K,V>> locazione;
  19.        
  20.         public MyEntry(K key, V elem) {
  21.             super(key, elem);
  22.         }
  23.        
  24.         public void setPosition(Position<MyEntry<K,V>> p){
  25.             locazione=p;
  26.         }
  27.        
  28.         public Position<MyEntry<K,V>> getPosition(){
  29.             return locazione;
  30.         }
  31.        
  32.     }
  33.    
  34.    
  35.     public EntryContainer(){
  36.         contenitore=new NodePositionList<MyEntry<K,V>>();
  37.         c=new DefaultComparator<K>();
  38.        
  39.     }
  40.    
  41.     public EntryContainer(Comparator<K> c){
  42.         contenitore=new NodePositionList<MyEntry<K,V>>();
  43.         this.c=c;
  44.     }
  45.    
  46.     public boolean checkKey(K key) throws InvalidKeyException{
  47.         boolean result;
  48.         try{
  49.             result = (c.compare(key, key)==0);
  50.         } catch (ClassCastException e)
  51.         { throw new InvalidKeyException();}
  52.         return result;
  53.     }
  54.    
  55.     public MyEntry<K,V> add(K key,V value){
  56.         checkKey(key);
  57.         if(contenitore.isEmpty()){
  58.             MyEntry<K,V> nuova=new MyEntry(key, value);
  59.             contenitore.addLast(nuova);
  60.             nuova.setPosition(contenitore.first());
  61.             return nuova;
  62.         }
  63.         for(MyEntry<K,V> a : contenitore){
  64.             if(a.getKey().equals(K))
  65.                 return null;
  66.            
  67.         }
  68.         MyEntry<K,V> nuova=new MyEntry(key, value);
  69.         for(MyEntry<K,V> a: contenitore){
  70.             if(c.compare(a.getKey(), nuova.getKey())>0)
  71.                 continue;
  72.             else{
  73.                 contenitore.addBefore(a.getPosition(), nuova);
  74.            
  75.             }
  76.         }
  77.         for(Position<MyEntry<K,V>> pos : contenitore.positions()){
  78.             if(c.compare(pos.element().getKey(), nuova.getKey())==0)
  79.                 nuova.setPosition(pos);
  80.         }
  81.     }
  82.    
  83.     public V remove(Entry<K,V> e){
  84.         try{
  85.             MyEntry<K,V> test=(MyEntry<K, V>) e;
  86.             return contenitore.remove(test).getEntry();
  87.         }catch(Exception x){
  88.             return null;
  89.         }
  90.        
  91.        
  92.     }
  93.    
  94.     public V remove(){
  95.         return contenitore.remove(contenitore.last()).getEntry();
  96.     }
  97.    
  98.    
  99.     public int size(){
  100.         return contenitore.size();
  101.     }
  102.    
  103.     public boolean isEmpty(){
  104.         return contenitore.isEmpty();
  105.     }
  106.    
  107. }
Add Comment
Please, Sign In to add comment