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 org.nuxeo.ecm.diff.model.DifferenceType;
020import org.nuxeo.ecm.diff.model.PropertyDiff;
021
022/**
023 * Implementation of {@code PropertyDiff} for a simple property.
024 *
025 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
026 * @since 5.6
027 */
028public class SimplePropertyDiff extends PropertyDiff {
029
030    private static final long serialVersionUID = -1100714461537900354L;
031
032    protected String leftValue;
033
034    protected String rightValue;
035
036    /**
037     * Instantiates a new simple property diff with a property type.
038     *
039     * @param propertyType the property type
040     */
041    public SimplePropertyDiff(String propertyType) {
042        this.propertyType = propertyType;
043    }
044
045    /**
046     * Instantiates a new simple property diff with a property type, the {@link DifferenceType#different} difference
047     * type, a left value and right value.
048     *
049     * @param propertyType the property type
050     * @param leftValue the left value
051     * @param rightValue the right value
052     */
053    public SimplePropertyDiff(String propertyType, String leftValue, String rightValue) {
054
055        this(propertyType, DifferenceType.different, leftValue, rightValue);
056    }
057
058    /**
059     * Instantiates a new simple property diff with a property type, difference type, left value and right value.
060     *
061     * @param propertyType the property type
062     * @param differenceType the difference type
063     * @param leftValue the left value
064     * @param rightValue the right value
065     */
066    public SimplePropertyDiff(String propertyType, DifferenceType differenceType, String leftValue, String rightValue) {
067
068        this.propertyType = propertyType;
069        this.differenceType = differenceType;
070        this.leftValue = leftValue;
071        this.rightValue = rightValue;
072    }
073
074    @Override
075    public boolean equals(Object other) {
076
077        if (!super.equals(other)) {
078            return false;
079        }
080        if (this == other) {
081            return true;
082        }
083        if (!(other instanceof SimplePropertyDiff)) {
084            return false;
085        }
086
087        String otherLeftValue = ((SimplePropertyDiff) other).getLeftValue();
088        String otherRightValue = ((SimplePropertyDiff) other).getRightValue();
089
090        return (leftValue == null && otherLeftValue == null && rightValue == null && otherRightValue == null)
091                || (leftValue == null && otherLeftValue == null && rightValue != null && rightValue.equals(otherRightValue))
092                || (rightValue == null && otherRightValue == null && leftValue != null && leftValue.equals(otherLeftValue))
093                || (leftValue != null && rightValue != null && leftValue.equals(otherLeftValue) && rightValue.equals(otherRightValue));
094    }
095
096    @Override
097    public String toString() {
098
099        StringBuilder sb = new StringBuilder();
100
101        sb.append(leftValue);
102        sb.append(" --> ");
103        sb.append(rightValue);
104        sb.append(super.toString());
105
106        return sb.toString();
107    }
108
109    public String getLeftValue() {
110        return leftValue;
111    }
112
113    public void setLeftValue(String leftValue) {
114        this.leftValue = leftValue;
115    }
116
117    public String getRightValue() {
118        return rightValue;
119    }
120
121    public void setRightValue(String rightValue) {
122        this.rightValue = rightValue;
123    }
124}