[Schmitzm-commits] r2299 - trunk/schmitzm-core/src/main/java/de/schmitzm/data

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Apr 18 20:33:33 CEST 2013


Author: mojays
Date: 2013-04-18 20:33:33 +0200 (Thu, 18 Apr 2013)
New Revision: 2299

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/RelativeUnit.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/DurationUnit.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java
Log:
RelativeUnit: new interface for relative units (e.g. duration or speed)
DurationUnit, SpeedUnit: conversion made generic by numerator and denominator conversion (using RelativeUnit)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/DurationUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/DurationUnit.java	2013-04-18 17:32:31 UTC (rev 2298)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/DurationUnit.java	2013-04-18 18:33:33 UTC (rev 2299)
@@ -36,29 +36,42 @@
  * function for conversion.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
-public enum DurationUnit implements Unit<DurationUnit> {
+public enum DurationUnit implements RelativeUnit<DurationUnit, TimeUnit, TimeUnit> {
   /** Hours per day */
-  HOURS_DAY,
+  HOURS_DAY(TimeUnit.HOUR,TimeUnit.DAY),
   /** Minutes per day */
-  MIN_DAY,
+  MIN_DAY(TimeUnit.MIN,TimeUnit.DAY),
   /** Seconds per day */
-  SEC_DAY,
+  SEC_DAY(TimeUnit.SEC,TimeUnit.DAY),
   /** Milliseconds per day */
-  MS_DAY,
+  MS_DAY(TimeUnit.MS,TimeUnit.DAY),
   /** Minutes per hour */
-  MIN_HOUR,
+  MIN_HOUR(TimeUnit.MIN,TimeUnit.HOUR),
   /** Seconds per hour */
-  SEC_HOUR,
+  SEC_HOUR(TimeUnit.SEC,TimeUnit.HOUR),
   /** Milliseconds per hour  */
-  MS_HOUR,
+  MS_HOUR(TimeUnit.MS,TimeUnit.HOUR),
   /** Seconds per minute */
-  SEC_MIN,
+  SEC_MIN(TimeUnit.SEC,TimeUnit.MIN),
   /** Milliseconds per minute  */
-  MS_MIN,
+  MS_MIN(TimeUnit.MS,TimeUnit.MIN),
   /** Milliseconds per second  */
-  MS_SEC;
+  MS_SEC(TimeUnit.MS,TimeUnit.SEC);
 
+  /** Numerator unit, e.g. {@linkplain TimeUnit#MIN minute} for {@linkplain #MIN_HOUR min/hour} */
+  protected TimeUnit numerator;
+  /** Denominator unit, e.g. {@linkplain TimeUnit#HOUR hour} for {@linkplain #MIN_HOUR min/hour} */
+  protected TimeUnit denominator;
+
   /**
+   * Creates new duration instance.
+   */
+  private DurationUnit(TimeUnit numerator, TimeUnit denominator) {
+    this.numerator = numerator;
+    this.denominator = denominator;
+  }
+  
+  /**
    * Returns a short abbreviation for the unit.
    */
   @Override
@@ -81,38 +94,45 @@
    */
   @Override
   public double getConversionFactor(DurationUnit unit) {
-    if ( this.equals(unit) )
-      return 1.0;
-    switch ( this ) {
-      case HOURS_DAY: switch (unit) {
-                        case MIN_DAY:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN);
-                        case SEC_DAY:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC);
-                        case MS_DAY:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS);
-                        case MIN_HOUR:  return TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
-                        case SEC_HOUR:  return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
-                        case MS_HOUR:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
-                        case SEC_MIN:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.MIN);
-                        case MS_MIN:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.MIN);
-                        case MS_SEC:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
-                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.SEC);
-                      }
-                      break;
-      case MIN_DAY:   return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN); 
-      case SEC_DAY:   return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC);
-      case MS_DAY:    return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.MS);
-      case MIN_HOUR:  return MIN_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
-      case SEC_HOUR:  return SEC_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
-      case MS_HOUR:   return MS_DAY.getConversionFactor(unit)  / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
-      case SEC_MIN:   return SEC_DAY.getConversionFactor(unit) / TimeUnit.MIN.getConversionFactor(TimeUnit.DAY);
-      case MS_MIN:    return MS_DAY.getConversionFactor(unit)  / TimeUnit.MIN.getConversionFactor(TimeUnit.DAY);
-      case MS_SEC:    return MS_DAY.getConversionFactor(unit)  / TimeUnit.SEC.getConversionFactor(TimeUnit.DAY);
-    }
-    throw new UnsupportedOperationException("Can not convert "+this+" to "+unit+".");
+//    if ( this.equals(unit) )
+//      return 1.0;
+//    switch ( this ) {
+//      case HOURS_DAY: switch (unit) {
+//                        case MIN_DAY:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN);
+//                        case SEC_DAY:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC);
+//                        case MS_DAY:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS);
+//                        case MIN_HOUR:  return TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
+//                        case SEC_HOUR:  return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
+//                        case MS_HOUR:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.HOUR);
+//                        case SEC_MIN:   return TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.MIN);
+//                        case MS_MIN:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.MIN);
+//                        case MS_SEC:    return TimeUnit.HOUR.getConversionFactor(TimeUnit.MS)
+//                                             / TimeUnit.DAY.getConversionFactor(TimeUnit.SEC);
+//                      }
+//                      break;
+//      case MIN_DAY:   return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.MIN); 
+//      case SEC_DAY:   return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.SEC);
+//      case MS_DAY:    return HOURS_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.MS);
+//      case MIN_HOUR:  return MIN_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
+//      case SEC_HOUR:  return SEC_DAY.getConversionFactor(unit) / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
+//      case MS_HOUR:   return MS_DAY.getConversionFactor(unit)  / TimeUnit.HOUR.getConversionFactor(TimeUnit.DAY);
+//      case SEC_MIN:   return SEC_DAY.getConversionFactor(unit) / TimeUnit.MIN.getConversionFactor(TimeUnit.DAY);
+//      case MS_MIN:    return MS_DAY.getConversionFactor(unit)  / TimeUnit.MIN.getConversionFactor(TimeUnit.DAY);
+//      case MS_SEC:    return MS_DAY.getConversionFactor(unit)  / TimeUnit.SEC.getConversionFactor(TimeUnit.DAY);
+//    }
+//    throw new UnsupportedOperationException("Can not convert "+this+" to "+unit+".");
+    
+    // Conversion factor for numerator unit (Zaehler)
+    double numConv = this.getNumeratorUnit().getConversionFactor(unit.getNumeratorUnit());
+    // Conversion factor for denominator unit (Nenner)
+    double denConv = this.getDenominatorUnit().getConversionFactor(unit.getDenominatorUnit());
+
+    return numConv / denConv;
   }
 
   /**
@@ -125,4 +145,22 @@
     return value * getConversionFactor(unit);
   }
 
+  /**
+   * Returns the numerator ("Zähler") unit.
+  * @return e.g. {@linkplain TimeUnit#MIN minute} for {@linkplain #MIN_HOUR min/hour} 
+   */
+ @Override
+  public TimeUnit getNumeratorUnit() {
+    return numerator;
+  }
+
+ /**
+  * Returns the denominator ("Nenner") unit.
+  * @return e.g. {@linkplain TimeUnit#HOUR hour} for {@linkplain #MIN_HOUR min/hour} 
+  */
+  @Override
+  public TimeUnit getDenominatorUnit() {
+    return denominator;
+  }
+
 }
\ No newline at end of file

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/RelativeUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/RelativeUnit.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/RelativeUnit.java	2013-04-18 18:33:33 UTC (rev 2299)
@@ -0,0 +1,18 @@
+package de.schmitzm.data;
+
+
+
+/**
+ * Interface for a unit enums, which unit is relative (e.g. duration hours/day, speed km/h). 
+ */
+public interface RelativeUnit<U extends RelativeUnit<?,?,?>, Z extends Unit<?>, N extends Unit<?>> extends Unit<U> {
+  /**
+   * Returns the numerator ("Zähler") unit.
+   */
+  public Z getNumeratorUnit();
+
+  /**
+   * Returns the denominator ("Nenner") unit.
+   */
+  public N getDenominatorUnit();
+}
\ No newline at end of file

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java	2013-04-18 17:32:31 UTC (rev 2298)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/SpeedUnit.java	2013-04-18 18:33:33 UTC (rev 2299)
@@ -36,20 +36,33 @@
  * function for conversion.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
-public enum SpeedUnit implements Unit<SpeedUnit> {
+public enum SpeedUnit implements RelativeUnit<SpeedUnit,DistanceUnit,TimeUnit> {
   /** Kilometers per hour */
-  KM_H,
+  KM_H(DistanceUnit.KM,TimeUnit.HOUR),
   /** Meters per second */
-  M_S,
+  M_S(DistanceUnit.METERS,TimeUnit.SEC),
   /** Miles per hour */
-  MI_H,
+  MI_H(DistanceUnit.MILES,TimeUnit.HOUR),
   /** Yards per second */
-  YT_S;
+  YT_S(DistanceUnit.YARDS,TimeUnit.SEC);
 
-  /** Factor to convert 1 m/s to km/h. */
-  public static final double MS_TO_KMH = 3.6;
-  
+//  /** Factor to convert 1 m/s to km/h. */
+//  public static final double MS_TO_KMH = 3.6;
+
+  /** Numerator unit, e.g. {@linkplain DistanceUnit#KM km} for {@linkplain #KM_H km/h} */
+  protected DistanceUnit numerator;
+  /** Denominator unit, e.g. {@linkplain TimeUnit#HOUR hour} for {@linkplain #KM_H km/h} */
+  protected TimeUnit denominator;
+
   /**
+   * Creates new duration instance.
+   */
+  private SpeedUnit(DistanceUnit numerator, TimeUnit denominator) {
+    this.numerator = numerator;
+    this.denominator = denominator;
+  }
+
+  /**
    * Returns a short abbreviation for the unit.
    */
   @Override
@@ -72,21 +85,28 @@
    */
   @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+".");
+//    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+".");
+
+    // Conversion factor for numerator unit (Zaehler)
+    double numConv = this.getNumeratorUnit().getConversionFactor(unit.getNumeratorUnit());
+    // Conversion factor for denominator unit (Nenner)
+    double denConv = this.getDenominatorUnit().getConversionFactor(unit.getDenominatorUnit());
+
+    return numConv / denConv;
   }
 
   /**
@@ -99,4 +119,21 @@
     return value * getConversionFactor(unit);
   }
 
+  /**
+   * Returns the numerator ("Zähler") unit.
+   * @return e.g. {@linkplain DistanceUnit#KM km} for {@linkplain #MIN_HOUR km/h} 
+   */
+  @Override
+  public DistanceUnit getNumeratorUnit() {
+    return numerator;
+  }
+
+  /**
+   * Returns the denominator ("Nenner") unit.
+   * @return e.g. {@linkplain TimeUnit#HOUR hour} for {@linkplain #MIN_HOUR km/h} 
+   */
+  @Override
+  public TimeUnit getDenominatorUnit() {
+    return denominator;
+  }
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list