Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

FeatChunk.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft;
00024 
00025 import java.util.Arrays;
00026 import java.util.Collection;
00027 import java.util.Iterator;
00028 import java.util.Vector;
00029 
00036 public class FeatChunk extends Chunk implements Cloneable
00037 {
00038     // List of features associated with this chunk.
00039     protected Vector features = null;
00040 
00044     public FeatChunk(String sf, double st, double l)
00045     {
00046         super(sf, st, l);
00047         
00048         features = new Vector();
00049     }
00050    
00054     public void addFeature(Object[] f)
00055     {
00056         features.addAll(Arrays.asList(f));
00057     }
00058 
00062     public void addFeature(Collection f)
00063     {
00064         features.addAll(f);
00065     }
00066 
00070     public void addFeature(double[] f)
00071     {
00072         // Java Fucking Sucks. why in gods name does the Double class need
00073         // to exist?  Why can't all instances of doubles in Object context
00074         // be automatically promoted to Doubles.  Why damnit why.  (This
00075         // is actually solved in Java 1.5...  They call it autoboxing)
00076         for(int i = 0; i < f.length; i++)
00077             features.add(new Double(f[i]));
00078     }
00079 
00083     public void addFeature(Object f)
00084     {
00085         features.add(f);
00086     }
00087 
00091     public void addFeature(double f)
00092     {
00093         features.add(new Double(f));
00094     }
00095 
00099     public void setFeature(int idx, double f)
00100     {
00101         features.set(idx, new Double(f));
00102     }
00103 
00107     public void setFeatures(double[] f)
00108     {
00109         features = new Vector(f.length);
00110         for(int x = 0; x < f.length; x++)
00111             features.add(x, new Double(f[x]));
00112     }
00113 
00117     public int numFeatures()
00118     {
00119         return features.size();
00120     }
00121 
00125     public double[] getFeatures()
00126     {
00127         double[] feats = new double[features.size()];
00128 
00129         for(int i = 0; i < feats.length; i++)
00130             feats[i] = ((Double)features.get(i)).doubleValue();
00131 
00132         return feats;
00133     }
00134     
00139     public double[] getFeatures(int[] idx)
00140     {
00141         if(features == null)
00142             return null;
00143 
00144         if (idx == null)
00145             return getFeatures();
00146 
00147         double[] feats = new double[idx.length];
00148 
00149         for(int i = 0; i < idx.length; i++)
00150             feats[i] = ((Double)features.get(idx[i])).doubleValue();
00151 
00152         return feats;
00153     }
00154 
00158     public void clearFeatures()
00159     {
00160         features.clear();
00161     }
00162 
00167     public String toString()
00168     {
00169         // concatenating strings is super slow.  Better to use StringBuffer
00170         // guesstimate the string length
00171         StringBuffer s = new StringBuffer(20*features.size());
00172         s.append(srcFile.replaceAll(" ", "%20")).append(" ").append(startTime).append(" ").append(length).append(" ");
00173 
00174         if(features != null)
00175         {
00176             Iterator x = features.iterator();
00177             while(x.hasNext())
00178                     s.append(x.next()).append(" ");
00179         }
00180 
00181         s.append(comment).append("\n");
00182         
00183         return s.toString();
00184     }
00185 
00189     public Object clone()
00190     {
00191         FeatChunk o = new FeatChunk(this.srcFile, this.startTime, this.length);
00192         o.comment = this.comment;
00193         o.features = (Vector)this.features.clone();
00194 
00195         return o;
00196     }
00197 }

Generated on Tue Feb 6 19:02:26 2007 for MEAPsoft by doxygen1.2.18