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

io.sphere.sdk.products.AttributeContainer Maven / Gradle / Ivy

The newest version!
package io.sphere.sdk.products;

import io.sphere.sdk.attributes.Attribute;
import io.sphere.sdk.attributes.AttributeGetter;

import java.util.List;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * A container for attributes. This can be either a product variant or nested attribute.
 */
public interface AttributeContainer {
    List getAttributes();

    /**
     * Access one attribute of a specific name and type which is known in the first place, consult {@link io.sphere.sdk.attributes.AttributeGetterSetter} how to implement these.
     *
     * @throws io.sphere.sdk.json.JsonException if the type of attribute cannot be parsed
     *
     * @param accessor declaration of the name and type of the attribute
     * @param  the underlying type of the attribute
     * @return the value of the attribute, or Optional.empty if absent
     */
     Optional getAttribute(final AttributeGetter accessor);

    default boolean hasAttribute(String attributeName) {
        return getAttributes().stream().anyMatch(attr -> attr.getName().equals(attributeName));
    }

    default boolean hasAttribute(AttributeGetter getter) {
        return getAttributes().stream().anyMatch(attr -> attr.getName().equals(getter.getName()));
    }

    default Optional getAttribute(final String attributeName) {
        requireNonNull(attributeName);
        return getAttributes().stream().filter(attr -> attr.getName().equals(attributeName)).findAny();
    }

    static AttributeContainer of(List attributes) {
        return AttributeContainerImpl.of(attributes);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy