com.contentful.java.cda.LocalizedResource Maven / Gradle / Ivy
package com.contentful.java.cda;
import java.util.Map;
/**
* Represents a resource which may contain field values for multiple locales.
*/
public abstract class LocalizedResource extends CDAResource {
String locale;
String defaultLocale;
Map fallbackLocaleMap;
Map fields;
Map rawFields;
/**
* Extracts a field from the fields set of the active locale, result type is inferred.
*
* @param key field key.
* @param type.
* @return field value, null if it doesn't exist.
*/
@SuppressWarnings("unchecked")
public T getField(String key) {
final Map value = (Map) fields.get(key);
if (value == null) {
return null;
}
return getFieldForFallbackLocale(value, locale);
}
private T getFieldForFallbackLocale(Map value, String locale) {
if (locale == null) {
return null;
}
final T localized = value.get(locale);
if (localized != null) {
return localized;
} else {
return getFieldForFallbackLocale(value, fallbackLocaleMap.get(locale));
}
}
/**
* @return raw unprocessed fields.
*/
public Map rawFields() {
return rawFields;
}
/**
* @return returns the active locale code for this resource.
*/
public String locale() {
return locale;
}
/**
* Switches the locale to the one matching the given locale code.
*
* @param locale the locale to be set.
*/
public void setLocale(String locale) {
this.locale = locale;
}
void setFallbackLocaleMap(Map fallbackLocaleMap) {
this.fallbackLocaleMap = fallbackLocaleMap;
}
Map fallbackLocaleMap() {
return fallbackLocaleMap;
}
void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
}
String defaultLocale() {
return defaultLocale;
}
}