ingo@1279: package de.intevation.flys.client.shared;
ingo@1279: 
ingo@1279: 
ingo@1279: public final class DoubleUtils {
ingo@1279: 
ingo@1279:     public static final String DEFAULT_DELIM = " ";
ingo@1279: 
ingo@1279: 
ingo@1279:     private DoubleUtils() {
ingo@1279:     }
ingo@1279: 
ingo@1279: 
ingo@1279:     public static Double getDouble(String value) {
ingo@1279:         try {
ingo@1279:             return Double.valueOf(value);
ingo@1279:         }
ingo@1279:         catch (NumberFormatException nfe) {
ingo@1279:             // do nothing
ingo@1279:         }
ingo@1279: 
ingo@1279:         return null;
ingo@1279:     }
ingo@1279: 
ingo@1279: 
ingo@1279:     public static double[] getMinMax(String value) {
ingo@1279:         return getMinMax(value, DEFAULT_DELIM);
ingo@1279:     }
ingo@1279: 
ingo@1279: 
ingo@1279:     public static double[] getMinMax(String value, String delim) {
ingo@1279:         if (value == null) {
ingo@1279:             return null;
ingo@1279:         }
ingo@1279: 
ingo@1279:         String[] values = value.split(delim);
ingo@1279: 
ingo@1279:         int     len = values != null ? values.length : 0;
ingo@1279:         double[] mm = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
ingo@1279: 
ingo@1279:         for (int i = 0; i < len; i++) {
ingo@1279:             Double d = getDouble(values[i]);
ingo@1279: 
ingo@1279:             if (d != null) {
ingo@1279:                 mm[0] = mm[0] < d ? mm[0] : d;
ingo@1279:                 mm[1] = mm[1] > d ? mm[1] : d;
ingo@1279:             }
ingo@1279:         }
ingo@1279: 
ingo@1279:         return mm[0] != Double.MAX_VALUE && mm[1] != -Double.MAX_VALUE
ingo@1279:             ? mm
ingo@1279:             : null;
ingo@1279:     }
felix@1616: 
felix@1616:     /** toIndex is not inclusive, fromIndex is. */
felix@1616:     static void fill(double[] array, int fromIndex, int toIndex, double val) {
felix@1616:         for (int i = fromIndex; i < toIndex; i++) {
felix@1616:             array[i] = val;
felix@1616:         }
felix@1616:     }
felix@1616: 
felix@1616:     /** @see java.util.Arrays.copyOf */
felix@1616:     public static double[] copyOf(double[] toCopy, int newLen) {
felix@1616:         double[] nArray = new double[newLen];
felix@1616: 
felix@1616:         if (toCopy == null) {
felix@1616:             fill(nArray, 0, nArray.length, 0d);
felix@1616:             return nArray;
felix@1616:         }
felix@1616: 
felix@1616:         int goTo = (newLen < toCopy.length)
felix@1616:             ? newLen
sascha@2905:             : toCopy.length;
felix@1616: 
felix@1616:         for (int i = 0; i < goTo; i++) {
felix@1616:             nArray[i] = toCopy[i];
felix@1616:         }
felix@1616:         fill (nArray, goTo, nArray.length, 0d);
felix@1616:         return nArray;
felix@1616:     }
ingo@1279: }
ingo@1279: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :