View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   * 
18   * $Id: AttribsData.java,v 1.12 2006/01/12 11:53:12 catoolsen Exp $
19   */
20  
21  package no.feide.moria.servlet;
22  
23  import java.util.HashMap;
24  import java.util.Comparator;
25  
26  
27  /***
28   *
29   * @author Eva Indal
30   * @version $Revision: 1.12 $
31   *
32   * Stores data for all possible user attributes registered. The attributes
33   * are read from an xml file to make it easy to add and change the attributes.
34   *
35   * The attribute is identified by the "key" keyword, which stores the attribute
36   * name, but each attribute has other data as well. See the attribute
37   * description file for more information.
38   *
39   */
40  
41  public class AttribsData implements Comparator {
42  
43      /*** Used for sorting the attributes. */
44      private int idx;
45  
46      /*** Used for storing all info about a attribute. */
47      private HashMap hashmap;
48  
49      /***
50       * Constructor.
51       * @param index order in xml file.
52       */
53      public AttribsData(final int index) {
54         idx = index;
55         hashmap = new HashMap();
56      }
57  
58      /***
59       * Implements Comparator.compare to be able to sort attributes based on xml file order.
60       *
61       * @param ad1 The first object.
62       * @param ad2 The second object.
63       * @return < 0 if object 1 index is less than object 2, > 0 if object 2 index i larger.
64       * @see Comparator#compare
65       */
66      public final int compare(final Object ad1, final Object ad2) {
67          AttribsData adata1 = (AttribsData) ad1;
68          AttribsData adata2 = (AttribsData) ad2;
69          return adata1.idx - adata2.idx;
70      }
71  
72      /***
73       * Implements Comparator.equals.
74       *
75       * @param ad Object to compare.
76       * @return true if equal.
77       */
78      public final boolean equals(final Object ad) {
79          AttribsData adata = (AttribsData) ad;
80          return idx == adata.idx;
81      }
82  
83      /***
84       * Generates a hashCode.
85       *
86       * @return The hashcode.
87       */
88      public final int hashCode() {
89          return idx;
90      }
91  
92      /***
93       * Adds info/data for an attribute.
94       *
95       * @param name The name of the information.
96       * @param data The actual data.
97       * @throws IllegalArgumentException
98       *          If name or data is null or zero length.
99       */
100     public final void addData(final String name, final String data) {
101         if (name == null || name.equals("")) {
102             throw new IllegalArgumentException("name must be a non-empty string.");
103         }
104         if (data == null || data.equals("")) {
105             throw new IllegalArgumentException("data must be a non-empty string.");
106         }
107         hashmap.put(name, data);
108     }
109     /***
110      * Returns data for an attribute.
111      *
112      * @param name the name of the infotmation to return
113      * @return the data associated with name
114      * @throws IllegalArgumentException
115      *          If name is null or zero length.
116      */
117     public final String getData(final String name) {
118         if (name == null || name.equals("")) {
119             throw new IllegalArgumentException("name must be a non-empty string.");
120         }
121         return (String) hashmap.get(name);
122     }
123 }