This project has retired. For details please refer to its Attic page.
InternationalStringImpl xref
View Javadoc
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      private final Map<MapKey,LocalizedString> map = new HashMap<MapKey,LocalizedString>();
41  
42      public InternationalStringImpl()
43      {
44      }
45  
46      public InternationalStringImpl(String str)
47      {
48          Locale locale = Locale.getDefault();
49          map.put(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME), new LocalizedStringImpl(locale, str, LocalizedString.DEFAULT_CHARSET_NAME));
50  
51      }
52  
53      public InternationalStringImpl(Locale locale, String str, String charsetName)
54      {
55          MapKey mapKey = new MapKey(locale, charsetName);
56          map.put(mapKey, new LocalizedStringImpl(locale, str, charsetName));
57      }
58  
59      public void addLocalizedString(LocalizedString localizedString) throws JAXRException
60      {
61          MapKey mapKey = new MapKey(localizedString);
62          map.put(mapKey, localizedString);
63      }
64  
65      public void addLocalizedStrings(Collection collection) throws JAXRException
66      {
67          for (Iterator i = collection.iterator(); i.hasNext();)
68          {
69              LocalizedString localizedString = (LocalizedString) i.next();
70              map.put(new MapKey(localizedString), localizedString);
71          }
72      }
73  
74      public Collection<LocalizedString> getLocalizedStrings() throws JAXRException
75      {
76          return Collections.unmodifiableCollection(map.values());
77      }
78  
79      public String getValue() throws JAXRException
80      {
81          return getValue(Locale.getDefault());
82      }
83  
84      public void setValue(String str) throws JAXRException
85      {
86          setValue(Locale.getDefault(), str);
87      }
88  
89      public String getValue(Locale locale) throws JAXRException
90      {
91          LocalizedString localizedString = (LocalizedString) map.get(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME));
92          return localizedString != null ? localizedString.getValue() : null;
93      }
94  
95      public void setValue(Locale locale, String value) throws JAXRException
96      {
97          map.put(new MapKey(locale, LocalizedString.DEFAULT_CHARSET_NAME), new LocalizedStringImpl(locale, value, LocalizedString.DEFAULT_CHARSET_NAME));
98      }
99  
100     public void removeLocalizedString(LocalizedString localizedString) throws JAXRException
101     {
102         map.remove(new MapKey(localizedString));
103     }
104 
105     public void removeLocalizedStrings(Collection collection) throws JAXRException
106     {
107         for (Iterator i = collection.iterator(); i.hasNext();)
108         {
109             removeLocalizedString((LocalizedString) i.next());
110         }
111     }
112 
113     public LocalizedString getLocalizedString(Locale locale, String charset) throws JAXRException
114     {
115         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         {
125             this.locale = locale;
126             this.charsetName = charsetName;
127         }
128 
129         public MapKey(LocalizedString localizedString) throws JAXRException
130         {
131             this.locale = localizedString.getLocale();
132             this.charsetName = localizedString.getCharsetName();
133         }
134 
135         public boolean equals(Object o)
136         {
137             if (this == o) return true;
138             if (!(o instanceof MapKey)) return false;
139             final MapKey mapKey = (MapKey) o;
140             if (!charsetName.equals(mapKey.charsetName)) return false;
141             if (!locale.equals(mapKey.locale)) return false;
142             return true;
143         }
144 
145         public int hashCode()
146         {
147             int result;
148             result = locale.hashCode();
149             result = 29 * result + charsetName.hashCode();
150             return result;
151         }
152 
153         public String toString()
154         {
155             StringBuffer buf = new StringBuffer(32);
156             buf.append('[').append(locale).append(',').append(charsetName).append(']');
157             return buf.toString();
158         }
159     }
160 }