001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * SelectedTag.java
019: * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.core;
024:
025: import java.util.HashSet;
026:
027: /**
028: * Represents a selected value from a finite set of values, where each
029: * value is a Tag (i.e. has some string associated with it). Primarily
030: * used in schemes to select between alternative behaviours,
031: * associating names with the alternative behaviours.
032: *
033: * @author <a href="mailto:len@reeltwo.com">Len Trigg</a>
034: * @version $Revision: 1.10 $
035: */
036: public class SelectedTag {
037:
038: /** The index of the selected tag */
039: protected int m_Selected;
040:
041: /** The set of tags to choose from */
042: protected Tag[] m_Tags;
043:
044: /**
045: * Creates a new <code>SelectedTag</code> instance.
046: *
047: * @param tagID the id of the selected tag.
048: * @param tags an array containing the possible valid Tags.
049: * @throws IllegalArgumentException if the selected tag isn't in the array
050: * of valid values or the IDs/IDStrs are not unique.
051: */
052: public SelectedTag(int tagID, Tag[] tags) {
053: // are IDs unique?
054: HashSet ID = new HashSet();
055: HashSet IDStr = new HashSet();
056: for (int i = 0; i < tags.length; i++) {
057: ID.add(new Integer(tags[i].getID()));
058: IDStr.add(tags[i].getIDStr());
059: }
060: if (ID.size() != tags.length)
061: throw new IllegalArgumentException(
062: "The IDs are not unique!");
063: if (IDStr.size() != tags.length)
064: throw new IllegalArgumentException(
065: "The ID strings are not unique!");
066:
067: for (int i = 0; i < tags.length; i++) {
068: if (tags[i].getID() == tagID) {
069: m_Selected = i;
070: m_Tags = tags;
071: return;
072: }
073: }
074:
075: throw new IllegalArgumentException("Selected tag is not valid");
076: }
077:
078: /**
079: * Creates a new <code>SelectedTag</code> instance.
080: *
081: * @param tagText the text of the selected tag (case-insensitive).
082: * @param tags an array containing the possible valid Tags.
083: * @throws IllegalArgumentException if the selected tag isn't in the array
084: * of valid values.
085: */
086: public SelectedTag(String tagText, Tag[] tags) {
087: for (int i = 0; i < tags.length; i++) {
088: if (tags[i].getReadable().equalsIgnoreCase(tagText)
089: || tags[i].getIDStr().equalsIgnoreCase(tagText)) {
090: m_Selected = i;
091: m_Tags = tags;
092: return;
093: }
094: }
095: throw new IllegalArgumentException("Selected tag is not valid");
096: }
097:
098: /**
099: * Returns true if this SelectedTag equals another object
100: *
101: * @param o the object to compare with
102: * @return true if the tags and the selected tag are the same
103: */
104: public boolean equals(Object o) {
105: if ((o == null) || !(o.getClass().equals(this .getClass()))) {
106: return false;
107: }
108: SelectedTag s = (SelectedTag) o;
109: if ((s.getTags() == m_Tags)
110: && (s.getSelectedTag() == m_Tags[m_Selected])) {
111: return true;
112: } else {
113: return false;
114: }
115: }
116:
117: /**
118: * Gets the selected Tag.
119: *
120: * @return the selected Tag.
121: */
122: public Tag getSelectedTag() {
123: return m_Tags[m_Selected];
124: }
125:
126: /**
127: * Gets the set of all valid Tags.
128: *
129: * @return an array containing the valid Tags.
130: */
131: public Tag[] getTags() {
132: return m_Tags;
133: }
134:
135: /**
136: * returns the selected tag in string representation
137: *
138: * @return the selected tag as string
139: */
140: public String toString() {
141: return getSelectedTag().toString();
142: }
143: }
|