001    /**
002     * Copyright (c) 2000-2013 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            @Override
039            public int compareTo(ShoppingCartItem cartItem) {
040                    if (cartItem == null) {
041                            return -1;
042                    }
043    
044                    int value = getItem().compareTo(cartItem.getItem());
045    
046                    if (value == 0) {
047                            value = getFields().compareTo(cartItem.getFields());
048                    }
049    
050                    return value;
051            }
052    
053            @Override
054            public boolean equals(Object obj) {
055                    if (this == obj) {
056                            return true;
057                    }
058    
059                    if (!(obj instanceof ShoppingCartItem)) {
060                            return false;
061                    }
062    
063                    ShoppingCartItem cartItem = (ShoppingCartItem)obj;
064    
065                    if (getItem().equals(cartItem.getItem()) &&
066                            getFields().equals(cartItem.getFields())) {
067    
068                            return true;
069                    }
070                    else {
071                            return false;
072                    }
073            }
074    
075            @Override
076            public String getCartItemId() {
077                    long itemId = getItem().getItemId();
078    
079                    if (Validator.isNull(_fields)) {
080                            return String.valueOf(itemId);
081                    }
082                    else {
083                            return itemId + "|" + _fields;
084                    }
085            }
086    
087            @Override
088            public String getFields() {
089                    return _fields;
090            }
091    
092            @Override
093            public String[] getFieldsArray() {
094                    return getFieldsArray(_fields);
095            }
096    
097            @Override
098            public ShoppingItem getItem() {
099                    return _item;
100            }
101    
102            @Override
103            public int hashCode() {
104                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
105    
106                    hashCode.append(_item.getItemId());
107                    hashCode.append(_fields);
108    
109                    return hashCode.toHashCode();
110            }
111    
112            private String _fields;
113            private ShoppingItem _item;
114    
115    }