
com.contentful.java.cda.LocalizedResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
Java SDK for Contentful's Content Delivery API.
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 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) {
Map, ?> value = (Map ,?>) fields.get(key);
if (value == null) {
return null;
}
Object localized = value.get(locale);
if (localized != null) {
return (T) localized;
}
return (T) value.get(defaultLocale);
}
/** Raw unprocessed fields. */
public Map rawFields() {
return rawFields;
}
/** Returns the active locale code for this resource. */
public String locale() {
return locale;
}
/** Switches the locale to the one matching the given locale code. */
public void setLocale(String locale) {
this.locale = locale;
}
void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
}
String defaultLocale() {
return defaultLocale;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy