Skip to content

Commit

Permalink
79: More energy demos
Browse files Browse the repository at this point in the history
Task-Url: #79
  • Loading branch information
keilw committed Jul 25, 2020
1 parent cb99467 commit 0980113
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2019, Werner Keil and others.
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
Expand Down Expand Up @@ -47,7 +47,7 @@

/**
* @author Werner Keil
* @version 0.9
* @version 1.0
* @see {@link SaffirSimpsonHurricaneWindScale}
*/
public class ThePerfectStorm {
Expand All @@ -56,7 +56,7 @@ public class ThePerfectStorm {
* @param args
*/
public static void main(String[] args) {
final SaffirSimpsonHurricaneWindScale std = SaffirSimpsonHurricaneWindScale.of(
final var std = SaffirSimpsonHurricaneWindScale.of(
null, Quantities.getQuantity(38, MILE_PER_HOUR), TROPICAL_DEPRESSION);
System.out.println(std);

Expand Down Expand Up @@ -127,7 +127,6 @@ public static void main(String[] args) {
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);
//Quantity<?> timeToEvacuate = l.divide(metricSpeed); if you don't want to cast ;-)
System.out.println(String.format("Time to evacuate: %s", timeToEvacuate));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2019, Werner Keil and others.
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
Expand Down Expand Up @@ -40,7 +40,7 @@

/**
* @author Werner Keil
* @version 1.1
* @version 1.2
* @see {@link SaffirSimpsonHurricaneWindScale}
*/
public class ThePerfectStorm {
Expand All @@ -54,7 +54,7 @@ public static void main(String[] args) {
if ("-v".equals(args[0])) verbose = true;
}

final SaffirSimpsonHurricaneWindScale std = SaffirSimpsonHurricaneWindScale.of(
final var std = SaffirSimpsonHurricaneWindScale.of(
null, Quantities.getQuantity(38, MILE_PER_HOUR), TROPICAL_DEPRESSION);
System.out.println(std);

Expand Down Expand Up @@ -88,12 +88,13 @@ public static void main(String[] args) {
if (verbose) System.out.println(s5);

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

// With Java 14 the switch/case segment gets much more compact
var scale = switch (argument) {
case 0 -> sts;
case 1 -> s1;
Expand Down
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
*
*/
class Messages {
private static final String BUNDLE_NAME = "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,6 +1,6 @@
/*
* Units of Measurement Demos for Java
* Copyright (c) 2005-2019, Werner Keil and others.
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
Expand Down Expand Up @@ -56,7 +56,7 @@ public class ThePerfectStorm {
/**
* @param args
*/
public static void main(String[] args) {
public static void main(String[] args) {
final SaffirSimpsonHurricaneWindScale std = SaffirSimpsonHurricaneWindScale.of(
null, Quantities.getQuantity(38, MILE_PER_HOUR), TROPICAL_DEPRESSION);
System.out.println(std);
Expand Down Expand Up @@ -125,16 +125,17 @@ public static void main(String[] args) {
scale.getMinimum().to(KILOMETRE_PER_HOUR);

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

Quantity<Time> timeToEvacuate = l.divide(metricSpeed).asType(Time.class);
//Quantity<?> timeToEvacuate = l.divide(metricSpeed); //if you don't want to cast ;-)
SimpleUnitFormat.getInstance().label(timeToEvacuate.getUnit(), "h");
System.out.println(String.format("Time to evacuate: %s", timeToEvacuate));
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("No scale given.");
System.out.println(Messages.getString("ThePerfectStorm.4")); //$NON-NLS-1$
}
}
}
14 changes: 14 additions & 0 deletions console/systems/common/src/main/resources/messages.properties
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.
12 changes: 12 additions & 0 deletions console/systems/common/src/main/resources/messages_de.properties
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
12 changes: 12 additions & 0 deletions console/systems/common/src/main/resources/messages_es.properties
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

0 comments on commit 0980113

Please sign in to comment.