View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.integration.beans;
21  
22  import java.beans.PropertyEditor;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  /**
30   * A factory that creates a new {@link PropertyEditor} which is appropriate for
31   * the specified object or class. 
32   * 
33   * @author The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 601247 $, $Date: 2007-12-05 09:42:41 +0100 (mer, 05 déc 2007) $
35   */
36  public final class PropertyEditorFactory {
37      @SuppressWarnings("unchecked")
38      public static PropertyEditor getInstance(Object object) {
39          if (object == null) {
40              return new NullEditor();
41          }
42          
43          if (object instanceof Collection) {
44              Class<?> elementType = null;
45              for (Object e: (Collection) object) {
46                  if (e != null) {
47                      elementType = e.getClass();
48                      break;
49                  }
50              }
51              
52              if (elementType != null) {
53                  if (object instanceof Set) {
54                      return new SetEditor(elementType);
55                  }
56                  
57                  if (object instanceof List) {
58                      return new ListEditor(elementType);
59                  }
60                  
61                  return new CollectionEditor(elementType);
62              }
63          }
64          
65          if (object instanceof Map) {
66              Class<?> keyType = null;
67              Class<?> valueType = null;
68              for (Object entry: ((Map) object).entrySet()) {
69                  Map.Entry e = (Map.Entry) entry;
70                  if (e.getKey() != null && e.getValue() != null) {
71                      keyType = e.getKey().getClass();
72                      valueType = e.getValue().getClass();
73                      break;
74                  }
75              }
76              
77              if (keyType != null && valueType != null) {
78                  return new MapEditor(keyType, valueType);
79              }
80          }
81          
82          return getInstance(object.getClass());
83      }
84      
85      // parent type / property name / property type
86      public static PropertyEditor getInstance(Class<?> type) {
87          if (type == null) {
88              throw new NullPointerException("type");
89          }
90          
91          if (type.isEnum()) {
92              return new EnumEditor(type);
93          }
94          
95          if (type.isArray()) {
96              return new ArrayEditor(type.getComponentType());
97          }
98          
99          if (Collection.class.isAssignableFrom(type)) {
100             if (Set.class.isAssignableFrom(type)) {
101                 return new SetEditor(String.class);
102             }
103             
104             if (List.class.isAssignableFrom(type)) {
105                 return new ListEditor(String.class);
106             }
107             
108             return new CollectionEditor(String.class);
109         }
110         
111         if (Map.class.isAssignableFrom(type)) {
112             return new MapEditor(String.class, String.class);
113         }
114         
115         if (Properties.class.isAssignableFrom(type)) {
116             return new PropertiesEditor();
117         }
118         
119         type = filterPrimitiveType(type);
120 
121         try {
122             return (PropertyEditor)
123                     PropertyEditorFactory.class.getClassLoader().loadClass(
124                             PropertyEditorFactory.class.getPackage().getName() +
125                             '.' + type.getSimpleName() + "Editor").newInstance();
126         } catch (Exception e) {
127             return null;
128         }
129     }
130     
131     private static Class<?> filterPrimitiveType(Class<?> type) {
132         if (type.isPrimitive()) {
133             if (type == boolean.class) {
134                 type = Boolean.class;
135             }
136             if (type == byte.class) {
137                 type = Byte.class;
138             }
139             if (type == char.class) {
140                 type = Character.class;
141             }
142             if (type == double.class) {
143                 type = Double.class;
144             }
145             if (type == float.class) {
146                 type = Float.class;
147             }
148             if (type == int.class) {
149                 type = Integer.class;
150             }
151             if (type == long.class) {
152                 type = Long.class;
153             }
154             if (type == short.class) {
155                 type = Short.class;
156             }
157         }
158         return type;
159     }
160     
161     private PropertyEditorFactory() {
162     }
163 }