2 * Portions Copyright 2001 Sun Microsystems, Inc.
3 * Portions Copyright 1999-2001 Language Technologies Institute,
4 * Carnegie Mellon University.
5 * All Rights Reserved. Use is subject to license terms.
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
11 package com.sun.speech.freetts;
13 import java.io.PrintWriter;
14 import java.text.DecimalFormat;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.LinkedHashMap;
19 import java.util.List;
22 import com.sun.speech.freetts.util.Utilities;
25 * Implementation of the FeatureSet interface.
27 public class FeatureSetImpl implements FeatureSet {
28 private final Map featureMap;
29 static DecimalFormat formatter;
32 * Creates a new empty feature set
34 public FeatureSetImpl() {
35 featureMap = new LinkedHashMap();
39 * Determines if the given feature is present.
41 * @param name the name of the feature of interest
43 * @return true if the named feature is present
45 public boolean isPresent(String name) {
46 return featureMap.containsKey(name);
51 * Removes the named feature from this set of features.
53 * @param name the name of the feature of interest
55 public void remove(String name) {
56 featureMap.remove(name);
60 * Convenience method that returns the named feature as a string.
62 * @param name the name of the feature
64 * @return the value associated with the name or null if the value
67 * @throws ClassCastException if the associated value is not a
70 public String getString(String name) {
71 return (String) getObject(name);
75 * Convenience method that returns the named feature as a int.
77 * @param name the name of the feature
79 * @return the value associated with the name or null if the value
82 * @throws ClassCastException if the associated value is not an int.
84 public int getInt(String name) {
85 return ((Integer) getObject(name)).intValue();
89 * Convenience method that returns the named feature as a float.
91 * @param name the name of the feature
93 * @return the value associated with the name or null if the value
96 * @throws ClassCastException if the associated value is not a
99 public float getFloat(String name) {
100 return ((Float) getObject(name)).floatValue();
104 * Returns the named feature as an object.
106 * @param name the name of the feature
108 * @return the value associated with the name or null if the value
111 public Object getObject(String name) {
112 return featureMap.get(name);
116 * Convenience method that sets the named feature as a int.
118 * @param name the name of the feature
119 * @param value the value of the feature
121 public void setInt(String name, int value) {
122 setObject(name, new Integer(value));
126 * Convenience method that sets the named feature as a float.
128 * @param name the name of the feature
129 * @param value the value of the feature
131 public void setFloat(String name, float value) {
132 setObject(name, new Float(value));
136 * Convenience method that sets the named feature as a String.
138 * @param name the name of the feature
139 * @param value the value of the feature
141 public void setString(String name, String value) {
142 setObject(name, value);
146 * Sets the named feature.
148 * @param name the name of the feature
149 * @param value the value of the feature
151 public void setObject(String name, Object value) {
152 featureMap.put(name, value);
156 * Dumps the FeatureSet in textual form. The feature name
157 * is not included in the dump.
159 * @param output where to send the formatted output
160 * @param pad the padding
161 * @param title the title
163 public void dump(PrintWriter output, int pad, String title) {
164 dump(output, pad, title, false);
168 * Dumps the FeatureSet in textual form.
170 * @param output where to send the formatted output
171 * @param pad the padding
172 * @param title the title
173 * @param showName if <code>true</code>, include the feature name
175 public void dump(PrintWriter output, int pad, String title,
177 List keys = new ArrayList(featureMap.keySet());
179 if (formatter == null) {
180 formatter = new DecimalFormat("########0.000000");
182 // Collections.sort(keys);
183 Collections.reverse(keys); // to match flite
185 Utilities.dump(output, pad, title);
186 for (Iterator i = keys.iterator(); i.hasNext(); ) {
187 String key = (String) i.next();
189 if (!showName && key.equals("name")) {
193 Object value = getObject(key);
194 if (value instanceof Dumpable) {
195 Dumpable d = (Dumpable) value;
196 d.dump(output, pad + 4, key);
198 if (value instanceof Float) {
199 Float fval = (Float) value;
200 Utilities.dump(output, pad + 4, key + "=" +
201 formatter.format(fval.floatValue()));
203 Utilities.dump(output, pad + 4, key + "=" + value);