upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / FeatureSetImpl.java
1 /**
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.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts;
12
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;
20 import java.util.Map;
21
22 import com.sun.speech.freetts.util.Utilities;
23
24 /**
25  * Implementation of the FeatureSet interface.
26  */
27 public class FeatureSetImpl implements FeatureSet {
28     private final Map featureMap;
29     static DecimalFormat formatter;
30
31     /**
32      * Creates a new empty feature set
33      */
34     public FeatureSetImpl() {
35         featureMap = new LinkedHashMap();
36     }
37
38     /**
39      * Determines if the given feature is present.
40      *
41      * @param name the name of the feature of interest
42      *
43      * @return true if the named feature is present
44      */
45     public boolean isPresent(String name) {
46         return featureMap.containsKey(name);
47     }
48
49
50     /**
51      * Removes the named feature from this set of features.
52      *
53      * @param name the name of the feature of interest
54      */
55     public void remove(String name) {
56         featureMap.remove(name);
57     }
58
59     /**
60      * Convenience method that returns the named feature as a string.
61      *
62      * @param name the name of the feature
63      *
64      * @return the value associated with the name or null if the value
65      *   is not found
66      *
67      * @throws ClassCastException if the associated value is not a
68      *   String
69      */
70     public String getString(String name) {
71         return (String) getObject(name);
72     }
73
74     /**
75      * Convenience method that returns the named feature as a int.
76      *
77      * @param name the name of the feature
78      *
79      * @return the value associated with the name or null if the value
80      *   is not found
81      *
82      * @throws ClassCastException if the associated value is not an int.
83      */
84     public int getInt(String name) {
85         return ((Integer) getObject(name)).intValue();
86     }
87
88     /**
89      * Convenience method that returns the named feature as a float.
90      *
91      * @param name the name of the feature
92      *
93      * @return the value associated with the name or null if the value
94      *   is not found.
95      *
96      * @throws ClassCastException if the associated value is not a
97      *   float
98      */
99     public float getFloat(String name) {
100         return ((Float) getObject(name)).floatValue();
101     }
102
103     /**
104      * Returns the named feature as an object.
105      *
106      * @param name the name of the feature
107      *
108      * @return the value associated with the name or null if the value
109      *   is not found
110      */
111     public Object getObject(String name) {
112         return featureMap.get(name);
113     }
114
115     /**
116      * Convenience method that sets the named feature as a int.
117      *
118      * @param name the name of the feature
119      * @param value the value of the feature
120      */
121     public void setInt(String name, int value) {
122         setObject(name, new Integer(value));
123     }
124
125     /**
126      * Convenience method that sets the named feature as a float.
127      *
128      * @param name the name of the feature
129      * @param value the value of the feature
130      */
131     public void setFloat(String name, float value) {
132         setObject(name, new Float(value));
133     }
134
135     /**
136      * Convenience method that sets the named feature as a String.
137      *
138      * @param name the name of the feature
139      * @param value the value of the feature
140      */
141     public void setString(String name, String value) {
142         setObject(name, value);
143     }
144
145     /**
146      * Sets the named feature.
147      *
148      * @param name the name of the feature
149      * @param value the value of the feature
150      */
151     public void setObject(String name, Object value) {
152         featureMap.put(name, value);
153     }
154
155     /**
156      * Dumps the FeatureSet in textual form.  The feature name
157      * is not included in the dump.
158      *
159      * @param output where to send the formatted output
160      * @param pad the padding
161      * @param title the title
162      */
163     public void dump(PrintWriter output, int pad, String title) {
164         dump(output, pad, title, false);
165     }
166
167     /**
168      * Dumps the FeatureSet in textual form.
169      *
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
174      */
175     public void dump(PrintWriter output, int pad, String title,
176             boolean showName) {
177         List keys = new ArrayList(featureMap.keySet());
178
179         if (formatter == null) {
180             formatter = new DecimalFormat("########0.000000");
181         }
182         // Collections.sort(keys);
183          Collections.reverse(keys);  // to match flite
184
185         Utilities.dump(output, pad, title);
186         for (Iterator i = keys.iterator(); i.hasNext(); ) {
187             String key = (String) i.next();
188
189             if (!showName && key.equals("name")) {
190                 continue;
191             }
192
193             Object value = getObject(key);
194             if (value instanceof Dumpable) {
195                 Dumpable d = (Dumpable) value;
196                 d.dump(output, pad + 4, key); 
197             } else {
198                 if (value instanceof Float) {
199                     Float fval = (Float) value;
200                     Utilities.dump(output, pad + 4, key + "=" + 
201                             formatter.format(fval.floatValue()));
202                 } else {
203                     Utilities.dump(output, pad + 4, key + "=" + value);
204                 }
205             }
206         }
207     }
208 }