~ubuntu-branches/ubuntu/lucid/libjcommon-java/lucid

« back to all changes in this revision

Viewing changes to source/org/jfree/xml/factory/objects/CollectionObjectDescription.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2006-02-09 15:58:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060209155813-fzi9zwh2rzedbllq
Tags: 1.0.0-1
* New stable upstream release (closes: #328574)
* Move to main - build with kaffe
* Use cdbs build system - added cdbs build-dependency
* Move package to pkg-java-maintainers for comaintenance, 
  added Christian Bayle and myself as uploaders
* Removed unneeded README.Debian
* Added README.Debian-source how the upstream tarball was cleaned
* Move big documentation in an own -doc package
* Register javadoc api with doc-base
* Standards-Version 3.6.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
44
import java.util.Collection;
 
45
import java.util.Iterator;
 
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 c) {
 
65
        super(c);
 
66
        if (!Collection.class.isAssignableFrom(c)) {
 
67
            throw new ClassCastException("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 name) {
 
79
        try {
 
80
            return Integer.parseInt(name);
 
81
        }
 
82
        catch (Exception 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 getParameterDefinition(final String 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.class;
 
104
    }
 
105
 
 
106
    /**
 
107
     * Returns an iterator for the parameter names.
 
108
     *
 
109
     * @return The iterator.
 
110
     */
 
111
    public Iterator getParameterNames() {
 
112
        final Integer size = (Integer) getParameter("size");
 
113
        if (size == null) {
 
114
            return getDefinedParameterNames();
 
115
        }
 
116
        else {
 
117
            final ArrayList l = new ArrayList();
 
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 createObject() {
 
132
        try {
 
133
            final Collection l = (Collection) getObjectClass().newInstance();
 
134
            int counter = 0;
 
135
            while (getParameterDefinition(String.valueOf(counter)) != null) {
 
136
                final Object 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 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 o) throws ObjectFactoryException {
 
161
        if (o == null) {
 
162
            throw new NullPointerException("Given object is null");
 
163
        }
 
164
        final Class 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 l = (Collection) o;
 
171
        final Iterator it = l.iterator();
 
172
        int counter = 0;
 
173
        while (it.hasNext()) {
 
174
            final Object ob = it.next();
 
175
            setParameter(String.valueOf(counter), ob);
 
176
            counter++;
 
177
        }
 
178
    }
 
179
}