001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer
016 */
017package org.nuxeo.ecm.diff.model.impl;
018
019import java.io.Serializable;
020
021import org.nuxeo.ecm.diff.model.DifferenceType;
022import org.nuxeo.ecm.diff.model.PropertyDiffDisplay;
023
024/**
025 * Default implementation of {@link PropertyDiffDisplay}.
026 *
027 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
028 * @since 5.6
029 */
030public class PropertyDiffDisplayImpl implements PropertyDiffDisplay {
031
032    private static final long serialVersionUID = 875764424409126845L;
033
034    protected Serializable value;
035
036    protected DifferenceType differenceType;
037
038    protected String styleClass = DEFAULT_STYLE_CLASS;
039
040    public PropertyDiffDisplayImpl(Serializable value) {
041        this(value, DifferenceType.different);
042    }
043
044    public PropertyDiffDisplayImpl(Serializable value, DifferenceType differenceType) {
045        this(value, differenceType, null);
046    }
047
048    public PropertyDiffDisplayImpl(Serializable value, String styleClass) {
049        this(value, DifferenceType.different, styleClass);
050    }
051
052    public PropertyDiffDisplayImpl(Serializable value, DifferenceType differenceType, String styleClass) {
053        this.value = value;
054        this.differenceType = differenceType;
055        this.styleClass = styleClass;
056    }
057
058    public Serializable getValue() {
059        return value;
060    }
061
062    public void setValue(Serializable value) {
063        this.value = value;
064    }
065
066    public DifferenceType getDifferenceType() {
067        return differenceType;
068    }
069
070    public void setDifferenceType(DifferenceType differenceType) {
071        this.differenceType = differenceType;
072    }
073
074    public String getStyleClass() {
075        return styleClass;
076    }
077
078    public void setStyleClass(String styleClass) {
079        this.styleClass = styleClass;
080    }
081
082    @Override
083    public boolean equals(Object other) {
084
085        if (this == other) {
086            return true;
087        }
088        if (other == null || !(other instanceof PropertyDiffDisplay)) {
089            return false;
090        }
091        Serializable otherValue = ((PropertyDiffDisplay) other).getValue();
092        Serializable otherStyleClass = ((PropertyDiffDisplay) other).getStyleClass();
093        DifferenceType otherDifferenceType = ((PropertyDiffDisplay) other).getDifferenceType();
094        if (value == null && otherValue == null && styleClass == null && otherStyleClass == null
095                && differenceType.equals(otherDifferenceType)) {
096            return true;
097        }
098        return differenceType.equals(otherDifferenceType)
099                && (value == null && otherValue == null && styleClass != null && styleClass.equals(otherStyleClass)
100                        || styleClass == null && otherStyleClass == null && value != null && value.equals(otherValue) || value != null
101                        && value.equals(otherValue) && styleClass != null && styleClass.equals(otherStyleClass));
102    }
103
104    @Override
105    public String toString() {
106        StringBuilder sb = new StringBuilder();
107        sb.append(value);
108        sb.append(" / ");
109        sb.append(differenceType.name());
110        sb.append(" / ");
111        sb.append(styleClass);
112        return sb.toString();
113    }
114}