View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.util;
25  
26  import java.util.*;
27  
28  public class ReferenceTable extends Hashtable {
29     protected String defaultReference = null;
30  
31     public ReferenceTable() {
32     }
33     public ReferenceTable(String defaultReference) {
34        Assert.notNull(defaultReference);
35        this.defaultReference = defaultReference;
36     }
37     public void add(String name, String reference) {
38        this.put(name,reference);
39     }
40     public void replace(String name, String reference) {
41        this.put(name,reference);
42     }
43     public void del(String name) {
44        this.remove(name);
45     }
46     public String lookup(String name) {
47        Assert.notNull(name);
48        String reference = (String)this.get(name);
49        if ( reference == null ) {
50           if ( defaultReference != null ) {
51              reference = defaultReference;
52              add(name,reference);
53           }
54        }
55        return reference;
56     }
57     public String lookup(String name, String otherwise) {
58        Assert.notNull(otherwise);
59        if ( name == null ) return otherwise;
60  
61        String reference = (String)this.get(name);
62        if ( reference == null ) {
63           reference = otherwise;
64           add(name,reference);
65        }
66        return reference;
67     }
68     public void set(Vector data) {
69        clear();
70        String name, reference;
71        for( int i = 0; data != null && i < data.size(); i += 2 ) {
72           name = (String)data.elementAt(i);
73           reference = (String)data.elementAt(i+1);
74           add(name,reference);
75        }
76     }
77     public String toString() {
78      Enumeration enum = this.keys();
79      String name, reference;
80      String s = new String();
81     
82      while( enum.hasMoreElements() ) {
83        name = (String) enum.nextElement();
84        reference = lookup(name);
85        s += name + " " + reference + " ";
86      }
87      s = s.trim();
88      return s;
89    }
90  
91    public String pprint() {
92      return pprint(0);
93    }
94  
95    public String pprint(int sp) {
96      String tabs = Misc.spaces(sp);
97      String eol  = "\n" + tabs;
98  
99      String s = new String();
100     Enumeration enum = this.keys();
101     String name, reference;
102    
103     while( enum.hasMoreElements() ) {
104       name = (String) enum.nextElement();
105       reference = lookup(name);
106       s += name + " " + reference + eol;
107     }
108     return s.trim();
109   }
110 
111   public static void main(String[] args) {
112      ReferenceTable t = new ReferenceTable("--DefaultName--");
113      t.add("divine","aaaaa");
114      t.add("john","bbbbb");
115      t.add("george","bbbbb");
116      String s = t.lookup("adam");
117      System.out.println(s);
118      System.out.println(t.pprint());
119   }
120      
121 }