2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 110   Methods: 6
NCLOC: 52   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractMessages.java 87.5% 94.7% 100% 93.9%
coverage coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.hivemind.impl;
 16   
 17    import java.text.MessageFormat;
 18    import java.util.Locale;
 19   
 20    import org.apache.hivemind.HiveMind;
 21    import org.apache.hivemind.Messages;
 22    import org.apache.hivemind.util.Defense;
 23   
 24    /**
 25    * Abstract base class for implementations of {@link org.apache.hivemind.Messages}. Subclasses must
 26    * provide {@link #getLocale()}and {@link #findMessage(String)} implementations.
 27    *
 28    * @author Howard M. Lewis Ship
 29    * @since 1.1
 30    */
 31    public abstract class AbstractMessages implements Messages
 32    {
 33  241 public String format(String key, Object[] args)
 34    {
 35  241 String pattern = getMessage(key);
 36   
 37  241 for (int i = 0; i < args.length; i++)
 38    {
 39  475 Object arg = args[i];
 40   
 41  475 if (arg != null && arg instanceof Throwable)
 42  51 args[i] = extractMessage((Throwable) arg);
 43    }
 44   
 45    // This ugliness is mandated for JDK 1.3 compatibility, which has a bug
 46    // in MessageFormat ... the
 47    // pattern is applied in the constructor, using the system default Locale,
 48    // regardless of what locale is later specified!
 49    // It appears that the problem does not exist in JDK 1.4.
 50   
 51  241 MessageFormat messageFormat = new MessageFormat("");
 52  241 messageFormat.setLocale(getLocale());
 53  241 messageFormat.applyPattern(pattern);
 54   
 55  241 return messageFormat.format(args);
 56    }
 57   
 58  51 private String extractMessage(Throwable t)
 59    {
 60  51 String message = t.getMessage();
 61   
 62  51 return HiveMind.isNonBlank(message) ? message : t.getClass().getName();
 63    }
 64   
 65  69 public String format(String key, Object arg0)
 66    {
 67  69 return format(key, new Object[]
 68    { arg0 });
 69    }
 70   
 71  125 public String format(String key, Object arg0, Object arg1)
 72    {
 73  125 return format(key, new Object[]
 74    { arg0, arg1 });
 75    }
 76   
 77  28 public String format(String key, Object arg0, Object arg1, Object arg2)
 78    {
 79  28 return format(key, new Object[]
 80    { arg0, arg1, arg2 });
 81    }
 82   
 83  301 public String getMessage(String key)
 84    {
 85  301 Defense.notNull(key, "key");
 86   
 87  301 String result = findMessage(key);
 88   
 89  301 if (result == null)
 90  0 result = "[" + key.toUpperCase() + "]";
 91   
 92  301 return result;
 93    }
 94   
 95    /**
 96    * Concrete implementations must provide a non-null Locale.
 97    */
 98   
 99    protected abstract Locale getLocale();
 100   
 101    /**
 102    * Concrete implementations must implement this method.
 103    *
 104    * @param key
 105    * @return the localized message for the key, or null if no such message exists.
 106    */
 107   
 108    protected abstract String findMessage(String key);
 109   
 110    }