[Schmitzm-commits] r1996 - in trunk: schmitzm-core/src/main/java/de/schmitzm/data schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales schmitzm-core/src/test/java/de/schmitzm/data schmitzm-gt/src/main/java/de/schmitzm/geotools/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon May 14 13:35:28 CEST 2012


Author: mojays
Date: 2012-05-14 13:35:27 +0200 (Mon, 14 May 2012)
New Revision: 1996

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/TimeUnit.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java
   trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle.properties
   trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle_de.properties
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/gui/RasterPositionLabel.java
Log:
RasterPositionLabel: Konstante LABEL_PREFIX wieder statisch gemacht!
Neue Enum-Klassen f?\195?\188r Einheiten-Umrechnung: TimeUnit, SpeedUnit
DistanceUnit auf neues Interface Unit angepasst.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java	2012-05-11 22:50:16 UTC (rev 1995)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -30,14 +30,13 @@
 
 package de.schmitzm.data;
 
-import de.schmitzm.lang.ResourceProvider;
 
 /**
  * This enum represents distance units and provides several utility
  * function for conversion.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
-public enum DistanceUnit {
+public enum DistanceUnit implements Unit<DistanceUnit> {
   /** Kilometers */
   KM,
   /** Meters */
@@ -49,8 +48,6 @@
   /** Feet (= 1/3 yard) */
   FEET;
 
-  private static final ResourceProvider RESOURCE = DataUtil.RESOURCE;
-
   /** Factor to convert 1 mile to kilometers. */
   public static final double MILE_TO_KM = 1.609344;
   /** Factor to convert 1 mile to yard. 1 mile = 1760 yards. */
@@ -64,6 +61,7 @@
   /**
    * Returns a short abbreviation for the unit.
    */
+  @Override
   public String getAbbreviation() {
     return RESOURCE.getString("DistanceUnit."+this+".Abb");
   }
@@ -71,6 +69,7 @@
   /**
    * Returns a title for the unit.
    */
+  @Override
   public String getTitle() {
     return RESOURCE.getString("DistanceUnit."+this);
   }
@@ -80,7 +79,10 @@
    * {@code x = A.getConversionFactor(B) <=> 1 A = x B}
    * @param units destination unit
    */
+  @Override
   public double getConversionFactor(DistanceUnit units) {
+    if ( this.equals(units) )
+      return 1.0;
     switch ( this ) {
       case METERS: return KM.getConversionFactor(units)/1000.0;
       case KM: switch (units) {
@@ -113,4 +115,15 @@
   public double convertDistanceTo(double dist, DistanceUnit units) {
     return dist * getConversionFactor(units);
   }
+  
+  /**
+   * Converts a value to another unit. 
+   * @param unit unit to convert the value to
+   * @param value value to convert
+   */
+  @Override
+  public double convertTo(DistanceUnit unit, double value) {
+    return convertDistanceTo(value, unit);
+  }
+
 }
\ No newline at end of file

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ ******************************************************************************/
+
+package de.schmitzm.data;
+
+
+/**
+ * This enum represents speed units and provides several utility
+ * function for conversion.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public enum SpeedUnit implements Unit<SpeedUnit> {
+  /** Kilometers per hour */
+  KM_H,
+  /** Meters per second */
+  M_S,
+  /** Miles per hour */
+  MI_H,
+  /** Yards per second */
+  YT_S;
+
+  /** Factor to convert 1 m/s to km/h. */
+  public static final double MS_TO_KMH = 3.6;
+  
+  /**
+   * Returns a short abbreviation for the unit.
+   */
+  @Override
+  public String getAbbreviation() {
+    return RESOURCE.getString("SpeedUnit."+this+".Abb");
+  }
+
+  /**
+   * Returns a title for the unit.
+   */
+  @Override
+  public String getTitle() {
+    return RESOURCE.getString("SpeedUnit."+this);
+  }
+
+  /**
+   * Returns the factor to convert 1 in "this" unit to another unit.<br>
+   * {@code x = A.getConversionFactor(B) <=> 1 A = x B}
+   * @param unit destination unit
+   */
+  @Override
+  public double getConversionFactor(SpeedUnit unit) {
+    if ( this.equals(unit) )
+      return 1.0;
+    
+    switch ( this ) {
+      case M_S: switch (unit) {
+                  case KM_H: return MS_TO_KMH;
+                  case YT_S: return DistanceUnit.METERS.getConversionFactor(DistanceUnit.YARDS);
+                  case MI_H: return getConversionFactor(KM_H) * DistanceUnit.KM.getConversionFactor(DistanceUnit.MILES);
+               }
+               break;
+      case KM_H: return M_S.getConversionFactor(unit) / MS_TO_KMH;
+      case YT_S: return M_S.getConversionFactor(unit)  / DistanceUnit.METERS.getConversionFactor(DistanceUnit.YARDS);
+      case MI_H: return KM_H.getConversionFactor(unit) / DistanceUnit.KM.getConversionFactor(DistanceUnit.MILES);
+    }
+    throw new UnsupportedOperationException("Can not convert "+this+" to "+unit+".");
+  }
+
+  /**
+   * Converts a value to another unit. 
+   * @param unit unit to convert the value to
+   * @param value value to convert
+   */
+  @Override
+  public double convertTo(SpeedUnit unit, double value) {
+    return value * getConversionFactor(unit);
+  }
+
+}
\ No newline at end of file

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/TimeUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/TimeUnit.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/TimeUnit.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -0,0 +1,118 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ ******************************************************************************/
+
+package de.schmitzm.data;
+
+
+/**
+ * This enum represents time units and provides several utility
+ * function for conversion.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public enum TimeUnit implements Unit<TimeUnit> {
+  /** Day */
+  DAY,
+  /** Hour */
+  HOUR,
+  /** Minute */
+  MIN,
+  /** Second */
+  SEC,
+  /** Millisecond */
+  MS;
+
+  /** Factor to convert 1 day to hours. */
+  public static final double DAY_TO_HOUR = 24;
+  /** Factor to convert 1 hour to minutes. */
+  public static final double HOUR_TO_MIN = 60;
+  /** Factor to convert 1 minute to seconds. */
+  public static final double MIN_TO_SEC = 60;
+  /** Factor to convert 1 second to milliseconds. */
+  public static final double SEC_TO_MS = 1000;
+  /** Factor to convert 1 day to minutes. */
+  public static final double DAY_TO_MIN = DAY_TO_HOUR * HOUR_TO_MIN;
+  /** Factor to convert 1 day to seconds. */
+  public static final double DAY_TO_SEC = DAY_TO_MIN * MIN_TO_SEC;
+  /** Factor to convert 1 day to milliseconds. */
+  public static final double DAY_TO_MS = DAY_TO_SEC * SEC_TO_MS;
+  
+  /**
+   * Returns a short abbreviation for the unit.
+   */
+  @Override
+  public String getAbbreviation() {
+    return RESOURCE.getString("TimeUnit."+this+".Abb");
+  }
+
+  /**
+   * Returns a title for the unit.
+   */
+  @Override
+  public String getTitle() {
+    return RESOURCE.getString("TimeUnit."+this);
+  }
+
+  /**
+   * Returns the factor to convert 1 in "this" unit to another unit.<br>
+   * {@code x = A.getConversionFactor(B) <=> 1 A = x B}
+   * @param unit destination unit
+   */
+  @Override
+  public double getConversionFactor(TimeUnit unit) {
+    if ( this.equals(unit) )
+      return 1.0;
+    switch ( this ) {
+      case DAY: switch (unit) {
+                  case DAY:  return 1;
+                  case HOUR: return DAY_TO_HOUR;
+                  case MIN:  return DAY_TO_MIN;
+                  case SEC:  return DAY_TO_SEC;
+                  case MS:   return DAY_TO_MS;
+               }
+               break;
+      case HOUR: return DAY.getConversionFactor(unit) / DAY_TO_HOUR;
+      case MIN:  return DAY.getConversionFactor(unit) / DAY_TO_MIN;
+      case SEC:  return DAY.getConversionFactor(unit) / DAY_TO_SEC;
+      case MS:   return DAY.getConversionFactor(unit) / DAY_TO_MS;
+    }
+    throw new UnsupportedOperationException("Can not convert "+this+" to "+unit+".");
+  }
+
+  /**
+   * Converts a value to another unit. 
+   * @param unit unit to convert the value to
+   * @param value value to convert
+   */
+  @Override
+  public double convertTo(TimeUnit unit, double value) {
+    return value * getConversionFactor(unit);
+  }
+
+}
\ No newline at end of file

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -0,0 +1,34 @@
+package de.schmitzm.data;
+
+import de.schmitzm.lang.ResourceProvider;
+
+
+/**
+ * Interface for a unit enums. 
+ */
+public interface Unit<U extends Unit<?>> {
+  public static final ResourceProvider RESOURCE = DataUtil.RESOURCE;
+
+  /**
+   * Returns a conversion faction to convert 1 unit to another. 
+   * @param unit unit to convert the value to
+   */
+  public double getConversionFactor(U units);
+  
+  /**
+   * Converts a value to another unit. 
+   * @param unit unit to convert the value to
+   * @param value value to convert
+   */
+  public double convertTo(U unit, double value);
+  
+  /**
+   * Returns a short abbreviation for the unit.
+   */
+  public String getAbbreviation();
+
+  /**
+   * Returns a title for the unit.
+   */
+  public String getTitle();
+}
\ No newline at end of file

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle.properties	2012-05-11 22:50:16 UTC (rev 1995)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle.properties	2012-05-14 11:35:27 UTC (rev 1996)
@@ -70,6 +70,28 @@
 DistanceUnit.YARDS.Abb=yt
 DistanceUnit.FEET.Abb=ft
 
+TimeUnit.DAY=days
+TimeUnit.HOUR=hours
+TimeUnit.MIN=minutes
+TimeUnit.SEC=seconds
+TimeUnit.MS=milliseconds
+
+TimeUnit.DAY.Abb=day
+TimeUnit.HOUR.Abb=h
+TimeUnit.MIN.Abb=min
+TimeUnit.SEC.Abb=s
+TimeUnit.MS.Abb=ms
+
+SpeedUnit.KM_H=kilometers per hour
+SpeedUnit.M_S=meters per second
+SpeedUnit.MI_H=miles per hour
+SpeedUnit.YT_S=yards per second
+
+SpeedUnit.KM_H.Abb=km/h
+SpeedUnit.M_S.Abb=m/S
+SpeedUnit.MI_H.Abb=mi/h
+SpeedUnit.YT_S.Abb=yt/s
+
 RasterDim.GridWidth=Grid width
 RasterDim.GridHeight=Grid height
 RasterDim.CellWidth=Cell width

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle_de.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle_de.properties	2012-05-11 22:50:16 UTC (rev 1995)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle_de.properties	2012-05-14 11:35:27 UTC (rev 1996)
@@ -64,6 +64,17 @@
 DistanceUnit.YARDS=Yards
 DistanceUnit.FEET=Feet
 
+TimeUnit.DAY=Tage
+TimeUnit.HOUR=Stunden
+TimeUnit.MIN=Minuten
+TimeUnit.SEC=Sekunden
+TimeUnit.MS=Millisekunden
+
+SpeedUnit.KM_H=Kilometer pro Stunde
+SpeedUnit.M_S=Meter pro Sekunde
+SpeedUnit.MI_H=Meilen pro Stunde
+SpeedUnit.YT_S=Yards pro Sekunde
+
 RasterDim.GridWidth=Raster-Breite
 RasterDim.GridHeight=Raster-Hoehe
 RasterDim.CellWidth=Zellen-Breite

Added: trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java	                        (rev 0)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -0,0 +1,79 @@
+package de.schmitzm.data;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Test;
+
+import de.schmitzm.testing.TestingClass;
+
+public class UnitConversionTest extends TestingClass {
+
+  @Test
+  public void testUnitConversion_Distance() {
+    assertEquals(0.001,DistanceUnit.METERS.getConversionFactor(DistanceUnit.KM),0.0);
+    assertEquals(1000.0,DistanceUnit.KM.getConversionFactor(DistanceUnit.METERS),0.0);
+    
+    assertEquals(1.609344,DistanceUnit.MILES.getConversionFactor(DistanceUnit.KM),0.0);
+    assertEquals(1/1.609344,DistanceUnit.KM.getConversionFactor(DistanceUnit.MILES),0.0);
+
+    assertEquals(1760,DistanceUnit.MILES.getConversionFactor(DistanceUnit.YARDS),0.0);
+    assertEquals(1/1760.0,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.MILES),0.0);
+
+    assertEquals(3,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.FEET),0.0);
+    assertEquals(1/3.0,DistanceUnit.FEET.getConversionFactor(DistanceUnit.YARDS),0.0);
+
+    assertEquals(1760*3,DistanceUnit.MILES.getConversionFactor(DistanceUnit.FEET),0.0);
+    assertEquals(1/(1760.0*3),DistanceUnit.FEET.getConversionFactor(DistanceUnit.MILES),0.0);
+
+    assertEquals(0.9144,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.METERS),0.0001);
+    assertEquals(1.0936133,DistanceUnit.METERS.getConversionFactor(DistanceUnit.YARDS),0.000001);
+
+    assertEquals(3.2808399,DistanceUnit.METERS.getConversionFactor(DistanceUnit.FEET),0.001);
+    assertEquals(1/3.2808399,DistanceUnit.FEET.getConversionFactor(DistanceUnit.METERS),0.001);
+
+    assertEquals(1.234,DistanceUnit.METERS.convertDistanceTo(1234, DistanceUnit.KM),0.0);
+    assertEquals(12070.08,DistanceUnit.MILES.convertDistanceTo(7.5, DistanceUnit.METERS),0.0);
+
+    checkAllCombinations(DistanceUnit.values());
+}
+
+  @Test
+  public void testUnitConversion_Time() {
+    assertEquals(24,TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR),0.0);
+    assertEquals(24*60,TimeUnit.DAY.getConversionFactor(TimeUnit.MIN),0.0);
+    assertEquals(24*60*60,TimeUnit.DAY.getConversionFactor(TimeUnit.SEC),0.0);
+    assertEquals(24*60*60*1000,TimeUnit.DAY.getConversionFactor(TimeUnit.MS),0.0);
+
+    assertEquals(1.0/24,TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY),0.0);
+    assertEquals(1.0/(1000*60*60),TimeUnit.MS.getConversionFactor(TimeUnit.HOUR),0.0);
+
+    checkAllCombinations(TimeUnit.values());
+}
+
+
+  @Test
+  public void testUnitConversion_Speed() {
+    assertEquals(3.6,SpeedUnit.M_S.getConversionFactor(SpeedUnit.KM_H),0.0);
+    assertEquals(1/3.6,SpeedUnit.KM_H.getConversionFactor(SpeedUnit.M_S),0.0);
+
+    assertEquals(0.9144,SpeedUnit.YT_S.getConversionFactor(SpeedUnit.M_S),0.000001);
+    assertEquals(0.44704,SpeedUnit.MI_H.getConversionFactor(SpeedUnit.M_S),0.000001);
+    assertEquals(1760/(60.0*60.0),SpeedUnit.MI_H.getConversionFactor(SpeedUnit.YT_S),0.000001);
+    
+    checkAllCombinations(SpeedUnit.values());
+  }
+  
+  private <U extends Unit<?>> void checkAllCombinations(U[] units) {
+    // check if conversion to same unit equals 1.0 and if
+    // there is there is a conversion for all unit combinations 
+    for (Unit u1 : units)
+      for (Unit u2 : units) {
+        if ( u1.equals(u2) )
+          assertEquals(1.0, u1.convertTo(u2, 1.0), 0.0);
+        else
+          assertFalse(u1.convertTo(u2, 1.0) == 1.0);
+      }
+  }
+
+}
\ No newline at end of file

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/gui/RasterPositionLabel.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/gui/RasterPositionLabel.java	2012-05-11 22:50:16 UTC (rev 1995)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/gui/RasterPositionLabel.java	2012-05-14 11:35:27 UTC (rev 1996)
@@ -88,8 +88,9 @@
 	 * @see #resetCaptions(Map)
 	 * 
 	 * SK 12.12.09: static entfernt, da sonst das Sprachenumschalten die Cpation nicht gewechselt hat.
+	 * MS 06.05.12: Der Key aendert sich beim Sprachumschalten ja wohl nicht!
 	 */
-	public final String LABEL_PREFIX = RasterPositionLabel.class
+	public static final String LABEL_PREFIX = RasterPositionLabel.class
 			.getSimpleName()
 			+ ".LABEL_PREFIX";
 	



More information about the Schmitzm-commits mailing list