1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 }