io.sphere.sdk.products.ProductsPackage Maven / Gradle / Ivy
package io.sphere.sdk.products;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
*
* This class is helper since Java does not multiple inheritance.
*/
final class ProductsPackage {
private ProductsPackage() {
}
static Optional getVariant(final Integer variantId, final ProductDataLike product) {
final Optional result;
final ProductVariant masterVariant = product.getMasterVariant();
if (variantId == masterVariant.getId()) {
result = Optional.of(masterVariant);
} else {
result = product.getVariants().stream().filter(v -> v.getId() == variantId).findFirst();
}
return result;
}
static ProductVariant getVariantOrMaster(final Integer variantId, final ProductDataLike product) {
return Optional.ofNullable(product.getVariant(variantId)).orElseGet(() -> product.getMasterVariant());
}
static List getAllVariants(final ProductDataLike product) {
final List nonMasterVariants = product.getVariants();
final ArrayList result = new ArrayList<>(1 + nonMasterVariants.size());
result.add(product.getMasterVariant());
result.addAll(nonMasterVariants);
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy