/*
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.com.bottega.ecommerce.sales.domain.offer;
import java.util.ArrayList;
import java.util.List;
import pl.com.bottega.ddd.annotations.domain.ValueObject;
import pl.com.bottega.ecommerce.canonicalmodel.publishedlanguage.AggregateId;
/**
* Offer that is available per client (including availability and discounts)
*
* @author Slawek
*
*/
@ValueObject
public class Offer {
private List<OfferItem> availabeItems = new ArrayList<OfferItem>();
private List<OfferItem> unavailableItems = new ArrayList<OfferItem>();
public Offer(List<OfferItem> availabeItems, List<OfferItem> unavailableItems) {
this.availabeItems = availabeItems;
this.unavailableItems = unavailableItems;
}
public List<OfferItem> getAvailabeItems() {
return availabeItems;
}
public List<OfferItem> getUnavailableItems() {
return unavailableItems;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((availabeItems == null) ? 0 : availabeItems.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Offer other = (Offer) obj;
if (availabeItems == null) {
if (other.availabeItems != null)
return false;
} else if (!availabeItems.equals(other.availabeItems))
return false;
return true;
}
/**
*
* @param seenOffer
* @param delta acceptable difference in percent
* @return
*/
public boolean sameAs(Offer seenOffer, double delta) {
if (! (availabeItems.size() == seenOffer.availabeItems.size()))
return false;
for (OfferItem item : availabeItems) {
OfferItem sameItem = seenOffer.findItem(item.getProductData().getProductId());
if (sameItem == null)
return false;
if (!sameItem.sameAs(item, delta))
return false;
}
return true;
}
private OfferItem findItem(AggregateId productId) {
for (OfferItem item : availabeItems){
if (item.getProductData().getProductId().equals(productId))
return item;
}
return null;
}
}