001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
025     * @author Brian Wing Shun Chan
026     */
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            public boolean equals(Object obj) {
053                    if (obj == null) {
054                            return false;
055                    }
056    
057                    ShoppingCartItem cartItem = (ShoppingCartItem)obj;
058    
059                    if (getItem().equals(cartItem.getItem()) &&
060                            getFields().equals(cartItem.getFields())) {
061    
062                            return true;
063                    }
064                    else {
065                            return false;
066                    }
067            }
068    
069            public String getCartItemId() {
070                    long itemId = getItem().getItemId();
071    
072                    if (Validator.isNull(_fields)) {
073                            return String.valueOf(itemId);
074                    }
075                    else {
076                            return itemId + "|" + _fields;
077                    }
078            }
079    
080            public String getFields() {
081                    return _fields;
082            }
083    
084            public String[] getFieldsArray() {
085                    return getFieldsArray(_fields);
086            }
087    
088            public ShoppingItem getItem() {
089                    return _item;
090            }
091    
092            public int hashCode() {
093                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
094    
095                    hashCode.append(_item.getItemId());
096                    hashCode.append(_fields);
097    
098                    return hashCode.toHashCode();
099            }
100    
101            private String _fields;
102            private ShoppingItem _item;
103    
104    }