Author: hlship
Date: Tue Nov 28 12:41:30 2006
New Revision: 480196

URL: http://svn.apache.org/viewvc?view=rev&rev=480196
Log:
Split an abstract base class off of MessagesImpl.

Added:
    
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/util/AbstractMessages.java
Modified:
    
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/MessagesImpl.java
    
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java

Modified: 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/MessagesImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/MessagesImpl.java?view=diff&rev=480196&r1=480195&r2=480196
==============================================================================
--- 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/MessagesImpl.java
 (original)
+++ 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/MessagesImpl.java
 Tue Nov 28 12:41:30 2006
@@ -12,99 +12,64 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.apache.tapestry.ioc.internal.util;
-
-import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newSet;
-import static 
org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeMap;
-
-import java.util.Enumeration;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
-import java.util.Set;
-
-import org.apache.tapestry.ioc.MessageFormatter;
+package org.apache.tapestry.ioc.internal.util;
+
+import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
+
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+
 import org.apache.tapestry.ioc.Messages;
-
-/**
- * Implementation of [EMAIL PROTECTED] org.apache.tapestry.ioc.Messages} based 
around a
- * [EMAIL PROTECTED] java.util.ResourceBundle}.
- */
-public class MessagesImpl implements Messages
-{
-    private final ResourceBundle _bundle;
-
-    private final Set<String> _keys = newSet();
-
-    /** String key to MF instance. */
-    private Map<String, MessageFormatter> _cache = newThreadSafeMap();
-
-    /**
-     * Finds the messages for a given Messages utility class. Strings the 
trailing "Messages" and
-     * replaces it with "Strings" to form the base path. Loads the bundle 
using the default locale,
-     * and the class' class loader.
-     * 
-     * @param forClass
-     * @return Messages for the class
-     */
-    public static Messages forClass(Class forClass)
-    {
-        String className = forClass.getName();
-        String stringsClassName = className.replaceAll("Messages$", "Strings");
-
-        ResourceBundle bundle = ResourceBundle.getBundle(
-                stringsClassName,
-                Locale.getDefault(),
-                forClass.getClassLoader());
-
-        return new MessagesImpl(bundle);
-    }
-
-    public MessagesImpl(ResourceBundle bundle)
-    {
-        _bundle = bundle;
-
-        // Our best (threadsafe) chance to determine all the available keys.
-        Enumeration<String> e = _bundle.getKeys();
-        while (e.hasMoreElements())
-            _keys.add(e.nextElement());
-    }
-
-    public boolean contains(String key)
-    {
-        return _keys.contains(key);
-    }
-
-    public String get(String key)
-    {
-        if (_keys.contains(key))
-            return _bundle.getString(key);
-
-        return "[" + key.toUpperCase() + "]";
-    }
-
-    public MessageFormatter getFormatter(String key)
-    {
-        MessageFormatter result = _cache.get(key);
-
-        if (result == null)
-        {
-            result = buildMessageFormatter(key);
-            _cache.put(key, result);
-        }
-
-        return result;
-    }
-
-    private MessageFormatter buildMessageFormatter(String key)
-    {
-        String format = get(key);
-
-        return new MessageFormatterImpl(format);
-    }
-
-    public String format(String key, Object... args)
-    {
-        return getFormatter(key).format(args);
-    }
-}
+import org.apache.tapestry.ioc.util.AbstractMessages;
+
+/**
+ * Implementation of [EMAIL PROTECTED] org.apache.tapestry.ioc.Messages} based 
around a
+ * [EMAIL PROTECTED] java.util.ResourceBundle}.
+ */
+public class MessagesImpl extends AbstractMessages
+{
+    private final Map<String, String> _properties = newMap();
+
+    /**
+     * Finds the messages for a given Messages utility class. Strings the 
trailing "Messages" and
+     * replaces it with "Strings" to form the base path. Loads the bundle 
using the default locale,
+     * and the class' class loader.
+     * 
+     * @param forClass
+     * @return Messages for the class
+     */
+    public static Messages forClass(Class forClass)
+    {
+        String className = forClass.getName();
+        String stringsClassName = className.replaceAll("Messages$", "Strings");
+
+        ResourceBundle bundle = ResourceBundle.getBundle(
+                stringsClassName,
+                Locale.getDefault(),
+                forClass.getClassLoader());
+
+        return new MessagesImpl(bundle);
+    }
+
+    public MessagesImpl(ResourceBundle bundle)
+    {
+        // Our best (threadsafe) chance to determine all the available keys.
+        Enumeration<String> e = bundle.getKeys();
+        while (e.hasMoreElements())
+        {
+            String key = e.nextElement();
+            String value = bundle.getString(key);
+
+            _properties.put(key, value);
+        }
+    }
+
+    @Override
+    protected String valueForKey(String key)
+    {
+        return _properties.get(key);
+    }
+
+}

Modified: 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java?view=diff&rev=480196&r1=480195&r2=480196
==============================================================================
--- 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java
 (original)
+++ 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/test/IOCTestCase.java
 Tue Nov 28 12:41:30 2006
@@ -35,8 +35,8 @@
 import org.apache.tapestry.ioc.def.DecoratorDef;
 import org.apache.tapestry.ioc.def.ModuleDef;
 import org.apache.tapestry.ioc.def.ServiceDef;
-import org.apache.tapestry.ioc.services.ClassFactory;
 import org.apache.tapestry.ioc.services.SymbolSource;
+import org.apache.tapestry.ioc.services.ThreadLocale;
 import org.apache.tapestry.ioc.services.TypeCoercer;
 
 /** Add factory and trainer methods for the public interfaces of Tapestry IOC. 
*/
@@ -259,7 +259,7 @@
     protected final void train_expandSymbols(SymbolSource source, String input)
     {
         train_expandSymbols(source, input, input);
-    
+
     }
 
     protected final void train_expandSymbols(SymbolSource source, String 
input, String expanded)
@@ -272,4 +272,8 @@
         return newMock(SymbolSource.class);
     }
 
+    protected final ThreadLocale newThreadLocale()
+    {
+        return newMock(ThreadLocale.class);
+    }
 }

Added: 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/util/AbstractMessages.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/util/AbstractMessages.java?view=auto&rev=480196
==============================================================================
--- 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/util/AbstractMessages.java
 (added)
+++ 
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/util/AbstractMessages.java
 Tue Nov 28 12:41:30 2006
@@ -0,0 +1,81 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.ioc.util;
+
+import static 
org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeMap;
+
+import java.util.Map;
+
+import org.apache.tapestry.ioc.MessageFormatter;
+import org.apache.tapestry.ioc.Messages;
+import org.apache.tapestry.ioc.internal.util.MessageFormatterImpl;
+
+/**
+ * Abstract implementation of [EMAIL PROTECTED] Messages} that doesn't know 
where values come from (that
+ * information is supplied in a subclass, via the [EMAIL PROTECTED] 
#valueForKey(String)} method).
+ */
+public abstract class AbstractMessages implements Messages
+{
+    /** String key to MF instance. */
+    private Map<String, MessageFormatter> _cache = newThreadSafeMap();
+
+    /**
+     * Invoked to provide the value for a particular key. This may be invoked 
multiple times even
+     * for the same key.
+     * 
+     * @param key
+     *            the key to obtain a value for
+     * @return the value for the key, or null if this instance can not provide 
the value
+     */
+    protected abstract String valueForKey(String key);
+
+    public boolean contains(String key)
+    {
+        return valueForKey(key) != null;
+    }
+
+    public String get(String key)
+    {
+        if (contains(key)) return valueForKey(key);
+
+        return "[" + key.toUpperCase() + "]";
+    }
+
+    public MessageFormatter getFormatter(String key)
+    {
+        MessageFormatter result = _cache.get(key);
+
+        if (result == null)
+        {
+            result = buildMessageFormatter(key);
+            _cache.put(key, result);
+        }
+
+        return result;
+    }
+
+    private MessageFormatter buildMessageFormatter(String key)
+    {
+        String format = get(key);
+
+        return new MessageFormatterImpl(format);
+    }
+
+    public String format(String key, Object... args)
+    {
+        return getFormatter(key).format(args);
+    }
+
+}


Reply via email to