KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > pmti > v1 > defimpl > DefaultListAttribute


1 /*
2  * @(#)DefaultListAttribute.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.pmti.v1.defimpl;
30
31 import net.sourceforge.groboutils.pmti.v1.IListAttribute;
32 import net.sourceforge.groboutils.pmti.v1.IAttributeInfo;
33
34 import java.util.Enumeration JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36
37
38 /**
39  * A specialization of an attribute which contains a list of values.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2003/02/10 22:51:59 $
43  * @since July 12, 2002
44  */

45 public class DefaultListAttribute implements IListAttribute
46 {
47     // don't use a vector for memory and synchronization efficiency
48
private Object JavaDoc[] obj;
49     private IAttributeInfo info;
50     
51     
52     public DefaultListAttribute( Object JavaDoc[] o, IAttributeInfo i )
53     {
54         if (i == null)
55         {
56             throw new IllegalArgumentException JavaDoc("no null arguments");
57         }
58         this.obj = copyObj( o );
59         this.info = i;
60     }
61     
62     
63     
64     /**
65      * Returns the enumeration for all values in this attribute.
66      */

67     public Object JavaDoc getValue()
68     {
69         return getValues();
70     }
71     
72     
73     /**
74      * Returns the meta-information for this attribute.
75      */

76     public IAttributeInfo getInfo()
77     {
78         return this.info;
79     }
80     
81     
82     /**
83      * A synonym for <tt>getValue()</tt>, but redefined here to explicitly
84      * declare the returned type.
85      */

86     public Enumeration JavaDoc getValues()
87     {
88         // don't need to copy the object array - it's immutable!
89
return new ArrayEnum( this.obj );
90     }
91     
92     
93     /**
94      *
95      */

96     public int getValueCount()
97     {
98         return this.obj.length;
99     }
100     
101     
102     /**
103      *
104      */

105     public boolean containsValue( Object JavaDoc value )
106     {
107         for (int i = 0; i < this.obj.length; ++i)
108         {
109             Object JavaDoc o = this.obj[i];
110             if (value == null)
111             {
112                 if (o == null)
113                 {
114                     return true;
115                 }
116             }
117             else
118             if (value.equals( o ))
119             {
120                 return true;
121             }
122         }
123         return false;
124     }
125     
126     
127     
128     private Object JavaDoc[] copyObj( Object JavaDoc[] o )
129     {
130         if (o == null)
131         {
132             throw new IllegalArgumentException JavaDoc("no null arguments");
133         }
134         Object JavaDoc ret[] = new Object JavaDoc[ o.length ];
135         System.arraycopy( o, 0, ret, 0, o.length );
136         return ret;
137     }
138     
139     
140     private static class ArrayEnum implements Enumeration JavaDoc
141     {
142         private Object JavaDoc vals[];
143         private int index;
144         public ArrayEnum( Object JavaDoc o[] )
145         {
146             this.vals = o;
147             this.index = 0;
148         }
149         
150         
151         public boolean hasMoreElements()
152         {
153             return (this.index < this.vals.length);
154         }
155         
156         
157         public Object JavaDoc nextElement()
158         {
159             if (this.index >= this.vals.length)
160             {
161                 throw new NoSuchElementException JavaDoc();
162             }
163             Object JavaDoc ret = this.vals[ this.index ];
164             ++this.index;
165             return ret;
166         }
167     }
168 }
169
170
Popular Tags