001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.content;
028:
029: import com.sun.midp.content.ContentHandlerImpl;
030:
031: /**
032: * An <code>ActionNameMap</code> provides a mapping between
033: * actions and corresponding action names.
034: * The action name SHOULD be used by an application when the action
035: * is presented to a user.
036: * The action names in each map apply to a single {@link #getLocale locale}.
037: * The application should get the appropriate
038: * <code>ActionNameMap</code> based on the desired locale
039: * from the method
040: * {@link ContentHandler#getActionNameMap(String locale)
041: * ContentHandler.getActionNameMap}.
042: * The actions and corresponding action names are set when the
043: * <code>ActionNameMap</code> is created and are immutable thereafter.
044: * The indices of the actions and action names are in the range
045: * 0 to size-1.
046: */
047: public final class ActionNameMap {
048: /** The locale for this ActionNameMap. */
049: private final String locale;
050:
051: /** The array of actions. */
052: private final String[] actions;
053:
054: /** The array of action names. */
055: private final String[] actionnames;
056:
057: /**
058: * Create a new map of actions to action names for a locale.
059: * The actions and names are parallel sequences of equal length.
060: * Each action maps to the corresponding action name.
061: *
062: * @param actions an array of actions; MUST NOT be <code>null</code>
063: * @param actionnames an array of action names;
064: * MUST NOT be <code>null</code>
065: * @param locale of the action names; MUST NOT be <code>null</code>;
066: * should be formatted according to the locale syntax
067: * conventions in {@link ContentHandler}.
068: *
069: * @exception IllegalArgumentException:
070: * <UL>
071: * <LI>if any of the <code>actions</code> strings or
072: * <code>actionname</code> strings have
073: * a length of zero,</LI>
074: * <LI>if the length of the <code>actions</code> and
075: * <code>actionnames</code> arrays
076: * are unequal, or equal to zero, or </LI>
077: * <LI>if the <code>actions</code> array includes any duplicate
078: * actions.</LI>
079: * </UL>
080: * @exception NullPointerException if <code>actions</code>,
081: * <code>actionnames</code>, <code>locale</code>, or
082: * any array element is <code>null</code>.
083: */
084: public ActionNameMap(String[] actions, String[] actionnames,
085: String locale) {
086: if (locale.length() == 0) { // trigger NullPointerException
087: throw new IllegalArgumentException("empty string");
088: }
089: if (actions.length != actionnames.length || actions.length == 0) {
090: throw new IllegalArgumentException("lengths incorrect");
091: }
092:
093: this .locale = locale;
094: this .actions = ContentHandlerImpl.copy(actions);
095: this .actionnames = ContentHandlerImpl.copy(actionnames);
096: if (findDuplicate(this .actions) >= 0) {
097: throw new IllegalArgumentException("duplicate string");
098: }
099: }
100:
101: /**
102: * Gets the action name for an action.
103: *
104: * @param action the action for which to get the associated action name;
105: * MUST NOT be <code>null</code>
106: * @return the action name; <code>null</code> is returned
107: * if the action is not found in the sequence of actions
108: * @exception NullPointerException if action is <code>null</code>
109: */
110: public String getActionName(String action) {
111: int index = find(actions, action);
112: return (index >= 0) ? actionnames[index] : null;
113: }
114:
115: /**
116: * Gets the action for the action name.
117: * If the action name appears more than once in the sequence,
118: * then any one of the corresponding actions may be returned.
119: *
120: * @param actionname the action name for which to get the
121: * associated action; MUST NOT be <code>null</code>
122: * @return the action; <code>null</code> is returned
123: * if the <code>actionname</code> is not found in the sequence
124: * of action names
125: * @exception NullPointerException if actionname is <code>null</code>
126: */
127: public String getAction(String actionname) {
128: int index = find(actionnames, actionname);
129: return (index >= 0) ? actions[index] : null;
130: }
131:
132: /**
133: * Gets the locale for this set of action names.
134: * @return the locale string; must not be <code>null</code>
135: */
136: public String getLocale() {
137: return locale;
138: }
139:
140: /**
141: * Gets the number of pairs of actions and action names.
142: * @return the number of actions and corresponding action names
143: */
144: public int size() {
145: return actions.length;
146: }
147:
148: /**
149: * Gets the action at the specified index.
150: *
151: * @param index the index of the action
152: * @return the action at the specified index
153: * @exception IndexOutOfBoundsException if index is less than zero or
154: * greater than or equal to the value of the {@link #size size} method.
155: */
156: public String getAction(int index) {
157: return actions[index];
158: }
159:
160: /**
161: * Gets the action name at the specified index.
162: *
163: * @param index the index of the action name
164: * @return the action name at the specified index
165: * @exception IndexOutOfBoundsException if index is less than zero or
166: * greater than or equal to the value of the {@link #size size} method.
167: */
168: public String getActionName(int index) {
169: return actionnames[index];
170: }
171:
172: /**
173: * Search a String for a string.
174: * @param strings the array of strings
175: * @param string the string to find
176: * @return the index of the string or -1 if not found
177: * @exception NullPointerException if string is <code>null</code>
178: */
179: private int find(String[] strings, String string) {
180: for (int i = 0; i < strings.length; i++) {
181: if (string.equals(strings[i])) {
182: return i;
183: }
184: }
185: return -1;
186: }
187:
188: /**
189: * Check the strings in an array are unique; no duplicates.
190: * @param strings the string array to check.
191: * @return return the index of a string that is duplicated;
192: * return -1 if none
193: *
194: */
195: private int findDuplicate(String[] strings) {
196: for (int i = 0; i < strings.length; i++) {
197: for (int j = i + 1; j < strings.length; j++) {
198: if (strings[i].equals(strings[j])) {
199: return j;
200: }
201: }
202: }
203: return -1;
204: }
205: }
|