001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.viewer;
028:
029: import org.cougaar.util.log.Logger;
030:
031: import java.lang.reflect.Method;
032: import java.util.HashSet;
033:
034: public class UniqueNameSet extends HashSet {
035: private String prefix;
036: private int nameCounter = 0;
037: protected transient Logger log;
038:
039: /**
040: * Construct an object which can generate unique names
041: * from the specified prefix.
042: * @param prefix the prefix to use in the unique names
043: */
044: public UniqueNameSet(String prefix) {
045: this .prefix = prefix;
046: createLogger();
047: }
048:
049: private void createLogger() {
050: log = CSMART.createLogger(this .getClass().getName());
051: }
052:
053: /**
054: * Initialize the unique name set with a set of objects,
055: * using the specified method to get the names of those objects.
056: * @param things an array of objects used to initialize the unique name set
057: * @param getNameMethod the <code>Method</code> to get the name of an object
058: */
059: public void init(Object[] things, Method getNameMethod) {
060: Object[] noArgs = new Object[0];
061: for (int i = 0; i < things.length; i++) {
062: try {
063: String name = (String) getNameMethod.invoke(things[i],
064: noArgs);
065: add(name);
066: } catch (Exception e) {
067: if (log.isErrorEnabled()) {
068: log.error("Reading: " + things[i], e);
069: }
070: }
071: }
072: }
073:
074: /**
075: * Get a unique name for an object.
076: * @return the unique name
077: */
078: public String generateName() {
079: return generateName(prefix);
080: }
081:
082: /**
083: * Generate a unique name from the given name by appending
084: * a bracketed counter to it.
085: * @param name the name to use as the base
086: * @return a new unique name
087: */
088: public String generateName(String name) {
089: if (contains(name)) {
090: String base = name;
091: int index = name.indexOf(" [");
092: if (index != -1)
093: base = name.substring(0, index);
094: do {
095: name = base + " [" + ++nameCounter + "]";
096: } while (contains(name));
097: }
098: return name;
099: }
100:
101: }
|