001    /* The Broad Institute SOFTWARE COPYRIGHT NOTICE AGREEMENT
002    This software and its documentation are copyright 2003 by the Broad Institute/Massachusetts Institute of Technology. All rights are reserved.
003    This software is supplied without any warranty or guaranteed support whatsoever. Neither the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. 
004    */
005    package calhoun.util;
006    
007    import java.io.ByteArrayInputStream;
008    import java.io.ByteArrayOutputStream;
009    import java.io.ObjectInputStream;
010    import java.io.ObjectOutputStream;
011    import java.util.ArrayList;
012    import java.util.Collection;
013    import java.util.Iterator;
014    import java.util.List;
015    
016    public class Util {
017    
018            /** Static helpers.  Do not instantiate */
019            private Util() {
020                    super();
021            }
022    
023            public static<T> void addAll(Collection<T> coll, Iterator<? extends T> iter) {
024                    while(iter.hasNext()) {
025                            coll.add(iter.next());
026                    }
027            }
028            
029            public static void normalizeWeights(float[] weights) {
030                    float total = 0.0f;
031                    for(int i=0; i<weights.length; ++i) {
032                            total += weights[i];
033                    }
034                    for(int i=0; i<weights.length; ++i) {
035                            weights[i] = weights[i]/total;
036                    }
037            }
038            
039            public static int[] convertIntList(List<Integer> list) {
040                    int[] labelArray = new int[list.size()];
041                    for(int j=0; j<labelArray.length; ++j) {
042                            labelArray[j] = list.get(j);
043                    }
044                    return labelArray;
045            }
046    
047            public static float[] convertFloatList(List<Float> list) {
048                    float[] labelArray = new float[list.size()];
049                    for(int j=0; j<labelArray.length; ++j) {
050                            labelArray[j] = list.get(j);
051                    }
052                    return labelArray;
053            }
054    
055            public static List<Double> convertDoubleArray(double[] list) {
056                    List<Double> labelArray = new ArrayList<Double>(list.length);
057                    for(int j=0; j<list.length; ++j) {
058                            labelArray.add(list[j]);
059                    }
060                    return labelArray;
061            }
062    
063            static public boolean safeEquals(Object x, Object y) {
064                    if(x == null)
065                            return y == null;
066                    else
067                            return x.equals(y);             
068            }
069    
070            /** make a copy of any serializable object graph */
071            static public Object deepClone(Object o) {
072                    try { 
073                            Object copy;
074                            
075                            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
076                            ObjectOutputStream oos = new ObjectOutputStream(baos);
077                            oos.writeObject(o);
078                            oos.close();
079                            ByteArrayInputStream bios = new ByteArrayInputStream(baos.toByteArray());
080                            ObjectInputStream ois = new ObjectInputStream(bios);
081                            copy = ois.readObject();
082                            
083                            return copy;
084                    } catch (Exception ex) {
085                            throw new ErrorException("Could not copy object "+o.toString(), ex);
086                    }
087            }
088    }