Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Voc implements VocItf
- // Implementazione dell'interfaccia VocItf
- {
- // OVERVIEW:
- // Un Vocabolario e' una collezione di associazioni del tipo
- // 'chiave -> valore', dove sia 'chiave' che 'valore' sono stringhe,
- // e valgono le seguenti proprieta':
- // - non ci possono essere due associazioni per la stessa chiave;
- // - le associazioni sono ordinate in base alla chiave.
- // Rappresentiamo astrattamente un vocabolario con n associazioni come
- // =====================
- // chiave-1 -> valore-1
- // chiave-n -> valore-n
- // chiave-2 -> valore-2
- // ...
- // ====================
- // Dove chiave-i < chiave-(i+1) per ogni 0 <= i < n.
- // Invariante di rappresentazione:
- // I(c) = (this.IsEmpty())|| this.vect.size()==1 ||
- // (for i=0...this.vect.size()-1
- // for j=1...this.vect.size()
- // (i<j implica this.vect.get(i).key<this.vect.get(j).key)))
- // Funzione di astrazione:
- // A(c) = =============
- // =============
- // se this.IsEmpty(), altrimenti
- // A(c) = =============
- // [key0->value]
- // [key1->value]
- // [....->.....]
- // [keyN->value]
- // =============
- // Variabili d'istanza
- private boolean empty;
- private java.util.Vector<Tuple> vect;
- // Costruttore senza parametri
- public Voc()
- // REQUIRE : ---
- // EFFECTS : inizializza this come vocabolario vuoto
- {
- this.empty = true;
- this.vect = new java.util.Vector<Tuple>();
- }
- // Metodi:
- public boolean isEmpty() throws NullPointerException
- // REQUIRE : this != null
- // EFFECTS : se il vocabolario è vuoto restituisce true
- // false altrimenti, oppure lancia un'eccezione se this è vuoto.
- {
- if(this==null) throw new NullPointerException("@isEmpty");
- return this.empty;
- }
- public boolean isIn(String k) throws NullPointerException
- // REQUIRE : this != null && key != null &&
- // (!this.IsEmpty()) -> per ogni
- // associazione 0 <= i < N : chiave(i) < chiave(i+1)
- // (clausola di ordinamento corretto)
- // EFFECTS : se key appartiene a this restituisce true
- // false altrimenti oppure lancia un eccezione se this==null o
- // key==null
- {
- if((this==null)||(k==null)) throw new NullPointerException("@isIn");
- if(this.isEmpty()) return false;
- int i = 0, s = this.vect.size();
- Tuple t = new Tuple(k,"");
- while((i<s-1)&&(t.compareTo(this.vect.get(i))>0)) i++;
- return (t.compareTo(this.vect.get(i))==0);
- }
- public String searchTuple(String k) throws NotFoundException, NullPointerException
- // REQUIRE : this != null && k != null && if (!this.IsEmpty()) -> per ogni
- // associazione 0 <= i < N : chiave(i) < chiave(i+1) (clausola di ordinamento corretto)
- // EFFECTS : Lancia un eccezione NullPointerException se this o una delle chiavi è null
- // altrimenti restituisce il valore associato a key se quest'ultima è presente,
- // nel caso in cui non lo sia lancia NotFoundException.
- {
- if((this==null)||(k==null)) throw new NullPointerException("@searchTuple");
- if(this.isEmpty()) throw new NotFoundException("@searchTuple");
- int i = 0, s = this.vect.size();
- Tuple t = new Tuple(k,"");
- while((i<s-1)&&(t.compareTo(vect.get(i))>0)) i++;
- if(t.compareTo(this.vect.get(i))==0) return this.vect.get(i).getValue();
- throw new NotFoundException("@searchTuple");
- }
- public void insertTuple(String k, String v) throws NullPointerException
- // REQUIRE : (this != null && k != null && v != null)
- // n.b. clausola di ordinamento corretto
- // EFFECTS : se (!isIn(k)) aggiunge all'oggetto this l'associazione key-value
- // altrimenti aggiorna il valore dell'associazione presente nel vocabolario
- // con quello passato come parametro di ingresso oppure lancia la NullPointerException
- // se this o una delle chiavi/valori è null.
- {
- if((this==null)||(k==null)||(v==null)) throw new NullPointerException("@insertTuple");
- Tuple nuT = new Tuple(k,v);
- if(this.isEmpty()) {
- this.vect.add(0,nuT);
- this.empty = false;
- }
- else{
- String s;
- boolean flag = false;
- try {s = this.searchTuple(k);}
- catch (NotFoundException e) {flag = true;}
- catch (NullPointerException e) {throw new NullPointerException("@insertTuple");}
- int i = 0;
- if(!flag){
- while((nuT.compareTo(this.vect.get(i))!=0)) i++;
- this.vect.set(i,nuT);
- }
- else{
- while((i<this.vect.size())&&(nuT.compareTo(this.vect.get(i))>0)) i++;
- this.vect.add(i,nuT);
- }
- }
- }
- public void removeTuple(String k) throws EmptyException, NotFoundException, NullPointerException
- // REQUIRE :(this != null && k != null)
- // // n.b. clausola di ordinamento corretto
- // EFFECTS : Se il vocabolario è vuoto lancia la EmptyException altrimenti
- // se isIn(key) rimuove dal vocabolario la tupla key-value, in caso contrario
- // lancia la NotFoundException o la NullPointerException se this o una delle chiavi
- // è nulla.
- {
- if((this==null)||(k==null)) throw new NullPointerException("@removeTuple");
- if(this.isEmpty()) throw new EmptyException("@removeTuple");
- if(!this.isIn(k)) throw new NotFoundException("@removeTuple");
- int i = 0, s = this.vect.size();
- Tuple vicT = new Tuple(k,"");
- while((vicT.compareTo(vect.get(i))!=0)) i++;
- this.vect.remove(i);
- }
- // Funzione di astrazione
- public String toString() throws NullPointerException
- // REQUIRE : this != null && clausola di ordinamento corretto
- // EFFECTS : restituisce sotto forma di stringa la rappresentazione grafica
- // dell'oggetto this come specificato nell' overview.
- {
- if(this==null) throw new NullPointerException("@toString");
- if (this.isEmpty()) return ("=====================\n"+"=====================");
- int i=0, s = this.vect.size();
- String abs = "=====================\n";
- while(i<s){
- abs = abs + this.vect.get(i).toString() +"\n";
- i++;
- }
- abs = abs + "=====================";
- return abs;
- }
- // Funzione di controllo dell'invariante di rappresentazione
- public boolean repOk() throws NullPointerException
- // REQUIRE : this != null && clausola di ordinamento corretto
- // EFFECTS : restituisce true se l'invariante è soddisfatta, false in caso
- // contrario
- {
- if(this==null) throw new NullPointerException("@repOk");
- if((this.isEmpty())||(this.vect.size())==1) return true;
- boolean rep = true;
- int i=0, j=1;
- while((i<this.vect.size()-1)&&(rep)){
- while((j<this.vect.size())&&(rep)){
- if((this.vect.get(i).compareTo(this.vect.get(j)))>0) rep = false;
- j++;
- }
- i++;
- }
- return rep;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement