View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999-2005 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.mapping.handlers;
37  
38  import org.exolab.castor.mapping.FieldHandler;
39  import org.exolab.castor.mapping.GeneralizedFieldHandler;
40  
41  import java.lang.reflect.Method;
42  import java.lang.reflect.Modifier;
43  
44  /**
45   * A specialized FieldHandler for the type-safe enum style classes.
46   * 
47   * Adapted from org.exolab.castor.xml.handlers.EnumFieldHandler which is used for the generated
48   * source code.
49   * 
50   * 
51   * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
52   * @version $Revision$ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
53   */
54  public class EnumFieldHandler extends GeneralizedFieldHandler {
55  
56  
57    /**
58     * Class type for the type-safe enum
59     */
60    private Class _enumType = null;
61  
62    /**
63     * The create method (eg: #valueOf(String))
64     */
65    private Method _createMethod = null;
66  
67    /**
68     * The underlying FieldHandler
69     */
70    private FieldHandler _handler = null;
71  
72  
73    /**
74     * Creates a new EnumFieldHandler with the given type and FieldHandler
75     *
76     * @param enumType the Class type of the described field
77     * @param handler the FieldHandler to delegate to
78     */
79    public EnumFieldHandler(Class enumType, FieldHandler handler, Method createMethod) {
80  
81      if (enumType == null) {
82        String err = "The argument 'enumType' must not be null.";
83        throw new IllegalArgumentException(err);
84      }
85  
86      if (handler == null) {
87        String err = "The argument 'handler' must not be null.";
88        throw new IllegalArgumentException(err);
89      }
90  
91      if (createMethod == null) {
92        String err = "The argument 'createMethod' must not be null.";
93        throw new IllegalArgumentException(err);
94      }
95  
96      _handler = handler;
97  
98      // -- pass handler to superclass
99      setFieldHandler(handler);
100 
101     _enumType = enumType;
102     _createMethod = createMethod;
103 
104     int mods = createMethod.getModifiers();
105 
106     if (!Modifier.isStatic(mods)) {
107       String err =
108           "The factory create method specified for " + enumType.getName() + " must be static";
109       throw new IllegalArgumentException(err);
110     }
111 
112   } // -- EnumFieldHandler
113 
114   /**
115    * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponGet(java.lang.Object)
116    */
117   public Object convertUponGet(Object value) {
118     // -- no conversion for getter
119     return value;
120 
121   } // -- getValue
122 
123   /**
124    * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponSet(java.lang.Object)
125    */
126   public Object convertUponSet(Object value) throws java.lang.IllegalStateException {
127     Object obj = null;
128     if (value != null) {
129       Object[] args = new String[1];
130       args[0] = value.toString();
131       try {
132         obj = _createMethod.invoke(null, args);
133       } catch (java.lang.reflect.InvocationTargetException ite) {
134         Throwable toss = ite.getTargetException();
135         throw new IllegalStateException(toss.toString());
136       } catch (java.lang.IllegalAccessException iae) {
137         throw new IllegalStateException(iae.toString());
138       }
139     }
140     return obj;
141 
142   } // -- setValue
143 
144 
145   /**
146    * @see org.exolab.castor.mapping.GeneralizedFieldHandler#getFieldType()
147    */
148   public Class getFieldType() {
149     return _enumType;
150   }
151 
152 
153   /**
154    * @see org.exolab.castor.mapping.FieldHandler#newInstance(java.lang.Object)
155    */
156   public Object newInstance(Object parent) throws IllegalStateException {
157     return "";
158   } // -- newInstance
159 
160   /**
161    * @see org.exolab.castor.mapping.ExtendedFieldHandler#newInstance(java.lang.Object,
162    *      java.lang.Object[])
163    */
164   public Object newInstance(Object parent, Object[] args) throws IllegalStateException {
165     return "";
166   }
167 
168 
169   /**
170    * Returns true if the given object is an XMLFieldHandler that is equivalent to the delegated
171    * handler. An equivalent XMLFieldHandler is an XMLFieldHandler that is an instances of the same
172    * class.
173    *
174    * @return true if the given object is an XMLFieldHandler that is equivalent to this one.
175    **/
176   public boolean equals(Object obj) {
177     if (obj == null)
178       return false;
179     if (obj == this)
180       return true;
181     if (!(obj instanceof FieldHandler))
182       return false;
183     return (_handler.getClass().isInstance(obj) || getClass().isInstance(obj));
184   } // -- equals
185 
186 } // -- EnumFieldHandler
187 
188