001/*
002 *  jDTAUS Core API
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container;
022
023import java.io.Serializable;
024import java.util.Iterator;
025import java.util.Locale;
026import java.util.Map;
027import java.util.TreeMap;
028
029/**
030 * Localized text.
031 *
032 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
033 * @version $JDTAUS: Text.java 8743 2012-10-07 03:06:20Z schulte $
034 */
035public class Text implements Cloneable, Serializable
036{
037    //--Constants---------------------------------------------------------------
038
039    /** Serial version UID for backwards compatibility with 1.5.x classes. */
040    private static final long serialVersionUID = 6677913442223787679L;
041
042    //---------------------------------------------------------------Constants--
043    //--Text--------------------------------------------------------------------
044
045    /**
046     * The value of the text.
047     * @serial
048     */
049    private String value;
050
051    /**
052     * Maps locales to values.
053     * @serial
054     */
055    private final Map values = new TreeMap();
056
057    /** Creates a new {@code Text} instance. */
058    public Text()
059    {
060        super();
061    }
062
063    /**
064     * Gets the value of the text for the default language.
065     *
066     * @return the value of the text for the default language or {@code null}.
067     */
068    public String getValue()
069    {
070        return this.value;
071    }
072
073    /**
074     * Setter for property {@code value}.
075     *
076     * @param value the new value of the text for the default language.
077     */
078    public void setValue( final String value )
079    {
080        this.value = value;
081    }
082
083    /**
084     * Gets the value of the text for a given locale.
085     *
086     * @param locale the locale of the value to return.
087     *
088     * @return the value of the text for {@code locale} or {@code null}.
089     *
090     * @throws NullPointerException if {@code locale} is {@code null}.
091     */
092    public String getValue( final Locale locale )
093    {
094        if ( locale == null )
095        {
096            throw new NullPointerException( "locale" );
097        }
098
099        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}