GroovyUtils
Administrator
17:21 21/05/2013	

Table of Contents

/* Groovy Class #* */
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Comparator;
class Utils { 
  def log = new ArrayList();
  String formatToScientific(number) { 
    DecimalFormat df = NumberFormat.getNumberInstance();
    df.applyPattern(”0.00E0”);
    df.setGroupingUsed(false);
    return df.format(new Double(number));
    return number;
  } 
  Double round(val, precision) { 
    log.add(”x”);
    if (Double.isInfinite(val) || Double.isNaN(val)) return val;
    BigDecimal bd = new BigDecimal(val, new java.math.MathContext(precision) );
    return bd.doubleValue();
  } 
  ArrayList sortMSAC(list) { 
    TreeSet ts = new TreeSet(new MSACcomparator(log));
    ts.addAll(list);
    ArrayList sortedList = new ArrayList(ts);
    return sortedList;
  } 
  ArrayList sort(list) { 
    TreeSet ts = new TreeSet();
    ts.addAll(list);
    ArrayList sortedList = new ArrayList(ts);
    return sortedList;
  } 
  String nextItem(list,item) { 
    int index = list.lastIndexOf(item)+1;
    if (index >= list.size()) index = 0;
    return list.get(index);
  } 
  String previousItem(list,item) { 
    int index = list.lastIndexOf(item)-1;
    if (index < 0) index = list.size()-1;
    return list.get(index);
  } 
 
  ArrayList getLog() { 
    return log;
  } 
} 
class MSACcomparator implements Comparator { 
  def log;
  MSACcomparator(log){ 
    this.log=log;
  } 
  int compare(Object o1, Object o2) { 
    String str1 = o1.toString();
    str1 = str1.substring(str1.indexOf(’.’)+1);
    String str2 = o2.toString();
    str2 = str2.substring(str2.indexOf(’.’)+1);
    String[] part1 = str1.split(”_”);
    try { 
      int m1=0,s1=0,a1=0;
      String c1=””;
      if (part1.length>4) { 
        m1 = Integer.parseInt(part1[0]);
        s1 = Integer.parseInt(part1[1]);
        a1 = Integer.parseInt(part1[2]);
        c1 = str1.substring(part1[0].length()+part1[1].length()+part1[2].length()+3);
      } 
      String[] part2 = str2.split(”_”);
      int m2=0,s2=0,a2=0;
      String c2=””;
      if (part2.length>4) { 
        m2 = Integer.parseInt(part2[0]);
        s2 = Integer.parseInt(part2[1]);
        a2 = Integer.parseInt(part2[2]);
        c2 = str2.substring(part2[0].length()+part2[1].length()+part2[2].length()+3);
      } 
      Integer i1 = a1+s1*1000+m1*100000;
       Integer i2 = a2+s2*1000+m2*100000;
      return i1.compareTo(i2);
       log.add(m1+”:”+m2+” - “+s1+”:”+s2+” - “+a1+”:”+a2+” - “+str1+” - “+str2);
      if (m1>m2) return 1;
      if (m1<m2) return -1;
      if (s1>s2) return 1;
      if (s1<s2) return -1;
      if (a1>a2) return 1;
      if (a1<a2) return -1;
      return c1.compareTo(c2);
    }  catch (Exception ex) { 
      log.add(ex+” - “+str1+” - “+str2);
      return 0;
    } 
    return 0;
    return s1.compareTo(s2);
  } 
} 
/* *# */