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

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

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

import javax.annotation.Nonnull;

import com.google.common.base.Optional;
import io.sphere.client.model.EmptyReference;
import io.sphere.client.model.Money;
import io.sphere.client.model.Reference;

import com.google.common.base.Predicate;
import com.neovisionaries.i18n.CountryCode;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

/** Price of a product variant.
 *
 * A price has a value and can be optionally restricted to a specific country and customer group.
 *
 * 

Examples *

    *
  • (10EUR, DE, gold) - applies to gold customers in Germany *
  • (15EUR, DE) - applies to customers in Germany *
  • (20EUR) - applies to all customers *
* * @see Variant#getPrice(String, com.neovisionaries.i18n.CountryCode, io.sphere.client.model.Reference) Variant.getPrice * */ public class Price { @Nonnull private Money value; private CountryCode country = null; @Nonnull private Reference customerGroup = EmptyReference.create("customerGroup"); private final Optional discounted; @JsonCreator Price(@Nonnull @JsonProperty("value") Money value, @JsonProperty("country") CountryCode country, @JsonProperty("customerGroup") Reference customerGroup, @JsonProperty("discounted") DiscountedPrice discounted) { this(value, country, customerGroup, Optional.fromNullable(discounted)); } @JsonIgnore public Price(final Money value, final CountryCode country, final Reference customerGroup, final Optional discounted) { this.value = value; this.country = country; this.discounted = discounted; this.customerGroup = customerGroup != null ? customerGroup : EmptyReference.create("customerGroup"); } @JsonIgnore public Price(Money value, CountryCode country, Reference customerGroup) { this(value, country, customerGroup, Optional.absent()); } /** The monetary value of the price. */ public Money getValue() { return value; } /** The optional country where this price applies, used in price calculations. Returns null if country is not defined. */ public CountryCode getCountry() { return country; } /** The optional country where this price applies, used in price calculations. * * @return The ISO 3166-1 (alpha-2) string representation of the country. If country is not defined, returns "" (empty string). * */ public String getCountryString() { return country == null ? "" : country.getAlpha2(); } /** The optional customer group for which this price applies, used in price calculations. */ @Nonnull public Reference getCustomerGroup() { return customerGroup; } public boolean matches(String currencyCode, CountryCode country, Reference customerGroup) { return (value == null || (value != null && value.getCurrencyCode().equals(currencyCode))) && (country == null ? this.country == null : country.equals(this.country)) && ((customerGroup == null || customerGroup.isEmpty()) ? this.customerGroup.isEmpty() : !this.customerGroup.isEmpty() && this.customerGroup.getId().equals(customerGroup.getId())); } public Optional getDiscounted() { return discounted; } // package private static final Predicate matchesP(final String currencyCode, final CountryCode country, final Reference customerGroup) { return new Predicate() { public boolean apply(Price p) { return p.matches(currencyCode, country, customerGroup); } }; } @Override public String toString() { String c = country == null ? "all-countries" : country.getAlpha2(); String g = customerGroup.isEmpty() ? "all-groups" : customerGroup.getId(); return "(" + value.toString() + ", " + c + ", " + g + ")"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy