01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: package org.apache.openjpa.jdbc.schema;
20:
21: import java.io.Serializable;
22: import java.util.HashSet;
23: import java.util.Set;
24:
25: import org.apache.commons.lang.StringUtils;
26: import org.apache.openjpa.lib.util.Localizer;
27:
28: /**
29: * Name sets track what names have been taken, ignoring case.
30: * {@link SchemaGroup}s implement this interface for tables, indexes, and
31: * constraints; {@link Table}s implement it for their columns.
32: *
33: * @author Abe White
34: */
35: public class NameSet implements Serializable {
36:
37: private static final Localizer _loc = Localizer
38: .forPackage(NameSet.class);
39:
40: private Set _names = null;
41:
42: /**
43: * Return true if the given name is in use already.
44: */
45: public boolean isNameTaken(String name) {
46: if (name == null)
47: return true;
48: return _names != null && _names.contains(name.toUpperCase());
49: }
50:
51: /**
52: * Attempt to add the given name to the set.
53: *
54: * @param name the name to add
55: * @param validate if true, null or empty names will not be accepted
56: */
57: protected void addName(String name, boolean validate) {
58: if (StringUtils.isEmpty(name)) {
59: if (validate)
60: throw new IllegalArgumentException(_loc.get("bad-name",
61: name).getMessage());
62: return;
63: }
64:
65: // unfortunately, we can't check for duplicate names, because different
66: // DBs use different namespaces for components, and it would be
67: // difficult to find a scheme that fits all and is still useful
68: if (_names == null)
69: _names = new HashSet();
70: _names.add(name.toUpperCase());
71: }
72:
73: /**
74: * Remove the given name from the table.
75: */
76: protected void removeName(String name) {
77: if (name != null && _names != null)
78: _names.remove(name.toUpperCase());
79: }
80: }
|