1 /*
2 * jDTAUS Core API
3 * Copyright (C) 2005 Christian Schulte
4 * <cs@schulte.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21 package org.jdtaus.core.container;
22
23 import java.io.Serializable;
24 import java.util.Iterator;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 /**
30 * Localized text.
31 *
32 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
33 * @version $JDTAUS: Text.java 8743 2012-10-07 03:06:20Z schulte $
34 */
35 public class Text implements Cloneable, Serializable
36 {
37 //--Constants---------------------------------------------------------------
38
39 /** Serial version UID for backwards compatibility with 1.5.x classes. */
40 private static final long serialVersionUID = 6677913442223787679L;
41
42 //---------------------------------------------------------------Constants--
43 //--Text--------------------------------------------------------------------
44
45 /**
46 * The value of the text.
47 * @serial
48 */
49 private String value;
50
51 /**
52 * Maps locales to values.
53 * @serial
54 */
55 private final Map values = new TreeMap();
56
57 /** Creates a new {@code Text} instance. */
58 public Text()
59 {
60 super();
61 }
62
63 /**
64 * Gets the value of the text for the default language.
65 *
66 * @return the value of the text for the default language or {@code null}.
67 */
68 public String getValue()
69 {
70 return this.value;
71 }
72
73 /**
74 * Setter for property {@code value}.
75 *
76 * @param value the new value of the text for the default language.
77 */
78 public void setValue( final String value )
79 {
80 this.value = value;
81 }
82
83 /**
84 * Gets the value of the text for a given locale.
85 *
86 * @param locale the locale of the value to return.
87 *
88 * @return the value of the text for {@code locale} or {@code null}.
89 *
90 * @throws NullPointerException if {@code locale} is {@code null}.
91 */
92 public String getValue( final Locale locale )
93 {
94 if ( locale == null )
95 {
96 throw new NullPointerException( "locale" );
97 }
98
99 String v =
100 (String) this.values.get( locale.getLanguage().toLowerCase() );
101
102 if ( v == null )
103 {
104 v = this.getValue();
105 }
106
107 return v;
108 }
109
110 /**
111 * Setter for property {@code value} for a given locale.
112 *
113 * @param locale the locale to store {@code value} with.
114 * @param value the new value of the text for {@code locale}.
115 *
116 * @throws NullPointerException if {@code locale} is {@code null}.
117 */
118 public void setValue( final Locale locale, final String value )
119 {
120 if ( locale == null )
121 {
122 throw new NullPointerException( "locale" );
123 }
124
125 this.values.put( locale.getLanguage().toLowerCase(), value );
126 }
127
128 /**
129 * Gets all locales for which the instance holds values.
130 *
131 * @return all locales for which the instance holds values.
132 */
133 public Locale[] getLocales()
134 {
135 final Locale[] locales = new Locale[ this.values.size() ];
136 int i = locales.length - 1;
137
138 for ( final Iterator it = this.values.keySet().iterator();
139 it.hasNext(); )
140 {
141 locales[i--] = new Locale( (String) it.next() );
142 }
143
144 return locales;
145 }
146
147 /**
148 * Creates a string representing the properties of the instance.
149 *
150 * @return a string representing the properties of the instance.
151 */
152 private String internalString()
153 {
154 final StringBuffer buf = new StringBuffer( 500 ).append( '{' );
155 final Locale[] locales = this.getLocales();
156 for ( int i = locales.length - 1; i >= 0; i-- )
157 {
158 buf.append( "[" ).append( locales[i] ).append( "]=" ).
159 append( this.getValue( locales[i] ) );
160
161 if ( i - 1 >= 0 )
162 {
163 buf.append( ", " );
164 }
165 }
166
167 buf.append( '}' );
168 return buf.toString();
169 }
170
171 //--------------------------------------------------------------------Text--
172 //--Object------------------------------------------------------------------
173
174 /**
175 * Returns a string representation of the object.
176 *
177 * @return a string representation of the object.
178 */
179 public String toString()
180 {
181 return super.toString() + this.internalString();
182 }
183
184 /**
185 * Creates and returns a copy of this object. This method performs a
186 * "shallow copy" of this object, not a "deep copy" operation.
187 *
188 * @return a clone of this instance.
189 */
190 public Object clone()
191 {
192 try
193 {
194 return super.clone();
195 }
196 catch ( final CloneNotSupportedException e )
197 {
198 throw new AssertionError( e );
199 }
200 }
201
202 //------------------------------------------------------------------Object--
203 }