1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder;
17
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.regex.Pattern;
22
23 import org.apache.ibatis.mapping.ParameterMode;
24 import org.apache.ibatis.mapping.ResultSetType;
25 import org.apache.ibatis.session.Configuration;
26 import org.apache.ibatis.type.JdbcType;
27 import org.apache.ibatis.type.TypeAliasRegistry;
28 import org.apache.ibatis.type.TypeHandler;
29 import org.apache.ibatis.type.TypeHandlerRegistry;
30
31
32
33
34 public abstract class BaseBuilder {
35 protected final Configuration configuration;
36 protected final TypeAliasRegistry typeAliasRegistry;
37 protected final TypeHandlerRegistry typeHandlerRegistry;
38
39 public BaseBuilder(Configuration configuration) {
40 this.configuration = configuration;
41 this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
42 this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
43 }
44
45 public Configuration getConfiguration() {
46 return configuration;
47 }
48
49 protected Pattern parseExpression(String regex, String defaultValue) {
50 return Pattern.compile(regex == null ? defaultValue : regex);
51 }
52
53 protected Boolean booleanValueOf(String value, Boolean defaultValue) {
54 return value == null ? defaultValue : Boolean.valueOf(value);
55 }
56
57 protected Integer integerValueOf(String value, Integer defaultValue) {
58 return value == null ? defaultValue : Integer.valueOf(value);
59 }
60
61 protected Set<String> stringSetValueOf(String value, String defaultValue) {
62 value = value == null ? defaultValue : value;
63 return new HashSet<>(Arrays.asList(value.split(",")));
64 }
65
66 protected JdbcType resolveJdbcType(String alias) {
67 try {
68 return alias == null ? null : JdbcType.valueOf(alias);
69 } catch (IllegalArgumentException e) {
70 throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
71 }
72 }
73
74 protected ResultSetType resolveResultSetType(String alias) {
75 try {
76 return alias == null ? null : ResultSetType.valueOf(alias);
77 } catch (IllegalArgumentException e) {
78 throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
79 }
80 }
81
82 protected ParameterMode resolveParameterMode(String alias) {
83 try {
84 return alias == null ? null : ParameterMode.valueOf(alias);
85 } catch (IllegalArgumentException e) {
86 throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
87 }
88 }
89
90 protected Object createInstance(String alias) {
91 Class<?> clazz = resolveClass(alias);
92 try {
93 return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();
94 } catch (Exception e) {
95 throw new BuilderException("Error creating instance. Cause: " + e, e);
96 }
97 }
98
99 protected <T> Class<? extends T> resolveClass(String alias) {
100 try {
101 return alias == null ? null : resolveAlias(alias);
102 } catch (Exception e) {
103 throw new BuilderException("Error resolving class. Cause: " + e, e);
104 }
105 }
106
107 protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
108 if (typeHandlerAlias == null) {
109 return null;
110 }
111 Class<?> type = resolveClass(typeHandlerAlias);
112 if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
113 throw new BuilderException(
114 "Type " + type.getName() + " is not a valid TypeHandler because it does not implement TypeHandler interface");
115 }
116 @SuppressWarnings("unchecked")
117 Class<? extends TypeHandler<?>> typeHandlerType = (Class<? extends TypeHandler<?>>) type;
118 return resolveTypeHandler(javaType, typeHandlerType);
119 }
120
121 protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
122 if (typeHandlerType == null) {
123 return null;
124 }
125
126 TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
127
128 return handler == null ? typeHandlerRegistry.getInstance(javaType, typeHandlerType) : handler;
129 }
130
131 protected <T> Class<? extends T> resolveAlias(String alias) {
132 return typeAliasRegistry.resolveAlias(alias);
133 }
134 }