001: /*
002: * $RCSfile: CaselessStringArrayTable.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:59 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.util;
013:
014: import java.util.Hashtable;
015: import javax.media.jai.util.CaselessStringKey;
016:
017: /**
018: * A class that maps an array of <code>String</code>s or
019: * <code>CaselessStringKey</code>s into the array indices and
020: * vice versa (all in a caseless fashion).
021: *
022: * This is used to map source names and parameter names to their
023: * indices in a case insensitive fashion.
024: */
025: public class CaselessStringArrayTable implements java.io.Serializable {
026:
027: private CaselessStringKey[] keys;
028: private Hashtable indices;
029:
030: /**
031: * Constructor for an array of <code>CaselessStringKey</code>s.
032: */
033: public CaselessStringArrayTable() {
034: this ((CaselessStringKey[]) null);
035: }
036:
037: /**
038: * Constructor for an array of <code>CaselessStringKey</code>s.
039: */
040: public CaselessStringArrayTable(CaselessStringKey[] keys) {
041:
042: this .keys = keys;
043: this .indices = new Hashtable();
044:
045: if (keys != null)
046: for (int i = 0; i < keys.length; i++) {
047: this .indices.put(keys[i], new Integer(i));
048: }
049: }
050:
051: /**
052: * Constructor for an array of <code>String</code>s.
053: */
054: public CaselessStringArrayTable(String[] keys) {
055: this (toCaselessStringKey(keys));
056: }
057:
058: /**
059: * Map an array of <code>String</code>s to <code>CaselessStringKey</code>s.
060: */
061: private static CaselessStringKey[] toCaselessStringKey(
062: String strings[]) {
063: if (strings == null)
064: return null;
065:
066: CaselessStringKey[] keys = new CaselessStringKey[strings.length];
067:
068: for (int i = 0; i < strings.length; i++)
069: keys[i] = new CaselessStringKey(strings[i]);
070:
071: return keys;
072: }
073:
074: /**
075: * Get the index of the specified key.
076: *
077: * @throws IllegalArgumentException if the key is <code>null or
078: * if the key is not found.
079: */
080: public int indexOf(CaselessStringKey key) {
081: if (key == null) {
082: throw new IllegalArgumentException(JaiI18N
083: .getString("CaselessStringArrayTable0"));
084: }
085:
086: Integer i = (Integer) indices.get(key);
087:
088: if (i == null) {
089: throw new IllegalArgumentException(key.getName() + " - "
090: + JaiI18N.getString("CaselessStringArrayTable1"));
091: }
092:
093: return i.intValue();
094: }
095:
096: /**
097: * Get the index of the specified key.
098: *
099: * @throws IllegalArgumentException if the key is <code>null or
100: * if the key is not found.
101: */
102: public int indexOf(String key) {
103: return indexOf(new CaselessStringKey(key));
104: }
105:
106: /**
107: * Get the <code>String</code> corresponding to the index <code>i</code>.
108: *
109: * @throws ArrayIndexOutOfBoundsException if <code>i</code> is out of range.
110: */
111: public String getName(int i) {
112: if (keys == null)
113: throw new ArrayIndexOutOfBoundsException();
114:
115: return keys[i].getName();
116: }
117:
118: /**
119: * Get the <code>CaselessStringKey</code> corresponding to the
120: * index <code>i</code>.
121: *
122: * @throws ArrayIndexOutOfBoundsException if <code>i</code> is out of range.
123: */
124: public CaselessStringKey get(int i) {
125: if (keys == null)
126: throw new ArrayIndexOutOfBoundsException();
127:
128: return keys[i];
129: }
130:
131: /**
132: * Tests if this table contains the specified key.
133: *
134: * @return true if the key is present. false otherwise.
135: */
136: public boolean contains(CaselessStringKey key) {
137: if (key == null) {
138: throw new IllegalArgumentException(JaiI18N
139: .getString("CaselessStringArrayTable0"));
140: }
141:
142: return indices.get(key) != null;
143: }
144:
145: /**
146: * Tests if this table contains the specified key.
147: *
148: * @return true if the key is present. false otherwise.
149: */
150: public boolean contains(String key) {
151: return contains(new CaselessStringKey(key));
152: }
153: }
|