Skip to content

Commit

Permalink
94: Translate Hurricane Demo
Browse files Browse the repository at this point in the history
Task-Url: #94
  • Loading branch information
keilw committed Jul 26, 2020
1 parent 6376051 commit 70b07ee
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Units of Measurement Demos for Java
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-385, Units of Measurement nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.demo.systems.common;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
* Local helper class for i18n and resource bundles
* @author Werner Keil
*
*/
class Messages {
private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$

private static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

private Messages() {
}

/**
* Loads the string using the default locale or using a language provided via <code>-Dlanguage={lang}</code>.
* @param key
* @param missingKeyOnly if the key is missing, return the key only and no "!"
* @return the local string or an error message including the key
*/
public static String getString(String key, boolean missingKeyOnly) {
final String language = System.getProperty("language");
if (language != null) {
if (!language.equals(RESOURCE_BUNDLE.getLocale().getLanguage())) {
//System.out.println(language);
RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, new Locale(language));
}
}
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}

/**
* Retrieves the string
* @param key
* @return
*/
public static String getString(String key) {
return getString(key, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Units of Measurement Console Demos
* Units of Measurement Demos for Java
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
Expand All @@ -25,7 +25,7 @@
*/
package tech.uom.demo.systems.common;

import static tech.units.indriya.unit.Units.KILOMETRE_PER_HOUR;
import static javax.measure.MetricPrefix.KILO;
import static tech.units.indriya.unit.Units.METRE;
import static tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale.Category.FIVE;
import static tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale.Category.FOUR;
Expand All @@ -34,99 +34,101 @@
import static tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale.Category.TROPICAL_DEPRESSION;
import static tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale.Category.TROPICAL_STORM;
import static tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale.Category.TWO;
import static javax.measure.MetricPrefix.KILO;
import static systems.uom.common.USCustomary.MILE_PER_HOUR;
import static tech.units.indriya.quantity.Quantities.getQuantity;
import static tech.units.indriya.unit.Units.KILOMETRE_PER_HOUR;

import javax.measure.Quantity;
import javax.measure.quantity.Length;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Time;

import tech.units.indriya.quantity.Quantities;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.uom.demo.systems.common.types.SaffirSimpsonHurricaneWindScale;

/**
* @author Werner Keil
* @version 1.0
* @version 1.2
* @see {@link SaffirSimpsonHurricaneWindScale}
*/
public class ThePerfectStorm {
// Constants
private static final SaffirSimpsonHurricaneWindScale STD = SaffirSimpsonHurricaneWindScale.of(
null, getQuantity(38, MILE_PER_HOUR), TROPICAL_DEPRESSION);
private static final SaffirSimpsonHurricaneWindScale STS = SaffirSimpsonHurricaneWindScale.of(
getQuantity(39, MILE_PER_HOUR), getQuantity(73, MILE_PER_HOUR), TROPICAL_STORM);
private static final SaffirSimpsonHurricaneWindScale S1 = SaffirSimpsonHurricaneWindScale.of(
getQuantity(74, MILE_PER_HOUR), getQuantity(95, MILE_PER_HOUR), ONE);
private static final SaffirSimpsonHurricaneWindScale S2 = SaffirSimpsonHurricaneWindScale.of(
getQuantity(96, MILE_PER_HOUR), getQuantity(110, MILE_PER_HOUR), TWO);
private static final SaffirSimpsonHurricaneWindScale S3 = SaffirSimpsonHurricaneWindScale.of(
getQuantity(111, MILE_PER_HOUR), getQuantity(129, MILE_PER_HOUR), THREE);
private static final SaffirSimpsonHurricaneWindScale S4 = SaffirSimpsonHurricaneWindScale.of(
getQuantity(130, MILE_PER_HOUR), getQuantity(156, MILE_PER_HOUR), FOUR);
private static final SaffirSimpsonHurricaneWindScale S5 = SaffirSimpsonHurricaneWindScale.of(
getQuantity(157, MILE_PER_HOUR), null, FIVE);

private static final double DEFAULT_DIST_KM = 500d;

/**
* @param args
*/
public static void main(String[] args) {
final var std = SaffirSimpsonHurricaneWindScale.of(
null, Quantities.getQuantity(38, MILE_PER_HOUR), TROPICAL_DEPRESSION);
System.out.println(std);

final var sts = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(39, MILE_PER_HOUR),
Quantities.getQuantity(73, MILE_PER_HOUR), TROPICAL_STORM);
System.out.println(sts);

final var s1 = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(74, MILE_PER_HOUR),
Quantities.getQuantity(95, MILE_PER_HOUR), ONE);
System.out.println(s1);

final var s2 = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(96, MILE_PER_HOUR),
Quantities.getQuantity(110, MILE_PER_HOUR), TWO);
System.out.println(s2);
public static void main(String[] args) {
System.out.println(STD);
System.out.println(STS);
System.out.println(S1);
System.out.println(S2);
System.out.println(S3);
System.out.println(S4);
System.out.println(S5);

final var s3 = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(111, MILE_PER_HOUR),
Quantities.getQuantity(129, MILE_PER_HOUR), THREE);
System.out.println(s3);

final var s4 = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(130, MILE_PER_HOUR),
Quantities.getQuantity(156, MILE_PER_HOUR), FOUR);
System.out.println(s4);

final var s5 = SaffirSimpsonHurricaneWindScale.of(
Quantities.getQuantity(157, MILE_PER_HOUR), null, FIVE);
System.out.println(s5);

int argument = -1;
if (args!= null && args.length>0) {
var argument = -1;
if (args != null && args.length > 0) {
argument = Integer.valueOf(args[0]).intValue();
}

SaffirSimpsonHurricaneWindScale scale = null;
var distKm = DEFAULT_DIST_KM;
if (args != null && args.length > 1) {
distKm = Double.valueOf(args[1]).doubleValue();
}

var scale = STS;
switch (argument) {
case 0:
scale = sts;
scale = STS;
break;
case 1:
scale = s1;
scale = S1;
break;
case 2:
scale = s2;
scale = S2;
break;
case 3:
scale = s3;
scale = S3;
break;
case 4:
scale = s4;
scale = S4;
break;
case 5:
scale = s5;
scale = S5;
break;
default:
scale = std;
scale = STD;
}

final Quantity<Speed> metricSpeed = scale.hasMaximum() ?
scale.getMaximum().to(KILOMETRE_PER_HOUR) :
scale.getMinimum().to(KILOMETRE_PER_HOUR);
if (scale != null) {
// If the wind scale has a maximum we take that assuming the worst case, otherwise the minimum
final var metricSpeed = scale.hasMaximum() ?
scale.getMaximum().to(KILOMETRE_PER_HOUR) :
scale.getMinimum().to(KILOMETRE_PER_HOUR);

System.out.print(metricSpeed);
System.out.println(" (" + scale.getCategory() + ")");
Quantity<Length> l = Quantities.getQuantity(500, KILO(METRE));
System.out.println(String.format("Distance: %s", l));
Quantity<Time> timeToEvacuate = l.divide(metricSpeed).asType(Time.class);
System.out.println(String.format("Time to evacuate: %s", timeToEvacuate));
System.out.print(metricSpeed);
System.out.println(" (" + Messages.getString("SaffirSimpsonHurricaneWindScale." + scale.getCategory(), true)
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
final var distance = getQuantity(distKm, KILO(METRE));
System.out.println(String.format(Messages.getString("ThePerfectStorm.1"), distance)); //$NON-NLS-1$

final var timeToEvacuate = distance.divide(metricSpeed).asType(Time.class);
SimpleUnitFormat.getInstance().label(timeToEvacuate.getUnit(), Messages.getString("ThePerfectStorm.2")); //$NON-NLS-1$
System.out.println(String.format(Messages.getString("ThePerfectStorm.3"), timeToEvacuate)); //$NON-NLS-1$
} else {
System.out.println(Messages.getString("ThePerfectStorm.4")); //$NON-NLS-1$
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

SaffirSimpsonHurricaneWindScale.FIVE = Category 5
SaffirSimpsonHurricaneWindScale.FOUR = Category 4
SaffirSimpsonHurricaneWindScale.ONE = Category 1
SaffirSimpsonHurricaneWindScale.THREE = Category 3
SaffirSimpsonHurricaneWindScale.TROPICAL_DEPRESSION = Tropical Depression
SaffirSimpsonHurricaneWindScale.TROPICAL_STORM = Tropical Storm
SaffirSimpsonHurricaneWindScale.TWO = Category 2
SaffirSimpsonHurricaneWindScale.UNKNOWN = Unknown

ThePerfectStorm.1 = Distance: %s
ThePerfectStorm.2 = h
ThePerfectStorm.3 = Time to evacuate: %s
ThePerfectStorm.4 = No scale given.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ThePerfectStorm.1=Entfernung: %s
ThePerfectStorm.2=std
ThePerfectStorm.3=Zeit zu Evakuieren: %s
ThePerfectStorm.4=Keine Skala angegeben.
SaffirSimpsonHurricaneWindScale.TROPICAL_DEPRESSION=Tropische Depression
SaffirSimpsonHurricaneWindScale.TROPICAL_STORM=Tropischer Sturm
SaffirSimpsonHurricaneWindScale.ONE=Kategorie 1
SaffirSimpsonHurricaneWindScale.TWO=Kategorie 2
SaffirSimpsonHurricaneWindScale.THREE=Kategorie 3
SaffirSimpsonHurricaneWindScale.FOUR=Kategorie 4
SaffirSimpsonHurricaneWindScale.FIVE=Kategorie 5
SaffirSimpsonHurricaneWindScale.UNKNOWN=Unbekannt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ThePerfectStorm.1=Distancia: %s
ThePerfectStorm.2=h
ThePerfectStorm.3=Tiempo de evacuación: %s
ThePerfectStorm.4=No se especifica escala.
SaffirSimpsonHurricaneWindScale.TROPICAL_DEPRESSION=Depresión tropical
SaffirSimpsonHurricaneWindScale.TROPICAL_STORM=Tormenta tropical
SaffirSimpsonHurricaneWindScale.ONE=Categoría 1
SaffirSimpsonHurricaneWindScale.TWO=Categoría 2
SaffirSimpsonHurricaneWindScale.THREE=Categoría 3
SaffirSimpsonHurricaneWindScale.FOUR=Categoría 4
SaffirSimpsonHurricaneWindScale.FIVE=Categoría 5
SaffirSimpsonHurricaneWindScale.UNKNOWN=Desconocido
Loading

0 comments on commit 70b07ee

Please sign in to comment.