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

io.sphere.client.shop.model.VariantList Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.client.shop.model;

import com.google.common.base.Optional;
import io.sphere.internal.util.ListUtil;

import javax.annotation.Nonnull;
import java.util.*;

/** List of variants of a {@link Product} that supports filtering by various criteria. */
public class VariantList implements Iterable {
    private final List variants;

    public VariantList(@Nonnull List variants) {
        if (variants == null) throw new NullPointerException("variants");
        this.variants = variants;
    }

    @Override public Iterator iterator() {
        return variants.iterator();
    }

    /** Returns the number of items in this list. */
    public int size() {
        return variants.size();
    }

    /** Returns {@code true} if this list contains no items. */
    public boolean isEmpty() {
        return variants.isEmpty();
    }

    /** Returns the first element in this list, or {@link Optional#absent()} if the list is empty. */
    public Optional first() {
        return (variants.size() == 0) ? Optional.absent() : Optional.of(variants.get(0));
    }

    /** Returns the items as a {@link List}. */
    public List asList() {
        return new ArrayList(variants);
    }

    /** Gets distinct values of given attribute across all variants of this product. */
    @Nonnull public List getAvailableAttributes(String attributeName) {
        List attributes = new ArrayList();
        Set seen = new HashSet();
        for(Variant v: variants) {
            for (Attribute a: v.getAttributes()) {
                if (a.getName().equals(attributeName) && !seen.contains(a.getValue())) {
                    attributes.add(a);
                    seen.add(a.getValue());
                }
            }
        }
        return attributes;
    }

    /** Returns the variant with given id, or {@link Optional#absent()} if no such variant exists. */
    @Nonnull public Optional byId(int id) {
        for (Variant v: variants) {
            if (v.getId() == id) return Optional.of(v);
        }
        return Optional.absent();
    }

    /** Returns the first variant with given SKU, or {@link Optional#absent()} if no such variant exists. */
    @Nonnull public Optional bySKU(String sku) {
        for (Variant v: variants) {
            if (v.getSKU().equals(sku)) return Optional.of(v);
        }
        return Optional.absent();
    }

    /** Finds variants that have all given attribute values.
     *
     * 

Example: * *

Implement a variant switcher for colors of current product: *

* for (Attribute color: product.getVariants().getAvailableAttributes("color")) { * Variant variant = p.getVariants().byAttributes(color).first().get(); // use get() only if you are sure the variant exists * } * * *

Implement a variant switcher that changes color, maintaining selected size: *

* VariantList greenVariants = p.getVariants().byAttributes(new Attribute("color", "green")); * VariantList greenInCurrentSize = greenVariants.byAttributes(currentVariant.getAttribute("size")); * * * @param desiredAttribute Attribute that the returned variants must have. * @param desiredAttributes Additional attributes that the returned variants must have. * @return Variants that have all given attribute values. */ @Nonnull public VariantList byAttributes(Attribute desiredAttribute, Attribute... desiredAttributes) { return byAttributes(ListUtil.list(desiredAttribute, desiredAttributes)); } /** Finds variants that have all given attribute values. * *

Example: * *

Implement a variant switcher for colors of current product: *

* for (Attribute color: product.getVariants().getAvailableAttributes("color")) { * Variant variant = p.getVariants().byAttributes(color).first().get(); // use get() only if you are sure the variant exists * } * * *

Implement a variant switcher that changes color, maintaining selected size: *

* VariantList greenVariants = p.getVariants().byAttributes(new Attribute("color", "green")); * VariantList greenInCurrentSize = greenVariants.byAttributes(currentVariant.getAttribute("size")); * * * @param desiredAttributes Attributes that the returned variants must have. * @return Variants that have all given attribute values. */ @Nonnull public VariantList byAttributes(@Nonnull Iterable desiredAttributes) { if (desiredAttributes == null) throw new NullPointerException("desiredAttributes"); Map desiredAttributesMap = toMap(desiredAttributes); ArrayList filtered = new ArrayList(); for (Variant v: variants) { int matchCount = 0; for (Attribute a: v.getAttributes()) { Attribute desiredAttribute = desiredAttributesMap.get(a.getName()); if (desiredAttribute != null && (desiredAttribute.getValue().equals(a.getValue()))) { matchCount++; } } if (matchCount == desiredAttributesMap.size()) { // has all desiredAttributes filtered.add(v); } } return new VariantList(filtered); } // ------------------------------------ // Helpers // ------------------------------------ /** Copies the attributes of given variant and overrides given attributes (based on name). */ private Map toMap(Iterable attributes) { Map map = new HashMap(); for (Attribute a: attributes) { if (a != null) { map.put(a.getName(), a); } } return map; } @Override public String toString() { return "VariantList{" + "variants=" + variants + '}'; } }