All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contentful.java.cda.LocalizedResource Maven / Gradle / Ivy

There is a newer version: 9.1.0
Show newest version
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