xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/base/AttributeList.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /*
30  *        Copyright (C) 1996  Active Software, Inc.
31  *                  All rights reserved.
32  *
33  * @(#) AttributeList.java 1.31 - last change made 07/25/97
34  */
35 
36 package sunsoft.jws.visual.rt.base;
37 
38 import java.util.Hashtable;
39 import java.util.Vector;
40 import java.util.Enumeration;
41 
42 /**
43  * Class for storing information about the attributes available on a
44  * particular type of GUI object.
45  *
46  * @version 	1.31, 07/25/97
47  */
48 public class AttributeList implements Cloneable {
49     private Hashtable table;
50     private Hashtable aliasTable;
51 
AttributeList()52     public AttributeList() {
53         table = new Hashtable();
54         aliasTable = new Hashtable();
55     }
56 
add(Attribute attr)57     public void add(Attribute attr) {
58         table.put(attr.getName(), attr);
59     }
60 
add(String name, String type, Object defaultValue)61     public void add(String name, String type, Object defaultValue) {
62         Attribute a = lookup(name);
63         if (a == null)
64             add(new Attribute(name, type, defaultValue, 0));
65         else
66             add(new Attribute(name, type, defaultValue, a.getFlags()));
67     }
68 
add(String name, String type, Object defaultValue, int flags)69     public void add(String name, String type, Object defaultValue,
70 		    int flags) {
71         table.put(name, (new Attribute(name, type, defaultValue,
72 				       flags)));
73     }
74 
alias(String name1, String name2)75     public void alias(String name1, String name2) {
76         aliasTable.put(name1, name2);
77     }
78 
aliasKeys()79     public Enumeration aliasKeys() {
80         return aliasTable.keys();
81     }
82 
resolveAlias(String name)83     public String resolveAlias(String name) {
84         String s1, s2;
85 
86         s1 = name;
87         s2 = s1;
88         while (s1 != null) {
89             s2 = s1;
90             s1 = (String)aliasTable.get(s1);
91         }
92 
93         return s2;
94     }
95 
remove(String name)96     public void remove(String name) {
97         table.remove(name);
98         aliasTable.remove(name);
99     }
100 
lookup(String name)101     private Attribute lookup(String name) {
102         Attribute attr = (Attribute)table.get(name);
103         if (attr == null) {
104             name = resolveAlias(name);
105             attr = (Attribute)table.get(name);
106         }
107         return attr;
108     }
109 
get(String name)110     public Attribute get(String name) {
111         return lookup(name);
112     }
113 
contains(String name)114     public boolean contains(String name) {
115         return (lookup(name) != null);
116     }
117 
118     /**
119      * Returns an enumeration of attributes with the given flags.
120      */
attributesWithFlags(int flags)121     public Enumeration attributesWithFlags(int flags) {
122         Attribute s;
123         Vector set = new Vector();
124         for (Enumeration e = table.elements(); e.hasMoreElements(); ) {
125             s = (Attribute) e.nextElement();
126             if ((s.getFlags() & flags) != 0) {
127                 set.addElement(s);
128             }
129         }
130         return (set.elements());
131     }
132 
133     /**
134      * Returns an enumeration of attributes that do not have
135      * the given flags.
136      */
attributesWithoutFlags(int flags)137     public Enumeration attributesWithoutFlags(int flags) {
138         Attribute s;
139         Vector set = new Vector();
140         for (Enumeration e = table.elements(); e.hasMoreElements(); ) {
141             s = (Attribute) e.nextElement();
142             if ((s.getFlags() & flags) == 0) {
143                 set.addElement(s);
144             }
145         }
146         return (set.elements());
147     }
148 
149     /**
150      * Returns an enumeration of all elements in the attribute list.
151      */
elements()152     public Enumeration elements() {
153         return (table.elements());
154     }
155 
156     /**
157      * Returns the number of elements in the attribute list.
158      */
size()159     public int size() {
160         return (table.size());
161     }
162 
163     /**
164      * Merge the contents of the given list with this one.  New elements
165      * by the same name will overwrite old ones.  Attributes are cloned
166      * before merging with this list.
167      */
merge(AttributeList list)168     public void merge(AttributeList list) {
169         if (list != null) {
170             for (Enumeration e = list.elements(); e.hasMoreElements(); )
171 
172 		{
173 		    add((Attribute) ((Attribute) e.nextElement()).clone());
174 		}
175             for (Enumeration e = list.aliasKeys();
176 		 /* JSTYLED */
177 		 e.hasMoreElements(); ) {
178 		String s = (String)e.nextElement();
179 		alias(s, list.resolveAlias(s));
180 	    }
181 	}
182     }
183 
184     /**
185      * Returns an alphabetized enumeration of attributes.
186      */
alphabetize(Enumeration e)187     public static Enumeration alphabetize(Enumeration e) {
188 	Vector v = new Vector();
189 	for (; e.hasMoreElements(); ) {
190 	    int i;
191 	    Attribute a = (Attribute) e.nextElement();
192 	    for (i = 0; i < v.size(); i++) {
193 		if (((Attribute)
194 		     v.elementAt(i)).getName().compareTo(a.getName()) > 0)
195 
196 		    {
197 			v.insertElementAt(a, i);
198 			break;
199 		    }
200 	    }
201 	    if (i >= v.size()) {
202 		v.addElement(a);
203 	    }
204 	}
205 	return (v.elements());
206     }
207 
208     /**
209      * Clones the attribute list.  The attributes in the list
210      * are cloned, but the attribute's value is not cloned (see
211      * the comment for the clone method in Attribute).
212      *
213      * @see Attribute
214      */
clone()215     public Object clone() {
216 	AttributeList retval = new AttributeList();
217 	retval.merge(this);
218 	return (retval);
219     }
220 
toString()221     public String toString() {
222 	StringBuffer buf = new StringBuffer();
223 
224 	for (Enumeration e = table.keys(); e.hasMoreElements(); ) {
225 	    String key = (String) e.nextElement();
226 	    buf.append(key + /* NOI18N */"(" + ((Attribute)
227 				table.get(key)).getType() + /* NOI18N */") ");
228 	}
229 
230 	for (Enumeration e = aliasTable.keys(); e.hasMoreElements(); ) {
231 	    String alias = (String) e.nextElement();
232 	    buf.append(/* NOI18N */"alias:" + alias + /* NOI18N */"->"
233 		       + (String) aliasTable.get(alias) + /* NOI18N */" ");
234 	}
235 
236 	return (buf.toString());
237     }
238 }
239