📄 basiccontrolledvocabulary.java
字号:
/* * File: BasicControlledVocabulary.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.util;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;/** * A ControlledVocabulary holds a restricted list of entries.<br> * The entries should be unique in the sence that the value of the entries * must be unique. Pending: we are using a List now and take care ourselves * that all elements are unique. Could use some kind of Set when we would * decide to let CVEntry override the equals() method. Pending: should the * entries always be sorted (alphabetically)? <b>Note:</b> this class is not * thread-safe. * * This class has no undo/redo - functionality! * * @author Han Sloejes, Alex Klassmann * $Id: BasicControlledVocabulary.java,v 1.5 2006/08/18 10:50:47 klasal Exp $ */public class BasicControlledVocabulary { /** constant for the move-to-top edit type */ public static final int MOVE_TO_TOP = 0; /** constant for the move-up edit type */ public static final int MOVE_UP = 1; /** constant for the move-down edit type */ public static final int MOVE_DOWN = 2; /** constant for the move-to-bottom edit type */ public static final int MOVE_TO_BOTTOM = 3; /** Holds value of property DOCUMENT ME! */ protected List entries; private String description; private String name; private boolean initMode; /** * Creates a CV with the specified name and description and the specified description. * * @param name the name of the CV * @param description the description of the CV * * throws IllegalArgumentException when the name is <code>null</code> * or of length 0 */ public BasicControlledVocabulary(String name, String description) { if ((name == null) || (name.length() == 0)) { throw new IllegalArgumentException( "The name can not be null or empty."); } this.name = name; this.description = description; entries = new ArrayList(); } /** * Creates a CV with the specified name and empty description. * * @param name the name of the CV * * throws IllegalArgumentException when the name is <code>null</code> * or of length 0 */ public BasicControlledVocabulary(String name) { this(name, ""); } /** * Sets the description of this CV. * * @param description the new description of the CV */ public void setDescription(String description) { this.description = description; if (!initMode) { handleModified(); } } /** * Returns the description of the CV. * * @return the description of the CV, can be <code>null</code> */ public String getDescription() { return description; } /** * Returns an array containing all entries in this Vocabulary. * * @return an array of entries */ public CVEntry[] getEntries() { return (CVEntry[]) entries.toArray(new CVEntry[] { }); } /** * Returns a sorted array of entries. The values are sorted using the * String.compareTo(String) method. * * @return a sorted array of CVEntry objects */ public CVEntry[] getEntriesSortedByAlphabet() { CVEntry[] allEntries = getEntries(); Arrays.sort(allEntries); return allEntries; } /** * Returns an array containing the values (Strings) of the entries. This is * convenience method to get a view on the entry values in the CV. * * @return an array of Strings containing the vallues in this CV */ public String[] getEntryValues() { String[] values = new String[entries.size()]; for (int i = 0; i < entries.size(); i++) { values[i] = ((CVEntry) entries.get(i)).getValue(); } return values; } /** * Returns the CVEntry with the specified value, if present. * * @param value the value of the entry * * @return the CVEntry with the specified value, or null */ public CVEntry getEntryWithValue(String value) { CVEntry entry = null; if (value == null) { return entry; } for (int i = 0; i < entries.size(); i++) { //ignore case ? if (((CVEntry) entries.get(i)).getValue().equals(value)) { entry = (CVEntry) entries.get(i); break; } } return entry; } /** * @param initMode if true, don't call handleModified */ public void setInitMode(boolean initMode) { this.initMode = initMode; } /** * Sets the name of this CV. * * @param name the new name of the CV * * throws IllegalArgumentException when the name is <code>null</code> * or of length 0 */ public void setName(String name) { if ((name == null) || (name.length() == 0)) { throw new IllegalArgumentException( "The name can not be null or empty."); } this.name = name; if (!initMode) { handleModified(); } } /** * Returns the name of the CV. * * @return the name of this CV */ public String getName() { return name; } /** * Returns an array containing the values (Strings) of the entries, ordered alphabetically.<br> * This is convenience method to get an ordered view on the entry values * in the CV. * * @return an sorted array of Strings containing the vallues in this CV */ public String[] getValuesSortedByAlphabet() { String[] values = getEntryValues(); Arrays.sort(values); return values; } /** * A shorthand for adding more than one CVEntry at a time. * * @param entries an array of entries */ public void addAll(CVEntry[] entries) { if (entries != null) { for (int i = 0; i < entries.length; i++) { addEntry(entries[i]); } } if (!initMode) { handleModified(); } } /** * Adds a new CVEntry to the List. * * @param entry the new entry * * @return true if the entry was succesfully added, false otherwise */ public boolean addEntry(CVEntry entry) { if (entry == null) { return false; } Iterator it = entries.iterator(); while (it.hasNext()) { if (((CVEntry) it.next()).getValue().equals(entry.getValue())) { return false; } } entries.add(entry); if (!initMode) { handleModified(); } return true; } /** * Removes all entries from this ControlledVocabulary. */ public void clear() { entries.clear(); if (!initMode) { handleModified(); } } /** * Checks whether the specified CVEntry is in this CV.<br> * <b>Note:</b> This only checks for object equality. * * @param entry the CVEntry * * @return true if entry is in the CV, false otherwise * * @see #containsValue(String) */ public boolean contains(CVEntry entry) { if (entry == null) { return false; } return entries.contains(entry); } /** * Checks whether there is a CVEntry with the specified value in this * CV.<br> * * @param value the value * * @return true if there is an entry with this value in the CV, false * otherwise * * @see #contains(CVEntry) */ public boolean containsValue(String value) { if (value == null) { return false; } for (int i = 0; i < entries.size(); i++) { if (((CVEntry) entries.get(i)).getValue().equals(value)) { //ignore case?? return true; } } return false; } /** * Overrides <code>Object</code>'s equals method by checking all fields of * the other object to be equal to all fields in this object. * * @param obj the reference object with which to compare * * @return true if this object is the same as the obj argument; false * otherwise */ public boolean equals(Object obj) { if (obj == null) { // null is never equal return false; } if (obj == this) { // same object reference return true; } if (!(obj instanceof BasicControlledVocabulary)) { // it should be a MediaDescriptor object return false; } // check the fields BasicControlledVocabulary other = (BasicControlledVocabulary) obj; if (!name.equals(other.getName())) { return false; } if ((description != null) && !description.equals(other.getDescription())) { return false; } if ((other.getDescription() != null) && !other.getDescription().equals(description)) { return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -