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

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

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

import io.sphere.sdk.categories.Category;
import io.sphere.sdk.models.LocalizedStrings;
import io.sphere.sdk.models.MetaAttributes;
import io.sphere.sdk.models.Reference;
import io.sphere.sdk.models.WithLocalizedSlug;

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

interface ProductDataLike extends WithLocalizedSlug, MetaAttributes {
    public LocalizedStrings getName();

    public List> getCategories();

    public Optional getDescription();

    public LocalizedStrings getSlug();

    public Optional getMetaTitle();

    public Optional getMetaDescription();

    public Optional getMetaKeywords();

    /**
     * Returns the master variant. Every product as 1 to n variants, so this is always present.
     *
     * @see #getAllVariants()
     * @see #getVariants()
     * @return the main variant in the product
     */
    public ProductVariant getMasterVariant();

    /**
     * Gets the variants which are not identical to the master variant. This list can be empty.
     *
     * @see #getAllVariants()
     * @see #getMasterVariant()
     *
     *
     * @return all variants except the one in {@link #getMasterVariant()}
     */
    public List getVariants();

    /**
     * Gets all variants in the product including the master variant as first element in the list.
     *
     * @see #getMasterVariant()
     * @see #getVariants()
     * @return all variants
     */
    default List getAllVariants() {
        return ProductsPackage.getAllVariants(this);
    }

    /**
     * Finds a product variant by id.
     *
     * @param variantId the id of the variant to find
     * @see #getVariantOrMaster(int)
     */
    default Optional getVariant(final int variantId) {
        final Optional result;
        if (variantId == getMasterVariant().getId()) {
            result = Optional.of(getMasterVariant());
        } else {
            result = getVariants().stream().filter(v -> v.getId() == variantId).findFirst();
        }
        return result;
    }

    /**
     * Finds a product variant by id and returns the master variant if not variant with the id is present.
     * @param variantId the id of the variant to find
     * @return a variant
     * @see #getVariant(int)
     */
    default ProductVariant getVariantOrMaster(final int variantId) {
        return ProductsPackage.getVariantOrMaster(variantId, this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy