KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > Immutable


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import org.jboss.logging.Logger;
25
26 import java.util.List JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
31  * @version <tt>$Revision: 1958 $</tt>
32  */

33 public class Immutable
34 {
35    private static final Logger log = Logger.getLogger(Immutable.class);
36
37    public final Class JavaDoc cls;
38
39    final List JavaDoc names = new java.util.ArrayList JavaDoc();
40
41    final List JavaDoc values = new java.util.ArrayList JavaDoc();
42
43    public Immutable(Class JavaDoc cls)
44    {
45       this.cls = cls;
46       if(log.isTraceEnabled())
47       {
48          log.trace("created immutable container for " + cls);
49       }
50    }
51
52    public void addChild(String JavaDoc localName, Object JavaDoc child)
53    {
54       if(!names.isEmpty() && names.get(names.size() - 1).equals(localName))
55       {
56          throw new IllegalStateException JavaDoc("Attempt to add duplicate element " +
57             localName +
58             ": prev value=" +
59             values.get(values.size() - 1) +
60             ", new value=" +
61             child
62          );
63       }
64       names.add(localName);
65       values.add(child);
66
67       if(log.isTraceEnabled())
68       {
69          log.trace("added child " + localName + " for " + cls + ": " + child);
70       }
71    }
72
73    public Object JavaDoc getChild(String JavaDoc localName)
74    {
75       return names.isEmpty() ?
76          null :
77          (names.get(names.size() - 1).equals(localName) ? values.get(values.size() - 1) : null);
78    }
79
80    public Object JavaDoc newInstance()
81    {
82       Constructor JavaDoc ctor = null;
83       Constructor JavaDoc[] ctors = cls.getConstructors();
84
85       if(ctors == null || ctors.length == 0)
86       {
87          throw new JBossXBRuntimeException("The class has no declared constructors: " + cls);
88       }
89
90       for(int i = 0; i < ctors.length; ++i)
91       {
92          Class JavaDoc[] types = ctors[i].getParameterTypes();
93
94          if(types == null || types.length == 0)
95          {
96             throw new IllegalStateException JavaDoc("Found no-arg constructor for immutable " + cls);
97          }
98
99          if(types.length == values.size())
100          {
101             ctor = ctors[i];
102
103             int typeInd = 0;
104             while(typeInd < types.length)
105             {
106                if(!types[typeInd].isAssignableFrom(values.get(typeInd++).getClass()))
107                {
108                   ctor = null;
109                   break;
110                }
111             }
112
113             if(ctor != null)
114             {
115                break;
116             }
117          }
118       }
119
120       if(ctor == null)
121       {
122          throw new IllegalStateException JavaDoc("No constructor in " + cls + " that would take arguments " + values);
123       }
124
125       try
126       {
127          return ctor.newInstance(values.toArray());
128       }
129       catch(Exception JavaDoc e)
130       {
131          throw new IllegalStateException JavaDoc("Failed to create immutable instance of " +
132             cls +
133             " using arguments: "
134             + values + ": " + e.getMessage()
135          );
136       }
137    }
138 }
139
Popular Tags