View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.exchange;
17  
18  import com.ibatis.sqlmap.engine.type.DomTypeMarker;
19  import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
20  
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * Factory for DataExchange objects.
26   */
27  public class DataExchangeFactory {
28  
29    /** The dom data exchange. */
30    private final DataExchange domDataExchange;
31  
32    /** The list data exchange. */
33    private final DataExchange listDataExchange;
34  
35    /** The map data exchange. */
36    private final DataExchange mapDataExchange;
37  
38    /** The primitive data exchange. */
39    private final DataExchange primitiveDataExchange;
40  
41    /** The complex data exchange. */
42    private final DataExchange complexDataExchange;
43  
44    /** The type handler factory. */
45    private TypeHandlerFactory typeHandlerFactory;
46  
47    /**
48     * Constructor for the factory.
49     *
50     * @param typeHandlerFactory
51     *          - a type handler factory for the factory
52     */
53    public DataExchangeFactory(TypeHandlerFactory typeHandlerFactory) {
54      this.typeHandlerFactory = typeHandlerFactory;
55      domDataExchange = new DomDataExchange(this);
56      listDataExchange = new ListDataExchange(this);
57      mapDataExchange = new ComplexDataExchange(this);
58      primitiveDataExchange = new PrimitiveDataExchange(this);
59      complexDataExchange = new ComplexDataExchange(this);
60    }
61  
62    /**
63     * Getter for the type handler factory.
64     *
65     * @return - the type handler factory
66     */
67    public TypeHandlerFactory getTypeHandlerFactory() {
68      return typeHandlerFactory;
69    }
70  
71    /**
72     * Get a DataExchange object for the passed in Class.
73     *
74     * @param clazz
75     *          - the class to get a DataExchange object for
76     *
77     * @return - the DataExchange object
78     */
79    public DataExchange getDataExchangeForClass(Class clazz) {
80      DataExchange dataExchange = null;
81      if (clazz == null) {
82        dataExchange = complexDataExchange;
83      } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
84        dataExchange = domDataExchange;
85      } else if (List.class.isAssignableFrom(clazz)) {
86        dataExchange = listDataExchange;
87      } else if (Map.class.isAssignableFrom(clazz)) {
88        dataExchange = mapDataExchange;
89      } else if (typeHandlerFactory.getTypeHandler(clazz) != null) {
90        dataExchange = primitiveDataExchange;
91      } else {
92        dataExchange = new JavaBeanDataExchange(this);
93      }
94      return dataExchange;
95    }
96  
97  }