001: /*
002: * ProfileKey.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.profiles;
013:
014: /**
015: * A class to uniquely identify a {@link workbench.db.ConnectionProfile}
016: *
017: * @author support@sql-workbench.net
018: */
019: public class ProfileKey {
020: private String name; // the name of the profile
021: private String group; // the profile group
022:
023: /**
024: * Create a new ProfileKey.
025: * The passed name can consist of the profile group and the profile name
026: * the group needs to be enclosed in curly brackets, e.g:
027: * <tt>{MainGroup}/HR Database</tt><br/>
028: * The divividing slash is optional.
029: *
030: * @param pname the name (can include the profile group) of the profile
031: */
032: public ProfileKey(String pname) {
033: if (pname == null)
034: throw new NullPointerException("Name cannot be null!");
035: setName(pname);
036: }
037:
038: /**
039: * Create a new key based on a profile name and a group name.
040: *
041: * @param pname the name of the profile
042: * @param pgroup the group to which the profile belongs
043: */
044: public ProfileKey(String pname, String pgroup) {
045: if (pgroup != null)
046: group = pgroup.trim();
047: setName(pname);
048: }
049:
050: private void setName(String pname) {
051: String tname = pname.trim();
052: if (tname.charAt(0) == '{') {
053: int pos = tname.indexOf('}');
054: if (pos < 0)
055: throw new IllegalArgumentException(
056: "Missing closing } to define group name");
057: int slashPos = tname.indexOf("/", pos + 1);
058: if (slashPos < 0)
059: slashPos = pos;
060: this .name = tname.substring(slashPos + 1).trim();
061: this .group = tname.substring(1, pos).trim();
062: } else {
063: name = tname;
064: }
065: }
066:
067: public String getName() {
068: return name;
069: }
070:
071: public String getGroup() {
072: return group;
073: }
074:
075: public String toString() {
076: if (group == null)
077: return name;
078: return "{" + group + "}/" + name;
079: }
080:
081: @Override
082: public int hashCode() {
083: return toString().hashCode();
084: }
085:
086: @Override
087: public boolean equals(Object other) {
088: if (other == null)
089: return false;
090: if (other instanceof ProfileKey) {
091: ProfileKey key = (ProfileKey) other;
092: if (key.getName() == null)
093: return false;
094: if (this .name.equals(key.getName())) {
095: if (key.getGroup() == null || this .group == null)
096: return true;
097: return this .getGroup().equals(key.getGroup());
098: }
099: }
100: return false;
101: }
102:
103: }
|