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

io.sphere.sdk.types.CustomFields Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.types;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.sphere.sdk.json.JsonException;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.json.TypeReferences;
import io.sphere.sdk.models.*;

import javax.annotation.Nullable;
import javax.money.MonetaryAmount;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Optional;

public class CustomFields extends Base {
    private final Reference type;
    private final Map fields;

    private CustomFields(final Reference type, final Map fields) {
        this.type = type;
        this.fields = fields;
    }

    @Nullable
    public JsonNode getFieldAsJsonNode(final String name) {
        return fields.get(name);
    }

    @Nullable
    public  T getField(final String name, final TypeReference typeReference) {
        try {
            return Optional.ofNullable(getFieldAsJsonNode(name))
                    .map(jsonNode -> SphereJsonUtils.readObject(jsonNode, typeReference))
                    .orElse(null);
        } catch (final RuntimeException e) {
            throw new JsonException(e);
        }
    }

    @Nullable
    public String getFieldAsString(final String name) {
        return getField(name, TypeReferences.stringTypeReference());
    }

    @Nullable
    public Boolean getFieldAsBoolean(final String name) {
        return getField(name, TypeReferences.booleanTypeReference());
    }

    @Nullable
    public LocalizedString getFieldAsLocalizedString(final String name) {
        return getField(name, LocalizedString.typeReference());
    }

    @Nullable
    public String getFieldAsEnumKey(final String name) {
        return getFieldAsString(name);
    }

    @Nullable
    public String getFieldAsLocalizedEnumKey(final String name) {
        return getFieldAsString(name);
    }

    @Nullable
    public Integer getFieldAsInteger(final String name) {
        return getField(name, TypeReferences.integerTypeReference());
    }

    @Nullable
    public Long getFieldAsLong(final String name) {
        return getField(name, TypeReferences.longTypeReference());
    }

    @Nullable
    public Double getFieldAsDouble(final String name) {
        return getField(name, TypeReferences.doubleTypeReference());
    }

    @Nullable
    public MonetaryAmount getFieldAsMoney(final String name) {
        return getField(name, TypeReferences.monetaryAmountTypeReference());
    }

    @Nullable
    public LocalDate getFieldAsDate(final String name) {
        return getField(name, TypeReferences.localDateTypeReference());
    }

    @Nullable
    public ZonedDateTime getFieldAsDateTime(final String name) {
        return getField(name, TypeReferences.zonedDateTimeTypeReference());
    }

    @Nullable
    public LocalTime getFieldAsTime(final String name) {
        return getField(name, TypeReferences.localTimeTypeReference());
    }

    public Reference getType() {
        return type;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy