This project has retired. For details please refer to its Attic page.
Coverage Report
Coverage Report - org.apache.ws.scout.registry.infomodel.InternationalStringImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
InternationalStringImpl
100%
33/33
100%
6/6
1.611
InternationalStringImpl$MapKey
85%
17/20
50%
4/8
1.611
 
 1  
 /*
 2  
  * Copyright 2001-2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.ws.scout.registry.infomodel;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.Locale;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.xml.registry.JAXRException;
 27  
 import javax.xml.registry.infomodel.InternationalString;
 28  
 import javax.xml.registry.infomodel.LocalizedString;
 29  
 
 30  
 /**
 31  
  * For futher details, look into the JAXR API Javadoc.
 32  
  *
 33  
  * @author Anil Saldhana  <anil@apache.org>
 34  
  */
 35  
 public class InternationalStringImpl implements InternationalString
 36  
 {
 37  
     /**
 38  
      * Maintains an Hashmap of locale to string value
 39  
      */
 40  2823
     private final Map<MapKey,LocalizedString> map = new HashMap<MapKey,LocalizedString>();
 41  
 
 42  
     public InternationalStringImpl()
 43  2020
     {
 44  2020
     }
 45  
 
 46  
     public InternationalStringImpl(String str)
 47  326
     {
 48  326
         Locale locale = Locale.getDefault();
 49  326
         map.put(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME), new LocalizedStringImpl(locale, str, LocalizedString.DEFAULT_CHARSET_NAME));
 50  
 
 51  326
     }
 52  
 
 53  
     public InternationalStringImpl(Locale locale, String str, String charsetName)
 54  477
     {
 55  477
         MapKey mapKey = new MapKey(locale, charsetName);
 56  477
         map.put(mapKey, new LocalizedStringImpl(locale, str, charsetName));
 57  477
     }
 58  
 
 59  
     public void addLocalizedString(LocalizedString localizedString) throws JAXRException
 60  
     {
 61  36
         MapKey mapKey = new MapKey(localizedString);
 62  36
         map.put(mapKey, localizedString);
 63  36
     }
 64  
 
 65  
     public void addLocalizedStrings(Collection collection) throws JAXRException
 66  
     {
 67  2
         for (Iterator i = collection.iterator(); i.hasNext();)
 68  
         {
 69  4
             LocalizedString localizedString = (LocalizedString) i.next();
 70  4
             map.put(new MapKey(localizedString), localizedString);
 71  4
         }
 72  2
     }
 73  
 
 74  
     public Collection<LocalizedString> getLocalizedStrings() throws JAXRException
 75  
     {
 76  226
         return Collections.unmodifiableCollection(map.values());
 77  
     }
 78  
 
 79  
     public String getValue() throws JAXRException
 80  
     {
 81  148
         return getValue(Locale.getDefault());
 82  
     }
 83  
 
 84  
     public void setValue(String str) throws JAXRException
 85  
     {
 86  2
         setValue(Locale.getDefault(), str);
 87  2
     }
 88  
 
 89  
     public String getValue(Locale locale) throws JAXRException
 90  
     {
 91  206
         LocalizedString localizedString = (LocalizedString) map.get(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME));
 92  206
         return localizedString != null ? localizedString.getValue() : null;
 93  
     }
 94  
 
 95  
     public void setValue(Locale locale, String value) throws JAXRException
 96  
     {
 97  20
         map.put(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME), new LocalizedStringImpl(locale, value, LocalizedString.DEFAULT_CHARSET_NAME));
 98  20
     }
 99  
 
 100  
     public void removeLocalizedString(LocalizedString localizedString) throws JAXRException
 101  
     {
 102  6
         map.remove(new MapKey(localizedString));
 103  6
     }
 104  
 
 105  
     public void removeLocalizedStrings(Collection collection) throws JAXRException
 106  
     {
 107  2
         for (Iterator i = collection.iterator(); i.hasNext();)
 108  
         {
 109  4
             removeLocalizedString((LocalizedString) i.next());
 110  
         }
 111  2
     }
 112  
 
 113  
     public LocalizedString getLocalizedString(Locale locale, String charset) throws JAXRException
 114  
     {
 115  4
         return (LocalizedString) map.get(new MapKey(locale, charset));
 116  
     }
 117  
 
 118  
     private static class MapKey
 119  
     {
 120  
         private final Locale locale;
 121  
         private final String charsetName;
 122  
 
 123  
         public MapKey(Locale locale, String charsetName)
 124  1033
         {
 125  1033
             this.locale = locale;
 126  1033
             this.charsetName = charsetName;
 127  1033
         }
 128  
 
 129  
         public MapKey(LocalizedString localizedString) throws JAXRException
 130  46
         {
 131  46
             this.locale = localizedString.getLocale();
 132  46
             this.charsetName = localizedString.getCharsetName();
 133  46
         }
 134  
 
 135  
         public boolean equals(Object o)
 136  
         {
 137  210
             if (this == o) return true;
 138  210
             if (!(o instanceof MapKey)) return false;
 139  210
             final MapKey mapKey = (MapKey) o;
 140  210
             if (!charsetName.equals(mapKey.charsetName)) return false;
 141  210
             if (!locale.equals(mapKey.locale)) return false;
 142  210
             return true;
 143  
         }
 144  
 
 145  
         public int hashCode()
 146  
         {
 147  
             int result;
 148  1079
             result = locale.hashCode();
 149  1079
             result = 29 * result + charsetName.hashCode();
 150  1079
             return result;
 151  
         }
 152  
 
 153  
         public String toString()
 154  
         {
 155  0
             StringBuffer buf = new StringBuffer(32);
 156  0
             buf.append('[').append(locale).append(',').append(charsetName).append(']');
 157  0
             return buf.toString();
 158  
         }
 159  
     }
 160  
 }