[Schmitzm-commits] r1997 - in trunk/schmitzm-core/src: main/java/de/schmitzm/data main/resources/de/schmitzm/data/resource/locales test/java/de/schmitzm/data
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Mon May 14 14:26:39 CEST 2012
Author: mojays
Date: 2012-05-14 14:26:39 +0200 (Mon, 14 May 2012)
New Revision: 1997
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/data/TemperatureUnit.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.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-core/src/test/java/de/schmitzm/data/UnitConversionTest.java
Log:
new TemperatureUnit
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/TemperatureUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/TemperatureUnit.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/TemperatureUnit.java 2012-05-14 12:26:39 UTC (rev 1997)
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * 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 temerature units and provides several utility
+ * function for conversion.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public enum TemperatureUnit implements Unit<TemperatureUnit> {
+ /** Degree Celsius */
+ C,
+ /** Degree Kelvin */
+ K,
+ /** Degree Fahrenheit */
+ F,
+ /** Degree Réaumur */
+ R;
+
+ /**
+ * Returns a short abbreviation for the unit.
+ */
+ @Override
+ public String getAbbreviation() {
+ return RESOURCE.getString("TemperatureUnit."+this+".Abb");
+ }
+
+ /**
+ * Returns a title for the unit.
+ */
+ @Override
+ public String getTitle() {
+ return RESOURCE.getString("TemperatureUnit."+this);
+ }
+
+ /**
+ * Method not supported for temperatures!
+ * @param unit destination unit
+ */
+ @Override
+ public double getConversionFactor(TemperatureUnit unit) {
+ if ( this.equals(unit) )
+ return 1.0;
+ throw new UnsupportedOperationException("Constant conversion factors not supported for temperatures. User convertTo(.) method!");
+ }
+
+ /**
+ * Converts a value to another unit.
+ * @param unit unit to convert the value to
+ * @param value value to convert
+ */
+ @Override
+ public double convertTo(TemperatureUnit unit, double value) {
+ if ( this.equals(unit) )
+ return value;
+
+ switch ( this ) {
+ case C: switch (unit) {
+ case K: return value + 273.15;
+ case F: return value * 1.8 + 32;
+ case R: return value * 0.8;
+ }
+ break;
+ case K: switch (unit) {
+ case C: return value - 273.15;
+ case F: return value * 1.8 - 459.67;
+ case R: return (value - 273.15) * 0.8;
+ }
+ break;
+ case F: switch (unit) {
+ case C: return (value - 32) * 5/9.0;
+ case K: return (value + 459.67) * 5/9.0;
+ case R: return (value - 32) * 4/9.0;
+ }
+ break;
+ case R: switch (unit) {
+ case C: return value * 1.25;
+ case K: return value * 1.25 + 273.15;
+ case F: return value * 2.25 + 32;
+ }
+ break;
+ }
+ throw new UnsupportedOperationException("Can not convert "+this+" to "+unit+".");
+ }
+
+}
\ No newline at end of file
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java 2012-05-14 11:35:27 UTC (rev 1996)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java 2012-05-14 12:26:39 UTC (rev 1997)
@@ -8,6 +8,15 @@
*/
public interface Unit<U extends Unit<?>> {
public static final ResourceProvider RESOURCE = DataUtil.RESOURCE;
+
+ /** Provided units for distance. */
+ public static final DistanceUnit[] DISTANCE_UNITS = DistanceUnit.values();
+ /** Provided units for time. */
+ public static final TimeUnit[] TIME_UNITS = TimeUnit.values();
+ /** Provided units for speed. */
+ public static final SpeedUnit[] SPEED_UNITS = SpeedUnit.values();
+ /** Provided units for temperature. */
+ public static final TemperatureUnit[] TEMPERATURE_UNITS = TemperatureUnit.values();
/**
* Returns a conversion faction to convert 1 unit to another.
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-14 11:35:27 UTC (rev 1996)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle.properties 2012-05-14 12:26:39 UTC (rev 1997)
@@ -92,6 +92,16 @@
SpeedUnit.MI_H.Abb=mi/h
SpeedUnit.YT_S.Abb=yt/s
+TemperatureUnit.C=celsius
+TemperatureUnit.F=fahrenheit
+TemperatureUnit.K=kelvin
+TemperatureUnit.R=réaumur
+
+TemperatureUnit.C.Abb=°C
+TemperatureUnit.F.Abb=°F
+TemperatureUnit.K.Abb=K
+TemperatureUnit.R.Abb=°Ré
+
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-14 11:35:27 UTC (rev 1996)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/data/resource/locales/DataResourceBundle_de.properties 2012-05-14 12:26:39 UTC (rev 1997)
@@ -75,6 +75,11 @@
SpeedUnit.MI_H=Meilen pro Stunde
SpeedUnit.YT_S=Yards pro Sekunde
+TemperatureUnit.C=Celsius
+TemperatureUnit.F=Fahrenheit
+TemperatureUnit.K=Kelvin
+TemperatureUnit.R=Réaumur
+
RasterDim.GridWidth=Raster-Breite
RasterDim.GridHeight=Raster-Hoehe
RasterDim.CellWidth=Zellen-Breite
Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java 2012-05-14 11:35:27 UTC (rev 1996)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/data/UnitConversionTest.java 2012-05-14 12:26:39 UTC (rev 1997)
@@ -64,6 +64,28 @@
checkAllCombinations(SpeedUnit.values());
}
+ @Test
+ public void testUnitConversion_Temperature() {
+ assertEquals(1.0,TemperatureUnit.C.getConversionFactor(TemperatureUnit.C),0.0);
+ assertEquals(273.15,TemperatureUnit.C.convertTo(TemperatureUnit.K, 0.0),0.0);
+ assertEquals(-273.15,TemperatureUnit.K.convertTo(TemperatureUnit.C, 0.0),0.0);
+
+ assertEquals(255.372222,TemperatureUnit.F.convertTo(TemperatureUnit.K, 0.0),0.00001);
+ assertEquals(-17.777777,TemperatureUnit.F.convertTo(TemperatureUnit.C, 0.0),0.00001);
+ assertEquals(32,TemperatureUnit.C.convertTo(TemperatureUnit.F, 0.0),0.00001);
+
+ assertEquals(0.0,TemperatureUnit.R.convertTo(TemperatureUnit.C, 0.0),0.00001);
+ assertEquals(32,TemperatureUnit.R.convertTo(TemperatureUnit.F, 0.0),0.00001);
+ assertEquals(273.15,TemperatureUnit.R.convertTo(TemperatureUnit.K, 0.0),0.00001);
+
+ assertEquals(80.0,TemperatureUnit.C.convertTo(TemperatureUnit.R, 100.0),0.00001);
+ assertEquals(212,TemperatureUnit.C.convertTo(TemperatureUnit.F, 100.0),0.00001);
+ assertEquals(373.15,TemperatureUnit.C.convertTo(TemperatureUnit.K, 100.0),0.00001);
+
+ checkAllCombinations(TemperatureUnit.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
More information about the Schmitzm-commits
mailing list