org.gatein.api.common.i18n.Localized Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.gatein.api.common.i18n;
import org.gatein.api.internal.ObjectToStringBuilder;
import org.gatein.api.internal.Parameters;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Base class for localizing values of type T.
*
* @author Nick Scavelli
*/
public abstract class Localized implements Iterable, Serializable {
private final Map> values;
protected Localized(Localized localized) {
this.values = new HashMap>(localized.values);
}
protected Localized(Locale locale, T value) {
this(Collections.singletonMap(locale, value));
}
protected Localized(Map valueMap) {
Parameters.requireNonNull(valueMap, "valueMap");
Map> map = new HashMap>(valueMap.size());
for (Map.Entry entry : valueMap.entrySet()) {
Locale locale = entry.getKey();
T value = entry.getValue();
if (value == null)
throw new IllegalArgumentException("valueMap contains a null value for key locale " + locale);
map.put(locale, new Value(locale, value));
}
values = map;
}
/**
* Gets the value mapped to the specific locale. Can return null if no mapping exists.
*
* @param locale the locale of the value
* @return the value object representing the mapping between locale and value.
*/
public Value getLocalizedValue(Locale locale) {
return values.get(locale);
}
/**
* Gets the T value mapped to the specific locale. Can return null if no mapping exists. Convenience method for
* getLocalizedValue(Locale).getValue()
*
* @param locale the locale of the value
* @return the value of type T
*/
public T getValue(Locale locale) {
Value value = getLocalizedValue(locale);
return (value == null) ? null : value.getValue();
}
/**
* Sets the value for the specified locale. If a locale already exists, will override the previous value mapped to that
* locale.
*
* @param locale the locale
* @param value the value
* @return this
*/
public Localized setLocalizedValue(Locale locale, T value) {
values.put(locale, new Value(locale, value));
return this;
}
/**
* Will remove the value mapped to the locale.
*
* @param locale the locale
*/
public void removeLocalizedValue(Locale locale) {
values.remove(locale);
}
/**
* Returns a collection of values representing the mapping of Locale to type T
*
* @return the collection of values
*/
public Collection> getLocalizedValues() {
return Collections.unmodifiableCollection(values.values());
}
@Override
public Iterator iterator() {
List list = new ArrayList(values.size());
for (Value value : values.values()) {
list.add(value.getValue());
}
return list.iterator();
}
@Override
public String toString() {
return ObjectToStringBuilder.toStringBuilder(getClass()).add("values", values.values()).toString();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Localized))
return false;
Localized> localized = (Localized>) o;
return values.equals(localized.values);
}
@Override
public int hashCode() {
return values.hashCode();
}
/**
* Value object representing the mapping of a Locale to a value of type T.
*
* @param the value type
*/
public static final class Value implements Serializable {
private final Locale locale;
private final T value;
private Value(Locale locale, T value) {
this.locale = locale;
this.value = value;
}
/**
* Returns the value object of type T.
*
* @return the value of type T
*/
public T getValue() {
return value;
}
/**
* Returns the locale associated with this value.
*
* @return the locale
*/
public Locale getLocale() {
return locale;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Value> v = (Value>) o;
return (locale == null) ? v.locale == null : locale.equals(v.locale) && (value == null) ? v.value == null : value
.equals(v.value);
}
@Override
public int hashCode() {
int result = locale != null ? locale.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
@Override
public String toString() {
return ObjectToStringBuilder.toStringBuilder(getClass()).add("locale", locale).add("value", value).toString();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy