Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Tuple implements TupleItf
- {
- // OVERVIEW : Tuple è la classe che
- // implementa l'interfaccia TupleItf seguendo le
- // specifiche indicate in quest'ultima.
- // Funzione di astrazione:
- // A(c) = [chiave->valore]
- // Invariante di rappresentazione:
- // I(c) = ((c.getKey()!=null)&&(c.getValue!=null))
- // Variabili di istanza:
- private String key, value;
- // Costruttore di default
- public Tuple()
- // REQUIRE : ---
- // EFFECTS : inizializza this come tupla vuota.
- {
- this.key = "";
- this.value = "";
- }
- // Costruttore con parametri
- public Tuple(String k, String v)
- // REQUIRE : ---
- // EFFECTS : inizializza this con i parametri di input.
- {
- this.key = k;
- this.value = v;
- }
- // Metdodi :
- public void setKey(String k) throws NullPointerException
- // REQUIRE : k è una stringa non nulla && this != null
- // EFFECTS : imposta l'elemento chiave con il
- // parametro k, se this è nullo o k è nullo lancia un' eccezione
- {
- if((k==null)||(this==null)) throw new NullPointerException("@setKey");
- this.key = k;
- }
- public void setValue(String v) throws NullPointerException
- // REQUIRE : v è una stringa non nulla && this != null
- // EFFECTS : imposta l'elemento valore con il
- // parametro v, se this è nullo o v è nullo lancia un' eccezione
- {
- if((v==null)||(this==null)) throw new NullPointerException("@setValue");
- this.value = v;
- }
- public String getKey() throws NullPointerException
- // REQUIRE : this != null
- // EFFECTS : restituisce una stringa contenente l'elemento chiave
- // o lancia un'eccezione nel caso in cui this==null o this.key == null
- {
- if((this==null)||(this.key==null)) throw new NullPointerException("@getKey");
- return this.key;
- }
- public String getValue() throws NullPointerException
- // REQUIRE : this != null
- // EFFECTS : restituisce una stringa contenente l'elemento valore
- // o lancia un'eccezione nel caso in cui this==null o this.value == null
- {
- if((this==null)||(this.value==null)) throw new NullPointerException("@getValue");
- return this.value;
- }
- public int compareTo(Tuple t) throws NullPointerException
- // REQUIRE : this != null && t != null
- // EFFECTS : restituisce un intero negativo se this.key precede
- // lessicograficamente t.key, positivo se segue o zero se sono
- // identici. Nel caso in cui uno degli elementi sopra citati o gli
- // oggetti stessi siano nulli, lancia un'eccezione.
- {
- if((this==null)||(t==null)) throw new NullPointerException("@compareTo");
- if((this.key==null)||(t.key==null)) throw new NullPointerException("@compareTo");
- return this.key.compareTo(t.key);
- }
- // Metodo di astrazione:
- public String toString() throws NullPointerException
- // REQUIRE : this != null
- // EFFECTS : restituisce una stringa contenente la rappresentazione
- // grafica della tupla come specificato nell'overview oppure lancia
- // un'eccezione se this == null.
- {
- if(this==null) throw new NullPointerException("@toString");
- if((this.getKey()=="")&&(this.getValue()=="")) return "[]";
- return "["+this.getKey()+"->"+this.getValue()+"]";
- }
- // Metodo di controllo dell'invariante di rappresentazione:
- public boolean repOk() throws NullPointerException
- // REQUIRE : this != null
- // EFFECTS : restituisce true se l'invariante è soddisfatto
- // false altrimenti oppure lancia
- // un'eccezione se this==null.
- {
- if(this==null) throw new NullPointerException("@repOk");
- return ((this.getKey()!=null)&&(this.getValue()!=null));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement