001
014
015 package com.liferay.portlet.shopping.model.impl;
016
017 import com.liferay.portal.kernel.util.HashCode;
018 import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portlet.shopping.model.ShoppingCartItem;
022 import com.liferay.portlet.shopping.model.ShoppingItem;
023
024
027 public class ShoppingCartItemImpl implements ShoppingCartItem {
028
029 public static String[] getFieldsArray(String fields) {
030 return StringUtil.split(fields, '&');
031 }
032
033 public ShoppingCartItemImpl(ShoppingItem item, String fields) {
034 _item = item;
035 _fields = fields;
036 }
037
038 public int compareTo(ShoppingCartItem cartItem) {
039 if (cartItem == null) {
040 return -1;
041 }
042
043 int value = getItem().compareTo(cartItem.getItem());
044
045 if (value == 0) {
046 value = getFields().compareTo(cartItem.getFields());
047 }
048
049 return value;
050 }
051
052 @Override
053 public boolean equals(Object obj) {
054 if (obj == null) {
055 return false;
056 }
057
058 ShoppingCartItem cartItem = (ShoppingCartItem)obj;
059
060 if (getItem().equals(cartItem.getItem()) &&
061 getFields().equals(cartItem.getFields())) {
062
063 return true;
064 }
065 else {
066 return false;
067 }
068 }
069
070 public String getCartItemId() {
071 long itemId = getItem().getItemId();
072
073 if (Validator.isNull(_fields)) {
074 return String.valueOf(itemId);
075 }
076 else {
077 return itemId + "|" + _fields;
078 }
079 }
080
081 public String getFields() {
082 return _fields;
083 }
084
085 public String[] getFieldsArray() {
086 return getFieldsArray(_fields);
087 }
088
089 public ShoppingItem getItem() {
090 return _item;
091 }
092
093 @Override
094 public int hashCode() {
095 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
096
097 hashCode.append(_item.getItemId());
098 hashCode.append(_fields);
099
100 return hashCode.toHashCode();
101 }
102
103 private String _fields;
104 private ShoppingItem _item;
105
106 }