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

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

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

import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.channels.Channel;
import io.sphere.sdk.customergroups.CustomerGroup;
import io.sphere.sdk.models.Builder;
import io.sphere.sdk.models.Reference;
import io.sphere.sdk.models.Referenceable;
import io.sphere.sdk.productdiscounts.DiscountedPrice;

import javax.money.MonetaryAmount;
import java.util.Objects;
import java.util.Optional;

public class PriceBuilder implements Builder {
    private final MonetaryAmount value;
    private Optional country = Optional.empty();
    private Optional> customerGroup = Optional.empty();
    private Optional> channel = Optional.empty();
    private Optional discounted = Optional.empty();

    private PriceBuilder(final MonetaryAmount value) {
        this.value = value;
    }

    public static PriceBuilder of(final MonetaryAmount value) {
        return new PriceBuilder(value);
    }

    public static PriceBuilder of(final Price template) {
        return of(template.getValue())
                .country(template.getCountry())
                .customerGroup(template.getCustomerGroup())
                .channel(template.getChannel())
                .discounted(template.getDiscounted());
    }

    public PriceBuilder country(final Optional country) {
        this.country = country;
        return this;
    }
    
    public PriceBuilder country(final CountryCode country) {
        Objects.requireNonNull(country);
        return country(Optional.of(country));
    }
    
    public PriceBuilder customerGroup(final Optional> customerGroup) {
        this.customerGroup = customerGroup;
        return this;
    }
    
    public PriceBuilder customerGroup(final Referenceable customerGroup) {
        Objects.requireNonNull(customerGroup);
        return customerGroup(Optional.of(customerGroup.toReference()));
    } 
    
    public PriceBuilder channel(final Optional> channel) {
        this.channel = channel;
        return this;
    }
    
    public PriceBuilder channel(final Referenceable channel) {
        Objects.requireNonNull(channel);
        return channel(Optional.of(channel.toReference()));
    }

    public PriceBuilder discounted(final Optional discounted) {
        this.discounted = discounted;
        return this;
    }

    public PriceBuilder discounted(final DiscountedPrice discounted) {
        Objects.requireNonNull(discounted);
        return discounted(Optional.of(discounted));
    }

    @Override
    public Price build() {
        return new Price(value, country, customerGroup, channel, discounted);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy