Android Open Source - AstroPhysCalc Time Unit






From Project

Back to project page AstroPhysCalc.

License

The source code is released under:

Apache License

If you think the Android project AstroPhysCalc listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.astrophyscalc;
/*  w w  w . ja  va  2s.  c  om*/
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

public enum TimeUnit implements Unit {

  MIC_S("micro s", 1E-6d),
  MS("ms", 1E-3d),
  S("s", 1d),
  MIN("minutes", 60d),
  HR("hours", 3600d),
  DAYS("days", 86400d),
  YEARS("years", 365d * 86400d);

  private String name;
  private double ratioToBase;

  private static TimeUnit base = S;
  private static TimeUnitComparator comparator = new TimeUnitComparator();

  private TimeUnit(final String name, final double ratioToBase) {
    this.name = name;
    this.ratioToBase = ratioToBase;
  }

  private static class TimeUnitComparator implements Comparator<Unit> {

    @Override
    public int compare(Unit lhs, Unit rhs) {
      final double diff = lhs.inBaseUnits() - rhs.inBaseUnits();
      if (diff < 0) {
        return -1;
      }
      else if (diff > 0) {
        return 1;
      }
      return 0;
    }
  }

  public int compare(TimeUnit unit) {
    return comparator.compare(this, unit);
  }

  public static TimeUnit getUnit(final String name) {
    TimeUnit[] units = TimeUnit.values();
    for (TimeUnit unit : units) {
      if (unit.getName().equals(name)) {
        return unit;
      }
    }
    return null;
  }

  public static Set<Unit> getAll() {
    TimeUnit[] units = TimeUnit.values();
    final Set<Unit> unitSet = new HashSet<Unit>();
    for (TimeUnit unit : units) {
      unitSet.add(unit);
    }
    return unitSet;
  }

  @Override
  public boolean isSameBaseAs(final Unit unit2) {
    return unit2 instanceof TimeUnit;
  }

  @Override
  public double inBaseUnits() {
    return ratioToBase;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public TimeUnit getBase() {
    return base;
  }

}




Java Source Code List

org.astrophyscalc.AstroPhysCalcActivity.java
org.astrophyscalc.CalcPage.java
org.astrophyscalc.CalcRow.java
org.astrophyscalc.Calculator.java
org.astrophyscalc.Constants.java
org.astrophyscalc.DimensionTest.java
org.astrophyscalc.Dimension.java
org.astrophyscalc.FractionTest.java
org.astrophyscalc.Fraction.java
org.astrophyscalc.LengthUnit.java
org.astrophyscalc.MassUnit.java
org.astrophyscalc.TimeUnitTest.java
org.astrophyscalc.TimeUnit.java
org.astrophyscalc.UnitAndDimTest.java
org.astrophyscalc.UnitAndDim.java
org.astrophyscalc.UnitExpressionTest.java
org.astrophyscalc.UnitExpression.java
org.astrophyscalc.UnitSelectionRule.java
org.astrophyscalc.UnitSelector.java
org.astrophyscalc.UnitSpinnerItem.java
org.astrophyscalc.UnitUtilTest.java
org.astrophyscalc.UnitUtil.java
org.astrophyscalc.Unit.java
org.astrophyscalc.ValueAndUnitsTest.java
org.astrophyscalc.ValueAndUnits.java