Advertisement
DanFloyd

Tuple

Mar 29th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. public class Tuple implements TupleItf
  2. {
  3.     // OVERVIEW : Tuple è la classe che
  4.     // implementa l'interfaccia TupleItf seguendo le
  5.     // specifiche indicate in quest'ultima.
  6.  
  7.     // Funzione di astrazione:
  8.     // A(c) = [chiave->valore]
  9.  
  10.     // Invariante di rappresentazione:
  11.     // I(c) = ((c.getKey()!=null)&&(c.getValue!=null))
  12.    
  13.     // Variabili di istanza:
  14.     private String key, value;
  15.    
  16.     // Costruttore di default
  17.     public Tuple()
  18.     // REQUIRE : ---
  19.     // EFFECTS : inizializza this come tupla vuota.
  20.     {
  21.         this.key = "";
  22.         this.value = "";
  23.     }
  24.  
  25.     // Costruttore con parametri
  26.     public Tuple(String k, String v)
  27.     // REQUIRE : ---
  28.     // EFFECTS : inizializza this con i parametri di input.
  29.     {
  30.         this.key = k;
  31.         this.value = v;
  32.     }
  33.  
  34.     // Metdodi :
  35.    
  36.     public void setKey(String k) throws NullPointerException
  37.     // REQUIRE : k è una stringa non nulla && this != null
  38.     // EFFECTS : imposta l'elemento chiave con il
  39.     // parametro k, se this è nullo o k è nullo lancia un' eccezione
  40.     {
  41.         if((k==null)||(this==null)) throw new NullPointerException("@setKey");
  42.         this.key = k;
  43.     }
  44.    
  45.     public void setValue(String v) throws NullPointerException
  46.     // REQUIRE : v è una stringa non nulla && this != null
  47.     // EFFECTS : imposta l'elemento valore con il
  48.     // parametro v, se this è nullo o v è nullo lancia un' eccezione
  49.     {
  50.         if((v==null)||(this==null)) throw new NullPointerException("@setValue");
  51.         this.value = v;
  52.     }
  53.  
  54.     public String getKey() throws NullPointerException
  55.     // REQUIRE : this != null
  56.     // EFFECTS : restituisce una stringa contenente l'elemento chiave
  57.     // o lancia un'eccezione nel caso in cui this==null o this.key == null
  58.     {
  59.         if((this==null)||(this.key==null)) throw new NullPointerException("@getKey");
  60.         return this.key;
  61.     }
  62.  
  63.     public String getValue() throws NullPointerException
  64.     // REQUIRE : this != null
  65.     // EFFECTS : restituisce una stringa contenente l'elemento valore
  66.     // o lancia un'eccezione nel caso in cui this==null o this.value == null
  67.     {
  68.         if((this==null)||(this.value==null)) throw new NullPointerException("@getValue");
  69.         return this.value;
  70.     }
  71.    
  72.     public int compareTo(Tuple t) throws NullPointerException
  73.     // REQUIRE : this != null && t != null
  74.     // EFFECTS : restituisce un intero negativo se this.key precede
  75.     // lessicograficamente t.key, positivo se segue o zero se sono
  76.     // identici. Nel caso in cui uno degli elementi sopra citati o gli
  77.     // oggetti stessi siano nulli, lancia un'eccezione.
  78.     {
  79.         if((this==null)||(t==null)) throw new NullPointerException("@compareTo");
  80.         if((this.key==null)||(t.key==null)) throw new NullPointerException("@compareTo");
  81.         return this.key.compareTo(t.key);
  82.     }
  83.  
  84.     // Metodo di astrazione:
  85.     public String toString() throws NullPointerException
  86.     // REQUIRE : this != null
  87.     // EFFECTS : restituisce una stringa contenente la rappresentazione
  88.     // grafica della tupla come specificato nell'overview oppure lancia
  89.     // un'eccezione se this == null.
  90.     {
  91.         if(this==null) throw new NullPointerException("@toString");
  92.         if((this.getKey()=="")&&(this.getValue()=="")) return "[]";
  93.         return "["+this.getKey()+"->"+this.getValue()+"]";
  94.     }
  95.  
  96.     // Metodo di controllo dell'invariante di rappresentazione:
  97.     public boolean repOk() throws NullPointerException
  98.     // REQUIRE : this != null
  99.     // EFFECTS : restituisce true se l'invariante è soddisfatto
  100.     // false altrimenti oppure lancia
  101.     // un'eccezione se this==null.
  102.     {
  103.         if(this==null) throw new NullPointerException("@repOk");
  104.         return ((this.getKey()!=null)&&(this.getValue()!=null));
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement