com.pelzer.util.l10n
Class Localizable<E>

java.lang.Object
  extended by com.pelzer.util.l10n.Localizable<E>
All Implemented Interfaces:
Serializable, Map<Locale,E>

public class Localizable<E>
extends Object
implements Map<Locale,E>, Serializable

This class wraps multiple versions of an object (E) based on a localizer, and then has some defaulting logic for grabbing a single entry back out.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
Localizable()
           
Localizable(E defaultValue, Locale locale)
          Instantiates, and then calls #setDefault(E, Locale)
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<Locale,E>> entrySet()
           
 E exactGet(Locale locale)
           
 E get(Locale... preferredLocales)
           
 E get(Object key)
           
 Locale[] getAllLocales()
           
 E getDefault()
           
 Locale getDefaultLocale()
           
 Map<Locale,E> getWrappedMap()
          Since this class is actually just wrapping an underlying hashmap, for hibernate support we need to have direct access to this value...
 boolean hasExactMatch(Locale locale)
           
 boolean isEmpty()
          Localizable can never be empty.
 Set<Locale> keySet()
           
 E put(Locale locale, E value)
           
 void putAll(Map<? extends Locale,? extends E> t)
           
 E remove(Object key)
           
 E set(E value, Locale locale)
          Adds or overrides a value to the internal hash.
 void setDefault(E value, Locale locale)
          Overrides the default locale value.
 void setWrappedMap(Map<Locale,E> values)
           
 int size()
           
 Collection<E> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

Localizable

public Localizable()

Localizable

public Localizable(E defaultValue,
                   Locale locale)
Instantiates, and then calls #setDefault(E, Locale)

Parameters:
defaultValue - can be null, and subsequent calls to getDefault will return null
Throws:
NullPointerException - if locale is null.
Method Detail

getAllLocales

public Locale[] getAllLocales()
Returns:
an array of the locales that this object has been localized for, including the default one as the last element of the array. They can then be used to call exactGet(Locale) to return absolute values

set

public E set(E value,
             Locale locale)
Adds or overrides a value to the internal hash.

Parameters:
value - If set to null, removes the value from the internal hash (the local will no longer show up in getAllLocales()
Returns:
The value previously associated with the locale, or null if none.
Throws:
NullPointerException - if locale is null.

setDefault

public void setDefault(E value,
                       Locale locale)
Overrides the default locale value.

Parameters:
value - can be null, and subsequent calls to getDefault will return null
Throws:
NullPointerException - if locale is null.

getDefault

public E getDefault()
Returns:
the String set during construction or later by a call to #setDefault(String, Localizer)

exactGet

public E exactGet(Locale locale)
Returns:
the value stored for the given wrapper, or null if no exact match found.

hasExactMatch

public boolean hasExactMatch(Locale locale)
Returns:
true if there is an EXACT match for the given wrapper, false if the system would need to use defaulting logic to satisfy the request.

get

public E get(Locale... preferredLocales)
Returns:
the best String that can be returned based on the passed-in preferred locales. If one of the locales matches a stored key, either using absolute equality or fuzzy matching (out of order/wildcard), that entry will be returned. If no exact or fuzzy matches occur, and one of the preferred locales was Locale.NONE, then getDefault() will be returned. A null is possible if no matches are found, and Locale.NONE is not one of the preferred locales.

getDefaultLocale

public Locale getDefaultLocale()
Returns:
the default localizer set during construction or #setDefault(String, Localizer)

getWrappedMap

public Map<Locale,E> getWrappedMap()
Since this class is actually just wrapping an underlying hashmap, for hibernate support we need to have direct access to this value... The only time this should be used is during (de)serialization.

See Also:
setWrappedMap(Map)

setWrappedMap

public void setWrappedMap(Map<Locale,E> values)
See Also:
getWrappedMap()

clear

public void clear()
Specified by:
clear in interface Map<Locale,E>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<Locale,E>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<Locale,E>

entrySet

public Set<Map.Entry<Locale,E>> entrySet()
Specified by:
entrySet in interface Map<Locale,E>

get

public E get(Object key)
Specified by:
get in interface Map<Locale,E>

isEmpty

public boolean isEmpty()
Localizable can never be empty.

Specified by:
isEmpty in interface Map<Locale,E>

keySet

public Set<Locale> keySet()
Specified by:
keySet in interface Map<Locale,E>

put

public E put(Locale locale,
             E value)
Specified by:
put in interface Map<Locale,E>

putAll

public void putAll(Map<? extends Locale,? extends E> t)
Specified by:
putAll in interface Map<Locale,E>

remove

public E remove(Object key)
Specified by:
remove in interface Map<Locale,E>

size

public int size()
Specified by:
size in interface Map<Locale,E>

values

public Collection<E> values()
Specified by:
values in interface Map<Locale,E>


Copyright © 2012. All Rights Reserved.