Advertisement
DanFloyd

Voc

Mar 29th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1. public class Voc implements VocItf
  2. // Implementazione dell'interfaccia VocItf
  3. {
  4.     // OVERVIEW:
  5.     // Un Vocabolario e' una collezione di associazioni del tipo
  6.     // 'chiave -> valore', dove sia 'chiave' che 'valore' sono stringhe,
  7.     // e valgono le seguenti proprieta':
  8.  
  9.     // - non ci possono essere due associazioni per la stessa chiave;
  10.     // - le associazioni sono ordinate in base alla chiave.
  11.  
  12.     // Rappresentiamo astrattamente un vocabolario con n associazioni come
  13.     // =====================
  14.     // chiave-1 -> valore-1
  15.     // chiave-n -> valore-n
  16.     // chiave-2 -> valore-2
  17.     // ...
  18.     // ====================
  19.     // Dove chiave-i < chiave-(i+1) per ogni 0 <= i < n.
  20.  
  21.     // Invariante di rappresentazione:
  22.     // I(c) = (this.IsEmpty())|| this.vect.size()==1 ||
  23.     //    (for i=0...this.vect.size()-1
  24.     //       for j=1...this.vect.size()
  25.     //             (i<j implica this.vect.get(i).key<this.vect.get(j).key)))
  26.  
  27.     // Funzione di astrazione:
  28.     // A(c) = =============
  29.     //    =============
  30.     // se this.IsEmpty(), altrimenti
  31.     // A(c) = =============
  32.     //        [key0->value]
  33.     //    [key1->value]
  34.     //    [....->.....]
  35.     //    [keyN->value]
  36.     //    =============
  37.  
  38.     // Variabili d'istanza
  39.     private boolean empty;
  40.     private java.util.Vector<Tuple> vect;
  41.  
  42.     // Costruttore senza parametri
  43.     public Voc()
  44.     // REQUIRE : ---
  45.     // EFFECTS : inizializza this come vocabolario vuoto
  46.     {
  47.         this.empty = true;
  48.         this.vect = new java.util.Vector<Tuple>();
  49.     }
  50.  
  51.     // Metodi:
  52.  
  53.     public boolean isEmpty() throws NullPointerException
  54.     // REQUIRE : this != null
  55.     // EFFECTS : se il vocabolario è vuoto restituisce true
  56.     // false altrimenti, oppure lancia un'eccezione se this è vuoto.
  57.     {
  58.         if(this==null) throw new NullPointerException("@isEmpty");
  59.         return this.empty;
  60.     }
  61.  
  62.     public boolean isIn(String k) throws NullPointerException
  63.     // REQUIRE : this != null && key != null &&
  64.     // (!this.IsEmpty()) -> per ogni
  65.     // associazione 0 <= i < N : chiave(i) < chiave(i+1)
  66.     // (clausola di ordinamento corretto)
  67.     // EFFECTS : se key appartiene a this restituisce true
  68.     // false altrimenti oppure lancia un eccezione se this==null o
  69.     // key==null
  70.     {
  71.         if((this==null)||(k==null)) throw new NullPointerException("@isIn");
  72.         if(this.isEmpty()) return false;
  73.         int i = 0, s = this.vect.size();
  74.         Tuple t = new Tuple(k,"");
  75.         while((i<s-1)&&(t.compareTo(this.vect.get(i))>0)) i++;
  76.         return (t.compareTo(this.vect.get(i))==0);
  77.     }
  78.  
  79.     public String searchTuple(String k) throws NotFoundException, NullPointerException
  80.     // REQUIRE : this != null && k != null && if (!this.IsEmpty()) -> per ogni
  81.     // associazione 0 <= i < N : chiave(i) < chiave(i+1) (clausola di ordinamento corretto)
  82.     // EFFECTS : Lancia un eccezione NullPointerException se this o una delle chiavi è null
  83.     // altrimenti restituisce il valore associato a key se quest'ultima è presente,
  84.     // nel caso in cui non lo sia lancia NotFoundException.
  85.     {
  86.         if((this==null)||(k==null)) throw new NullPointerException("@searchTuple");
  87.         if(this.isEmpty()) throw new NotFoundException("@searchTuple");
  88.         int i = 0, s = this.vect.size();
  89.         Tuple t = new Tuple(k,"");
  90.         while((i<s-1)&&(t.compareTo(vect.get(i))>0)) i++;
  91.         if(t.compareTo(this.vect.get(i))==0) return this.vect.get(i).getValue();
  92.         throw new NotFoundException("@searchTuple");
  93.     }
  94.    
  95.     public void insertTuple(String k, String v) throws NullPointerException
  96.     // REQUIRE : (this != null && k != null && v != null)
  97.     // n.b. clausola di ordinamento corretto
  98.     // EFFECTS : se (!isIn(k)) aggiunge all'oggetto this l'associazione key-value
  99.     // altrimenti aggiorna il valore dell'associazione presente nel vocabolario
  100.     // con quello passato come parametro di ingresso oppure lancia la NullPointerException
  101.     // se this o una delle chiavi/valori è null.
  102.     {
  103.         if((this==null)||(k==null)||(v==null)) throw new NullPointerException("@insertTuple");
  104.         Tuple nuT = new Tuple(k,v);
  105.         if(this.isEmpty()) {
  106.             this.vect.add(0,nuT);
  107.             this.empty = false;
  108.         }
  109.         else{
  110.             String s;
  111.             boolean flag = false;
  112.             try {s = this.searchTuple(k);}
  113.             catch (NotFoundException e) {flag = true;}
  114.             catch (NullPointerException e) {throw new NullPointerException("@insertTuple");}
  115.             int i = 0;         
  116.             if(!flag){
  117.                 while((nuT.compareTo(this.vect.get(i))!=0)) i++;
  118.                 this.vect.set(i,nuT);
  119.             }
  120.             else{
  121.                 while((i<this.vect.size())&&(nuT.compareTo(this.vect.get(i))>0)) i++;
  122.                 this.vect.add(i,nuT);
  123.             }
  124.         }
  125.     }
  126.  
  127.     public void removeTuple(String k) throws EmptyException, NotFoundException, NullPointerException
  128.     // REQUIRE :(this != null && k != null)
  129.     // // n.b. clausola di ordinamento corretto
  130.     // EFFECTS : Se il vocabolario è vuoto lancia la EmptyException altrimenti
  131.     // se isIn(key) rimuove dal vocabolario la tupla key-value, in caso contrario
  132.     // lancia la NotFoundException o la NullPointerException se this o una delle chiavi
  133.     // è nulla.
  134.     {
  135.         if((this==null)||(k==null)) throw new NullPointerException("@removeTuple");
  136.         if(this.isEmpty()) throw new EmptyException("@removeTuple");
  137.         if(!this.isIn(k)) throw new NotFoundException("@removeTuple");
  138.         int i = 0, s = this.vect.size();
  139.         Tuple vicT = new Tuple(k,"");
  140.         while((vicT.compareTo(vect.get(i))!=0)) i++;
  141.         this.vect.remove(i);
  142.     }
  143.  
  144.     // Funzione di astrazione
  145.     public String toString() throws NullPointerException
  146.     // REQUIRE : this != null && clausola di ordinamento corretto
  147.     // EFFECTS : restituisce sotto forma di stringa la rappresentazione grafica
  148.     // dell'oggetto this come specificato nell' overview.
  149.     {
  150.         if(this==null) throw new NullPointerException("@toString");
  151.         if (this.isEmpty()) return ("=====================\n"+"=====================");
  152.         int i=0, s = this.vect.size();
  153.         String abs = "=====================\n";
  154.         while(i<s){
  155.             abs = abs + this.vect.get(i).toString() +"\n";
  156.             i++;
  157.         }
  158.         abs = abs + "=====================";
  159.         return abs;
  160.     }
  161.  
  162.     // Funzione di controllo dell'invariante di rappresentazione
  163.     public boolean repOk() throws NullPointerException
  164.     // REQUIRE : this != null && clausola di ordinamento corretto
  165.     // EFFECTS : restituisce true se l'invariante è soddisfatta, false in caso
  166.     // contrario
  167.     {
  168.         if(this==null) throw new NullPointerException("@repOk");
  169.         if((this.isEmpty())||(this.vect.size())==1) return true;
  170.         boolean rep = true;
  171.         int i=0, j=1;
  172.         while((i<this.vect.size()-1)&&(rep)){
  173.             while((j<this.vect.size())&&(rep)){
  174.                 if((this.vect.get(i).compareTo(this.vect.get(j)))>0) rep = false;
  175.                 j++;
  176.             }
  177.             i++;       
  178.         }
  179.         return rep;
  180.     }
  181.        
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement