KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > xml > factory > objects > CollectionObjectDescription


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------
28  * ObjectDescription.java
29  * ----------------------
30  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: CollectionObjectDescription.java,v 1.2 2005/10/18 13:31:58 mungady Exp $
36  *
37  * Changes
38  * -------------------------
39  * 06-May-2003 : Initial version
40  */

41 package org.jfree.xml.factory.objects;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 import org.jfree.util.Log;
48
49 /**
50  * An object description for simple collection objects, like java.util.List
51  * or java.util.Set.
52  *
53  * @author Thomas Morgner
54  */

55 public class CollectionObjectDescription extends AbstractObjectDescription {
56
57     /**
58      * Creates a list object description for the given collection class.
59      * <P>
60      * Throws <code>ClassCastException</code> if the given class is no collection instance.
61      *
62      * @param c the class of the collection implementation.
63      */

64     public CollectionObjectDescription(final Class JavaDoc c) {
65         super(c);
66         if (!Collection JavaDoc.class.isAssignableFrom(c)) {
67             throw new ClassCastException JavaDoc("The given class is no Collection instance");
68         }
69     }
70
71     /**
72      * Tries to parse the given parameter string into a positive integer.
73      * Returns -1 if the parsing failed for some reason.
74      *
75      * @param name the name of the parameter.
76      * @return the parsed int value or -1 on errors.
77      */

78     private int parseParameterName(final String JavaDoc name) {
79         try {
80             return Integer.parseInt(name);
81         }
82         catch (Exception JavaDoc e) {
83             return -1;
84         }
85     }
86
87     /**
88      * Returns a parameter definition. If the parameter is invalid, this
89      * function returns null.
90      *
91      * @param name the definition name.
92      *
93      * @return The parameter class or null, if the parameter is not defined.
94      */

95     public Class JavaDoc getParameterDefinition(final String JavaDoc name) {
96         if (name.equals("size")) {
97             return Integer.TYPE;
98         }
99         final int par = parseParameterName(name);
100         if (par < 0) {
101             return null;
102         }
103         return Object JavaDoc.class;
104     }
105
106     /**
107      * Returns an iterator for the parameter names.
108      *
109      * @return The iterator.
110      */

111     public Iterator JavaDoc getParameterNames() {
112         final Integer JavaDoc size = (Integer JavaDoc) getParameter("size");
113         if (size == null) {
114             return getDefinedParameterNames();
115         }
116         else {
117             final ArrayList JavaDoc l = new ArrayList JavaDoc();
118             l.add("size");
119             for (int i = 0; i < size.intValue(); i++) {
120                 l.add(String.valueOf(i));
121             }
122             return l.iterator();
123         }
124     }
125
126     /**
127      * Creates an object based on the description.
128      *
129      * @return The object.
130      */

131     public Object JavaDoc createObject() {
132         try {
133             final Collection JavaDoc l = (Collection JavaDoc) getObjectClass().newInstance();
134             int counter = 0;
135             while (getParameterDefinition(String.valueOf(counter)) != null) {
136                 final Object JavaDoc value = getParameter(String.valueOf(counter));
137                 if (value == null) {
138                     break;
139                 }
140
141                 l.add(value);
142                 counter += 1;
143             }
144             return l;
145         }
146         catch (Exception JavaDoc ie) {
147             Log.warn("Unable to instantiate Object", ie);
148             return null;
149         }
150     }
151
152     /**
153      * Sets the parameters of this description object to match the supplied object.
154      *
155      * @param o the object.
156      *
157      * @throws ObjectFactoryException if there is a problem while reading the
158      * properties of the given object.
159      */

160     public void setParameterFromObject(final Object JavaDoc o) throws ObjectFactoryException {
161         if (o == null) {
162             throw new NullPointerException JavaDoc("Given object is null");
163         }
164         final Class JavaDoc c = getObjectClass();
165         if (!c.isInstance(o)) {
166             throw new ObjectFactoryException("Object is no instance of " + c + "(is "
167                 + o.getClass() + ")");
168         }
169
170         final Collection JavaDoc l = (Collection JavaDoc) o;
171         final Iterator JavaDoc it = l.iterator();
172         int counter = 0;
173         while (it.hasNext()) {
174             final Object JavaDoc ob = it.next();
175             setParameter(String.valueOf(counter), ob);
176             counter++;
177         }
178     }
179 }
180
Popular Tags