changeset 553:e7413ea27f17

Refactor Translate class review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002834.html reviewed-by: jerboaa
author Mario Torre <neugens.limasoftware@gmail.com>
date Mon, 20 Aug 2012 16:03:04 +0200
parents b0f4e49b9f75
children 6ca85a6f23c7
files common/core/pom.xml common/core/src/main/java/com/redhat/thermostat/common/ApplicationInfo.java common/core/src/main/java/com/redhat/thermostat/common/Version.java common/core/src/main/java/com/redhat/thermostat/common/locale/LocaleResources.java common/core/src/main/java/com/redhat/thermostat/common/locale/Translate.java common/core/src/test/java/com/redhat/thermostat/common/ApplicationInfoTest.java common/core/src/test/java/com/redhat/thermostat/common/VersionTest.java common/core/src/test/java/com/redhat/thermostat/common/locale/TranslateTest.java launcher/src/test/java/com/redhat/thermostat/launcher/LauncherTest.java
diffstat 9 files changed, 37 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/pom.xml	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/pom.xml	Mon Aug 20 16:03:04 2012 +0200
@@ -92,6 +92,7 @@
             <Private-Package>
               com.redhat.thermostat.test,
               com.redhat.thermostat.tools,
+              com.redhat.thermostat.common.locale.impl
             </Private-Package>
             <!-- Do not autogenerate uses clauses in Manifests -->
             <_nouses>true</_nouses>
--- a/common/core/src/main/java/com/redhat/thermostat/common/ApplicationInfo.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/ApplicationInfo.java	Mon Aug 20 16:03:04 2012 +0200
@@ -36,8 +36,6 @@
 
 package com.redhat.thermostat.common;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
@@ -45,6 +43,7 @@
 import java.util.logging.Logger;
 
 import com.redhat.thermostat.common.locale.LocaleResources;
+import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 public class ApplicationInfo {
@@ -55,6 +54,8 @@
     
     private Properties appInfo;
 
+    private static final Translate t = LocaleResources.createLocalizer();
+    
     public ApplicationInfo() {
         appInfo = new Properties();
         // the properties file should be in the same package as this class
@@ -69,7 +70,7 @@
     }
 
     public String getName() {
-        return appInfo.getProperty("APP_NAME", localize(LocaleResources.MISSING_INFO));
+        return appInfo.getProperty("APP_NAME", t.localize(LocaleResources.MISSING_INFO));
     }
 
     public Version getVersion() {
@@ -77,27 +78,27 @@
     }
 
     public String getDescription() {
-        return localize(LocaleResources.APPLICATION_INFO_DESCRIPTION);
+        return t.localize(LocaleResources.APPLICATION_INFO_DESCRIPTION);
     }
 
     public String getReleaseDate() {
-        return appInfo.getProperty("APP_RELEASE_DATE", localize(LocaleResources.MISSING_INFO));
+        return appInfo.getProperty("APP_RELEASE_DATE", t.localize(LocaleResources.MISSING_INFO));
     }
 
     public String getCopyright() {
-        return appInfo.getProperty("APP_COPYRIGHT", localize(LocaleResources.MISSING_INFO));
+        return appInfo.getProperty("APP_COPYRIGHT", t.localize(LocaleResources.MISSING_INFO));
     }
 
     public String getLicenseSummary() {
-        return localize(LocaleResources.APPLICATION_INFO_LICENSE);
+        return t.localize(LocaleResources.APPLICATION_INFO_LICENSE);
     }
 
     public String getEmail() {
-        return appInfo.getProperty("APP_EMAIL", localize(LocaleResources.MISSING_INFO));
+        return appInfo.getProperty("APP_EMAIL", t.localize(LocaleResources.MISSING_INFO));
     }
 
     public String getWebsite() {
-        return appInfo.getProperty("APP_WEBSITE", localize(LocaleResources.MISSING_INFO));
+        return appInfo.getProperty("APP_WEBSITE", t.localize(LocaleResources.MISSING_INFO));
     }
 
 }
--- a/common/core/src/main/java/com/redhat/thermostat/common/Version.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/Version.java	Mon Aug 20 16:03:04 2012 +0200
@@ -35,14 +35,13 @@
  */
 package com.redhat.thermostat.common;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
-
 import java.text.MessageFormat;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.FrameworkUtil;
 
 import com.redhat.thermostat.common.locale.LocaleResources;
+import com.redhat.thermostat.common.locale.Translate;
 
 /**
  * Utility class for Thermostat version info.
@@ -75,8 +74,9 @@
      */
     public String getVersionInfo() {
         ApplicationInfo appInfo = new ApplicationInfo();
+        Translate t = LocaleResources.createLocalizer();
         String format = MessageFormat.format(
-                localize(LocaleResources.APPLICATION_VERSION_INFO),
+                t.localize(LocaleResources.APPLICATION_VERSION_INFO),
                 appInfo.getName())
                 + " " + VERSION_NUMBER_FORMAT;
         return String.format(format, getMajor(), getMinor(), getMicro());
--- a/common/core/src/main/java/com/redhat/thermostat/common/locale/LocaleResources.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/locale/LocaleResources.java	Mon Aug 20 16:03:04 2012 +0200
@@ -45,6 +45,10 @@
     APPLICATION_VERSION_INFO,
     ;
 
-    static final String RESOURCE_BUNDLE =
+    public static final String RESOURCE_BUNDLE =
             "com.redhat.thermostat.common.locale.strings";
+    
+    public static Translate createLocalizer() {
+        return new Translate(RESOURCE_BUNDLE);
+    }
 }
--- a/common/core/src/main/java/com/redhat/thermostat/common/locale/Translate.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/locale/Translate.java	Mon Aug 20 16:03:04 2012 +0200
@@ -39,19 +39,19 @@
 import java.text.MessageFormat;
 import java.util.ResourceBundle;
 
+
 public class Translate {
 
-    private static ResourceBundle resourceBundle = null;
-
-    static {
-        resourceBundle = ResourceBundle.getBundle(LocaleResources.RESOURCE_BUNDLE);
+    private ResourceBundle resourceBundle = null;
+    public Translate(String stringResources) {
+        resourceBundle = ResourceBundle.getBundle(stringResources);
     }
-
-    public static String localize(LocaleResources toTranslate) {
+    
+    public <T extends Enum<?>> String localize(T toTranslate) {
         return resourceBundle.getString(toTranslate.name());
     }
 
-    public static String localize(LocaleResources toTranslate, String... params) {
+    public <T extends Enum<?>> String localize(T toTranslate, String... params) {
         return MessageFormat.format(localize(toTranslate), (Object[]) params);
     }
 }
--- a/common/core/src/test/java/com/redhat/thermostat/common/ApplicationInfoTest.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/test/java/com/redhat/thermostat/common/ApplicationInfoTest.java	Mon Aug 20 16:03:04 2012 +0200
@@ -36,7 +36,6 @@
 
 package com.redhat.thermostat.common;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
 import static org.junit.Assert.*;
 
 import java.util.Locale;
@@ -61,7 +60,7 @@
     @Test
     public void testProperties() {
         ApplicationInfo appInfo = new ApplicationInfo();
-        assertFalse(appInfo.getName().compareTo(localize(LocaleResources.MISSING_INFO)) == 0);
+        assertFalse(appInfo.getName().compareTo(LocaleResources.createLocalizer().localize(LocaleResources.MISSING_INFO)) == 0);
     }
 
     @After
--- a/common/core/src/test/java/com/redhat/thermostat/common/VersionTest.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/test/java/com/redhat/thermostat/common/VersionTest.java	Mon Aug 20 16:03:04 2012 +0200
@@ -36,7 +36,6 @@
 
 package com.redhat.thermostat.common;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -166,7 +165,7 @@
     private String createFormat() {
         ApplicationInfo appInfo = new ApplicationInfo();
         String format = MessageFormat.format(
-                localize(LocaleResources.APPLICATION_VERSION_INFO),
+                LocaleResources.createLocalizer().localize(LocaleResources.APPLICATION_VERSION_INFO),
                 appInfo.getName())
                 + " " + Version.VERSION_NUMBER_FORMAT;
         return format;
--- a/common/core/src/test/java/com/redhat/thermostat/common/locale/TranslateTest.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/common/core/src/test/java/com/redhat/thermostat/common/locale/TranslateTest.java	Mon Aug 20 16:03:04 2012 +0200
@@ -46,8 +46,6 @@
 import org.junit.Before;
 import org.junit.Test;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
-
 public class TranslateTest {
 
     private Locale lang;
@@ -65,16 +63,18 @@
     
     @Test
     public void testLocalizeWithoutArguments() {
-        String testString = localize(LocaleResources.MISSING_INFO);
+        String testString = LocaleResources.createLocalizer().localize(LocaleResources.MISSING_INFO);
         Assert.assertEquals("Missing Information", testString);
     }
     
     @Test
-    public void testLocalizeWithArguments() {        
-        String testString = localize(LocaleResources.APPLICATION_INFO_DESCRIPTION);
+    public void testLocalizeWithArguments() {
+        Translate t = LocaleResources.createLocalizer();
+        
+        String testString = t.localize(LocaleResources.APPLICATION_INFO_DESCRIPTION);
         Assert.assertEquals("A monitoring and serviceability tool for OpenJDK",
                             testString);
-        testString = localize(LocaleResources.APPLICATION_INFO_LICENSE);
+        testString = t.localize(LocaleResources.APPLICATION_INFO_LICENSE);
         Assert.assertEquals("Licensed under GPLv2+ with Classpath exception",
                 testString);
     }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/LauncherTest.java	Mon Aug 20 15:42:19 2012 +0200
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/LauncherTest.java	Mon Aug 20 16:03:04 2012 +0200
@@ -36,7 +36,6 @@
 
 package com.redhat.thermostat.launcher;
 
-import static com.redhat.thermostat.common.locale.Translate.localize;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
@@ -73,6 +72,7 @@
 import com.redhat.thermostat.common.cli.SimpleArgumentSpec;
 import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.locale.LocaleResources;
+import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.launcher.internal.HelpCommand;
 import com.redhat.thermostat.launcher.internal.LauncherImpl;
 import com.redhat.thermostat.test.TestCommandContextFactory;
@@ -327,8 +327,9 @@
         int micro = 0;
         
         ApplicationInfo appInfo = new ApplicationInfo();
+        Translate t = LocaleResources.createLocalizer();
         String format = MessageFormat.format(
-                localize(LocaleResources.APPLICATION_VERSION_INFO),
+                t.localize(LocaleResources.APPLICATION_VERSION_INFO),
                 appInfo.getName())
                 + " " + Version.VERSION_NUMBER_FORMAT;