Main Page   Class Hierarchy   Compound List   File List   Compound Members  

FeatChunk.java

00001 /*
00002  *  Copyright 2006 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     protected Vector features = null;
00039 
00043     public FeatChunk(String sf, double st, double l)
00044     {
00045         super(sf, st, l);
00046         
00047         features = new Vector();
00048     }
00049    
00050     public void addFeature(Object[] f)
00051     {
00052         features.addAll(Arrays.asList(f));
00053     }
00054 
00055     public void addFeature(Collection f)
00056     {
00057         features.addAll(f);
00058     }
00059 
00060     public void addFeature(double[] f)
00061     {
00062         // Java Fucking Sucks. why in gods name does the Double class need
00063         // to exist?  Why can't all instances of doubles in Object context
00064         // be automatically promoted to Doubles.  Why damnit why.  (This
00065         // is actually solved in Java 1.5...  They call it autoboxing)
00066         for(int i = 0; i < f.length; i++)
00067             features.add(new Double(f[i]));
00068     }
00069 
00070     public void addFeature(Object f)
00071     {
00072         features.add(f);
00073     }
00074 
00075     public void addFeature(double f)
00076     {
00077         features.add(new Double(f));
00078     }
00079 
00083     public int numFeatures()
00084     {
00085         return features.size();
00086     }
00087 
00091     public double[] getFeatures()
00092     {
00093         double[] feats = new double[features.size()];
00094 
00095         for(int i = 0; i < feats.length; i++)
00096             feats[i] = ((Double)features.get(i)).doubleValue();
00097 
00098         return feats;
00099     }
00100     
00105     public double[] getFeatures(int[] idx)
00106     {
00107         if(features == null)
00108             return null;
00109 
00110         if (idx == null)
00111             return getFeatures();
00112 
00113         double[] feats = new double[idx.length];
00114 
00115         for(int i = 0; i < idx.length; i++)
00116             feats[i] = ((Double)features.get(idx[i])).doubleValue();
00117 
00118         return feats;
00119     }
00120 
00124     public void clearFeatures()
00125     {
00126         features.clear();
00127     }
00128 
00133     public String toString()
00134     {
00135         // concatenating strings is super slow.  Better to use StringBuffer
00136         // guesstimate the string length
00137         StringBuffer s = new StringBuffer(20*features.size());
00138         s.append(srcFile).append(" ").append(startTime).append(" ").append(length).append(" ");
00139 
00140         if(features != null)
00141         {
00142             Iterator x = features.iterator();
00143             while(x.hasNext())
00144                     s.append(x.next()).append(" ");
00145         }
00146 
00147         s.append(comment).append("\n");
00148         
00149         return s.toString();
00150     }
00151 
00155     public Object clone()
00156     {
00157         FeatChunk o = new FeatChunk(this.srcFile, this.startTime, this.length);
00158         o.comment = this.comment;
00159         o.features = (Vector)this.features.clone();
00160 
00161         return o;
00162     }
00163 }

Generated on Thu May 11 15:04:10 2006 for MEAPsoft by doxygen1.2.18