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 <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public final class PropertyEditorFactory {
36      @SuppressWarnings("unchecked")
37      public static PropertyEditor getInstance(Object object) {
38          if (object == null) {
39              return new NullEditor();
40          }
41  
42          if (object instanceof Collection<?>) {
43              Class<?> elementType = null;
44              
45              for (Object e : (Collection<Object>) 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              
69              for (Object entry : ((Map<?,?>) object).entrySet()) {
70                  Map.Entry<?,?> e = (Map.Entry<?,?>) entry;
71                  
72                  if ((e.getKey() != null) && (e.getValue() != null)) {
73                      keyType = e.getKey().getClass();
74                      valueType = e.getValue().getClass();
75                      break;
76                  }
77              }
78  
79              if ((keyType != null) && (valueType != null)) {
80                  return new MapEditor(keyType, valueType);
81              }
82          }
83  
84          return getInstance(object.getClass());
85      }
86  
87      // parent type / property name / property type
88      public static PropertyEditor getInstance(Class<?> type) {
89          if (type == null) {
90              throw new IllegalArgumentException("type");
91          }
92  
93          if (type.isEnum()) {
94              return new EnumEditor(type);
95          }
96  
97          if (type.isArray()) {
98              return new ArrayEditor(type.getComponentType());
99          }
100 
101         if (Collection.class.isAssignableFrom(type)) {
102             if (Set.class.isAssignableFrom(type)) {
103                 return new SetEditor(String.class);
104             }
105 
106             if (List.class.isAssignableFrom(type)) {
107                 return new ListEditor(String.class);
108             }
109 
110             return new CollectionEditor(String.class);
111         }
112 
113         if (Map.class.isAssignableFrom(type)) {
114             return new MapEditor(String.class, String.class);
115         }
116 
117         if (Properties.class.isAssignableFrom(type)) {
118             return new PropertiesEditor();
119         }
120 
121         type = filterPrimitiveType(type);
122 
123         try {
124             return (PropertyEditor) PropertyEditorFactory.class
125                     .getClassLoader()
126                     .loadClass(
127                             PropertyEditorFactory.class.getPackage().getName() + '.' + type.getSimpleName() + "Editor")
128                     .newInstance();
129         } catch (Exception e) {
130             return null;
131         }
132     }
133 
134     private static Class<?> filterPrimitiveType(Class<?> type) {
135         if (type.isPrimitive()) {
136             if (type == boolean.class) {
137                 type = Boolean.class;
138             }
139             if (type == byte.class) {
140                 type = Byte.class;
141             }
142             if (type == char.class) {
143                 type = Character.class;
144             }
145             if (type == double.class) {
146                 type = Double.class;
147             }
148             if (type == float.class) {
149                 type = Float.class;
150             }
151             if (type == int.class) {
152                 type = Integer.class;
153             }
154             if (type == long.class) {
155                 type = Long.class;
156             }
157             if (type == short.class) {
158                 type = Short.class;
159             }
160         }
161         return type;
162     }
163 
164     private PropertyEditorFactory() {
165     }
166 }