Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: it/rabellino/toska/User.java


1   package it.rabellino.toska;
2   
3   import java.util.HashMap;
4   import java.util.NoSuchElementException;
5   
6   /**
7    * A class representing a user. Contains key references.
8    * 
9    * @author <a href="gianugo@apache.org">Gianugo Rabellino</a>
10   *
11   */
12  public class User {
13  
14    protected String name;
15    protected HashMap keys;
16  
17    /**
18     * Constructor for User.
19     */
20    public User(String name) {
21      this.name=name;  
22      keys = new HashMap();
23    }
24  
25    /**
26     * Gets the name.
27     * @return Returns a String
28     */
29    public String getName() {
30      return name;
31    }
32  
33    /**
34     * Sets the name.
35     * @param name The name to set
36     */
37    public void setName(String name) {
38      this.name = name;
39    }
40  
41    /**
42     * Gets the keys.
43     * @return Returns a HashMap
44     */
45    public HashMap getKeys() {
46      return keys;
47    }
48  
49    /**
50     * Sets the keys.
51     * @param keys The keys to set
52     */
53    public void setKeys(HashMap keys) {
54      this.keys = keys;
55    }
56    
57    /**
58     * Adds a key.
59     * @param key
60     * @throws DuplicateKeyException
61     */
62    public void addKey(Key key) throws DuplicateKeyForUserException {
63      if (keys.containsKey(key))
64        throw new DuplicateKeyForUserException("Key already exists for this user: " +
65          key.toString());
66      keys.put(key.toString(), key);          
67    }
68    
69    public void removeKey(Key key) throws NoSuchElementException {
70      if (keys.remove(key.toString()) == null)
71        throw new NoSuchElementException("Not existing key for this user" +
72          key.toString());
73    }
74  
75    /**
76     * @see Object#equals(Object)
77     */
78    public boolean equals(Object obj) {
79      if (obj instanceof User) 
80        return ((User)obj).getName().equals(this.getName());      
81      else if (obj instanceof String)
82        return this.getName().equals((String)obj);
83        
84      return false;
85    }
86  
87    /**
88     * @see Object#toString()
89     */
90    public String toString() {
91      return this.getName();
92    }
93    
94  
95  }