001 /*
002 * jDTAUS Core API
003 * Copyright (c) 2005 Christian Schulte
004 *
005 * Christian Schulte, Haldener Strasse 72, 58095 Hagen, Germany
006 * <schulte2005@users.sourceforge.net> (+49 2331 3543887)
007 *
008 * This library is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU Lesser General Public
010 * License as published by the Free Software Foundation; either
011 * version 2.1 of the License, or any later version.
012 *
013 * This library is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with this library; if not, write to the Free Software
020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021 *
022 */
023 package org.jdtaus.core.container;
024
025 import java.io.Serializable;
026 import java.util.Iterator;
027 import java.util.Locale;
028 import java.util.Map;
029 import java.util.TreeMap;
030
031 /**
032 * Localized text.
033 *
034 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
035 * @version $Id: Text.java 8044 2009-07-02 01:29:05Z schulte2005 $
036 */
037 public class Text implements Cloneable, Serializable
038 {
039 //--Constants---------------------------------------------------------------
040
041 /** Serial version UID for backwards compatibility with 1.5.x classes. */
042 private static final long serialVersionUID = 6677913442223787679L;
043
044 //---------------------------------------------------------------Constants--
045 //--Text--------------------------------------------------------------------
046
047 /**
048 * The value of the text.
049 * @serial
050 */
051 private String value;
052
053 /**
054 * Maps locales to values.
055 * @serial
056 */
057 private final Map values = new TreeMap();
058
059 /**
060 * Gets the value of the text for the default language.
061 *
062 * @return the value of the text for the default language or {@code null}.
063 */
064 public String getValue()
065 {
066 return this.value;
067 }
068
069 /**
070 * Setter for property {@code value}.
071 *
072 * @param value the new value of the text for the default language.
073 */
074 public void setValue( final String value )
075 {
076 this.value = value;
077 }
078
079 /**
080 * Gets the value of the text for a given locale.
081 *
082 * @param locale the locale of the value to return.
083 *
084 * @return the value of the text for {@code locale} or {@code null}.
085 *
086 * @throws NullPointerException if {@code locale} is {@code null}.
087 */
088 public String getValue( final Locale locale )
089 {
090 if ( locale == null )
091 {
092 throw new NullPointerException( "locale" );
093 }
094
095 String v =
096 (String) this.values.get( locale.getLanguage().toLowerCase() );
097
098 if ( v == null )
099 {
100 v = this.getValue();
101 }
102
103 return v;
104 }
105
106 /**
107 * Setter for property {@code value} for a given locale.
108 *
109 * @param locale the locale to store {@code value} with.
110 * @param value the new value of the text for {@code locale}.
111 *
112 * @throws NullPointerException if {@code locale} is {@code null}.
113 */
114 public void setValue( final Locale locale, final String value )
115 {
116 if ( locale == null )
117 {
118 throw new NullPointerException( "locale" );
119 }
120
121 this.values.put( locale.getLanguage().toLowerCase(), value );
122 }
123
124 /**
125 * Gets all locales for which the instance holds values.
126 *
127 * @return all locales for which the instance holds values.
128 */
129 public Locale[] getLocales()
130 {
131 final Locale[] locales = new Locale[ this.values.size() ];
132 int i = locales.length - 1;
133
134 for ( Iterator it = this.values.keySet().iterator(); it.hasNext();)
135 {
136 locales[i--] = new Locale( (String) it.next() );
137 }
138
139 return locales;
140 }
141
142 /**
143 * Creates a string representing the properties of the instance.
144 *
145 * @return a string representing the properties of the instance.
146 */
147 private String internalString()
148 {
149 final StringBuffer buf = new StringBuffer( 500 ).append( '{' );
150 final Locale[] locales = this.getLocales();
151 for ( int i = locales.length - 1; i >= 0; i-- )
152 {
153 buf.append( "[" ).append( locales[i] ).append( "]=" ).
154 append( this.getValue( locales[i] ) );
155
156 if ( i - 1 >= 0 )
157 {
158 buf.append( ", " );
159 }
160 }
161
162 buf.append( '}' );
163 return buf.toString();
164 }
165
166 //--------------------------------------------------------------------Text--
167 //--Object------------------------------------------------------------------
168
169 /**
170 * Returns a string representation of the object.
171 *
172 * @return a string representation of the object.
173 */
174 public String toString()
175 {
176 return super.toString() + this.internalString();
177 }
178
179 /**
180 * Creates and returns a copy of this object. This method performs a
181 * "shallow copy" of this object, not a "deep copy" operation.
182 *
183 * @return a clone of this instance.
184 */
185 public Object clone()
186 {
187 try
188 {
189 return super.clone();
190 }
191 catch ( CloneNotSupportedException e )
192 {
193 throw new AssertionError( e );
194 }
195 }
196
197 //------------------------------------------------------------------Object--
198 }