1 package org.apache.torque.generator.option;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.torque.generator.qname.Namespace;
30 import org.apache.torque.generator.qname.QualifiedName;
31 import org.apache.torque.generator.qname.QualifiedNameMap;
32
33 /**
34 * A Store for all options.
35 */
36 public class Options
37 {
38 /** The options with global scope. */
39 private QualifiedNameMap<Option> globalScope
40 = new QualifiedNameMap<Option>();
41
42 /**
43 * Sets an option with global scope.
44 *
45 * @param option the option to set, not null.
46 *
47 * @throws NullPointerException if option is null.
48 */
49 public void setGlobalOption(Option option)
50 {
51 globalScope.put(option.getQualifiedName(), option);
52 }
53
54 /**
55 * Adds several options with global scope.
56 *
57 * @param options the option to add, not null, may not contain null.
58 *
59 * @throws NullPointerException if options is null or contains null.
60 */
61 public void addGlobalOptions(Collection<Option> options)
62 {
63 for (Option option : options)
64 {
65 setGlobalOption(option);
66 }
67 }
68
69 /**
70 * Returns the value of the option which is closest in Hierarchy.
71 * If more than one matching options (options with the matching name
72 * and in the name space or parent name space of the key) are found,
73 * the one which name space is closer to the given key's name space
74 * is chosen.
75 *
76 * @param key the key for the option which value should be retrieved.
77 *
78 * @return the value of the option (can be null), or null if no matching
79 * option exists.
80 */
81 public Option getInHierarchy(QualifiedName key)
82 {
83 QualifiedName globalKey = globalScope.getKeyInHierarchy(key);
84 Option globalOption = globalScope.get(globalKey);
85 return globalOption;
86 }
87
88 /**
89 * Returns all mappings which live in the given name space.
90 * If one mapping hides another mapping, i.e. if one mapping
91 * is a more specialized version of another, the hidden mapping
92 * is NOT returned.
93 *
94 * @param namespace the name space from which the returned options should
95 * be visible.
96 *
97 * @return an Options object containing the matching options
98 */
99 public Options getInHierarchy(Namespace namespace)
100 {
101 Options result = new Options();
102 result.globalScope = globalScope.getInHierarchy(namespace);
103 return result;
104 }
105
106 /**
107 * Returns all mappings which live in the given namespace.
108 * If one mapping hides another mapping, i.e. if one mapping
109 * is a more specialized version of another, both
110 * mappings are present in the returned map.
111 *
112 * @param namespace the name space from which the returned options should
113 * be visible.
114 *
115 * @return an Options object containing the matching options
116 */
117 public Options getAllInHierarchy(Namespace namespace)
118 {
119 Options result = new Options();
120 result.globalScope = globalScope.getAllInHierarchy(namespace);
121 return result;
122 }
123
124 /**
125 * Returns all options in a Collection.
126 *
127 * @return a Collection containing all options, not null.
128 * The collection is unmodifiable.
129 */
130 public Collection<Option> values()
131 {
132 Map<QualifiedName, Option> result
133 = new HashMap<QualifiedName, Option>();
134 for (Option globalOption : globalScope.values())
135 {
136 result.put(globalOption.getQualifiedName(), globalOption);
137 }
138 return Collections.unmodifiableCollection(result.values());
139 }
140
141 /**
142 * Returns all options in a set.
143 *
144 * @return a Collection containing the qualified names of all options,
145 * not null.
146 */
147 public Collection<QualifiedName> keySet()
148 {
149 Collection<Option> options = values();
150 Set<QualifiedName> result = new HashSet<QualifiedName>(options.size());
151 for (Option option : options)
152 {
153 result.add(option.getQualifiedName());
154 }
155 return result;
156 }
157
158 /**
159 * Returns a map containing all options in the global scope.
160 * The key of the map is the key of the option, the value is the
161 * option itself.
162 *
163 * @return a map containing the options in global scope, not null.
164 */
165 public Map<QualifiedName, Option> getGlobalScope()
166 {
167 return Collections.unmodifiableMap(globalScope);
168 }
169
170 /**
171 * Checks if an option with the given key exists in any scope.
172 *
173 * @param key the key to check.
174 *
175 * @return true if the key exists, false otherwise.
176 */
177 public boolean containsKey(QualifiedName key)
178 {
179 return (globalScope.containsKey(key));
180 }
181
182 @Override
183 public String toString()
184 {
185 return "global Scope: " + globalScope.toString();
186 }
187 }