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              for (Object e: (Collection) object) {
45                  if (e != null) {
46                      elementType = e.getClass();
47                      break;
48                  }
49              }
50              
51              if (elementType != null) {
52                  if (object instanceof Set) {
53                      return new SetEditor(elementType);
54                  }
55                  
56                  if (object instanceof List) {
57                      return new ListEditor(elementType);
58                  }
59                  
60                  return new CollectionEditor(elementType);
61              }
62          }
63          
64          if (object instanceof Map) {
65              Class<?> keyType = null;
66              Class<?> valueType = null;
67              for (Object entry: ((Map) object).entrySet()) {
68                  Map.Entry e = (Map.Entry) entry;
69                  if (e.getKey() != null && e.getValue() != null) {
70                      keyType = e.getKey().getClass();
71                      valueType = e.getValue().getClass();
72                      break;
73                  }
74              }
75              
76              if (keyType != null && valueType != null) {
77                  return new MapEditor(keyType, valueType);
78              }
79          }
80          
81          return getInstance(object.getClass());
82      }
83      
84      // parent type / property name / property type
85      public static PropertyEditor getInstance(Class<?> type) {
86          if (type == null) {
87              throw new NullPointerException("type");
88          }
89          
90          if (type.isEnum()) {
91              return new EnumEditor(type);
92          }
93          
94          if (type.isArray()) {
95              return new ArrayEditor(type.getComponentType());
96          }
97          
98          if (Collection.class.isAssignableFrom(type)) {
99              if (Set.class.isAssignableFrom(type)) {
100                 return new SetEditor(String.class);
101             }
102             
103             if (List.class.isAssignableFrom(type)) {
104                 return new ListEditor(String.class);
105             }
106             
107             return new CollectionEditor(String.class);
108         }
109         
110         if (Map.class.isAssignableFrom(type)) {
111             return new MapEditor(String.class, String.class);
112         }
113         
114         if (Properties.class.isAssignableFrom(type)) {
115             return new PropertiesEditor();
116         }
117         
118         type = filterPrimitiveType(type);
119 
120         try {
121             return (PropertyEditor)
122                     PropertyEditorFactory.class.getClassLoader().loadClass(
123                             PropertyEditorFactory.class.getPackage().getName() +
124                             '.' + type.getSimpleName() + "Editor").newInstance();
125         } catch (Exception e) {
126             return null;
127         }
128     }
129     
130     private static Class<?> filterPrimitiveType(Class<?> type) {
131         if (type.isPrimitive()) {
132             if (type == boolean.class) {
133                 type = Boolean.class;
134             }
135             if (type == byte.class) {
136                 type = Byte.class;
137             }
138             if (type == char.class) {
139                 type = Character.class;
140             }
141             if (type == double.class) {
142                 type = Double.class;
143             }
144             if (type == float.class) {
145                 type = Float.class;
146             }
147             if (type == int.class) {
148                 type = Integer.class;
149             }
150             if (type == long.class) {
151                 type = Long.class;
152             }
153             if (type == short.class) {
154                 type = Short.class;
155             }
156         }
157         return type;
158     }
159     
160     private PropertyEditorFactory() {
161     }
162 }