01: /*
02: Copyright (c) 2007, Dennis M. Sosnoski
03: All rights reserved.
04:
05: Redistribution and use in source and binary forms, with or without modification,
06: are permitted provided that the following conditions are met:
07:
08: * Redistributions of source code must retain the above copyright notice, this
09: list of conditions and the following disclaimer.
10: * Redistributions in binary form must reproduce the above copyright notice,
11: this list of conditions and the following disclaimer in the documentation
12: and/or other materials provided with the distribution.
13: * Neither the name of JiBX nor the names of its contributors may be used
14: to endorse or promote products derived from this software without specific
15: prior written permission.
16:
17: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28:
29: package org.jibx.binding.generator;
30:
31: import java.util.HashSet;
32: import java.util.Iterator;
33: import java.util.Set;
34:
35: /**
36: * Set of unique names for a context. This assures uniqueness as names are added
37: * to the set.
38: */
39: public class UniqueNameSet {
40: /** Set of names used. */
41: private final Set m_nameSet;
42:
43: /**
44: * Constructor.
45: */
46: public UniqueNameSet() {
47: m_nameSet = new HashSet();
48: }
49:
50: /**
51: * Add name to set. If the supplied name is already present, it is modified
52: * by appending a numeric suffix starting at '1' and going as high as
53: * necessary to obtain a unique name.
54: *
55: * @param base name to try adding
56: * @return assigned name
57: */
58: public String add(String base) {
59: String name = base;
60: int index = 1;
61: while (m_nameSet.contains(name)) {
62: name = base + index++;
63: }
64: m_nameSet.add(name);
65: return name;
66: }
67:
68: /**
69: * Get iterator for names in set.
70: *
71: * @return iterator
72: */
73: public Iterator iterator() {
74: return m_nameSet.iterator();
75: }
76: }
|