1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.shopping.model.impl;
16  
17  import com.liferay.portal.kernel.util.HashCode;
18  import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portlet.shopping.model.ShoppingCartItem;
22  import com.liferay.portlet.shopping.model.ShoppingItem;
23  
24  /**
25   * <a href="ShoppingCartItemImpl.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class ShoppingCartItemImpl implements ShoppingCartItem {
30  
31      public static String[] getFieldsArray(String fields) {
32          return StringUtil.split(fields, "&");
33      }
34  
35      public ShoppingCartItemImpl(ShoppingItem item, String fields) {
36          _item = item;
37          _fields = fields;
38      }
39  
40      public int compareTo(ShoppingCartItem cartItem) {
41          if (cartItem == null) {
42              return -1;
43          }
44  
45          int value = getItem().compareTo(cartItem.getItem());
46  
47          if (value == 0) {
48              value = getFields().compareTo(cartItem.getFields());
49          }
50  
51          return value;
52      }
53  
54      public boolean equals(Object obj) {
55          if (obj == null) {
56              return false;
57          }
58  
59          ShoppingCartItem cartItem = (ShoppingCartItem)obj;
60  
61          if (getItem().equals(cartItem.getItem()) &&
62              getFields().equals(cartItem.getFields())) {
63  
64              return true;
65          }
66          else {
67              return false;
68          }
69      }
70  
71      public String getCartItemId() {
72          long itemId = getItem().getItemId();
73  
74          if (Validator.isNull(_fields)) {
75              return String.valueOf(itemId);
76          }
77          else {
78              return itemId + "|" + _fields;
79          }
80      }
81  
82      public String getFields() {
83          return _fields;
84      }
85  
86      public String[] getFieldsArray() {
87          return getFieldsArray(_fields);
88      }
89  
90      public ShoppingItem getItem() {
91          return _item;
92      }
93  
94      public int hashCode() {
95          HashCode hashCode = HashCodeFactoryUtil.getHashCode();
96  
97          hashCode.append(_item.getItemId());
98          hashCode.append(_fields);
99  
100         return hashCode.toHashCode();
101     }
102 
103     private String _fields;
104     private ShoppingItem _item;
105 
106 }