1 /*
2 * Copyright 2004-2023 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.mapping.result;
17
18 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
19
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 /**
25 * Created by IntelliJ IDEA. User: cbegin Date: May 13, 2005 Time: 11:11:05 PM To change this template use File |
26 * Settings | File Templates.
27 */
28 public class Discriminator {
29
30 /** The delegate. */
31 private SqlMapExecutorDelegate delegate;
32
33 /** The result mapping. */
34 private ResultMapping resultMapping;
35
36 /** The sub maps. */
37 private Map subMaps;
38
39 /**
40 * Instantiates a new discriminator.
41 *
42 * @param delegate
43 * the delegate
44 * @param resultMapping
45 * the result mapping
46 */
47 public Discriminator(SqlMapExecutorDelegate delegate, ResultMapping resultMapping) {
48 this.delegate = delegate;
49 this.resultMapping = resultMapping;
50 }
51
52 /**
53 * Sets the result mapping.
54 *
55 * @param resultMapping
56 * the new result mapping
57 */
58 public void setResultMapping(ResultMapping resultMapping) {
59 this.resultMapping = resultMapping;
60 }
61
62 /**
63 * Gets the result mapping.
64 *
65 * @return the result mapping
66 */
67 public ResultMapping getResultMapping() {
68 return resultMapping;
69 }
70
71 /**
72 * Adds the sub map.
73 *
74 * @param discriminatorValue
75 * the discriminator value
76 * @param resultMapName
77 * the result map name
78 */
79 public void addSubMap(String discriminatorValue, String resultMapName) {
80 if (subMaps == null) {
81 subMaps = new HashMap();
82 }
83 subMaps.put(discriminatorValue, resultMapName);
84 }
85
86 /**
87 * Gets the sub map.
88 *
89 * @param s
90 * the s
91 *
92 * @return the sub map
93 */
94 public ResultMap getSubMap(String s) {
95 return (ResultMap) subMaps.get(s);
96 }
97
98 /**
99 * Bind sub maps.
100 */
101 public void bindSubMaps() {
102 if (subMaps != null) {
103 Iterator keys = subMaps.keySet().iterator();
104 while (keys.hasNext()) {
105 Object key = keys.next();
106 Object id = subMaps.get(key);
107 if (id instanceof String) {
108 subMaps.put(key, delegate.getResultMap((String) id));
109 }
110 }
111 }
112 }
113
114 }