001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer
018 */
019package org.nuxeo.ecm.diff.model.impl;
020
021import org.nuxeo.ecm.diff.model.DifferenceType;
022import org.nuxeo.ecm.diff.model.PropertyDiff;
023
024/**
025 * Implementation of {@code PropertyDiff} for a simple property.
026 *
027 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
028 * @since 5.6
029 */
030public class SimplePropertyDiff extends PropertyDiff {
031
032    private static final long serialVersionUID = -1100714461537900354L;
033
034    protected String leftValue;
035
036    protected String rightValue;
037
038    /**
039     * Instantiates a new simple property diff with a property type.
040     *
041     * @param propertyType the property type
042     */
043    public SimplePropertyDiff(String propertyType) {
044        this.propertyType = propertyType;
045    }
046
047    /**
048     * Instantiates a new simple property diff with a property type, the {@link DifferenceType#different} difference
049     * type, a left value and right value.
050     *
051     * @param propertyType the property type
052     * @param leftValue the left value
053     * @param rightValue the right value
054     */
055    public SimplePropertyDiff(String propertyType, String leftValue, String rightValue) {
056
057        this(propertyType, DifferenceType.different, leftValue, rightValue);
058    }
059
060    /**
061     * Instantiates a new simple property diff with a property type, difference type, left value and right value.
062     *
063     * @param propertyType the property type
064     * @param differenceType the difference type
065     * @param leftValue the left value
066     * @param rightValue the right value
067     */
068    public SimplePropertyDiff(String propertyType, DifferenceType differenceType, String leftValue, String rightValue) {
069
070        this.propertyType = propertyType;
071        this.differenceType = differenceType;
072        this.leftValue = leftValue;
073        this.rightValue = rightValue;
074    }
075
076    @Override
077    public boolean equals(Object other) {
078
079        if (!super.equals(other)) {
080            return false;
081        }
082        if (this == other) {
083            return true;
084        }
085        if (!(other instanceof SimplePropertyDiff)) {
086            return false;
087        }
088
089        String otherLeftValue = ((SimplePropertyDiff) other).getLeftValue();
090        String otherRightValue = ((SimplePropertyDiff) other).getRightValue();
091
092        return (leftValue == null && otherLeftValue == null && rightValue == null && otherRightValue == null)
093                || (leftValue == null && otherLeftValue == null && rightValue != null && rightValue.equals(otherRightValue))
094                || (rightValue == null && otherRightValue == null && leftValue != null && leftValue.equals(otherLeftValue))
095                || (leftValue != null && rightValue != null && leftValue.equals(otherLeftValue) && rightValue.equals(otherRightValue));
096    }
097
098    @Override
099    public String toString() {
100
101        StringBuilder sb = new StringBuilder();
102
103        sb.append(leftValue);
104        sb.append(" --> ");
105        sb.append(rightValue);
106        sb.append(super.toString());
107
108        return sb.toString();
109    }
110
111    public String getLeftValue() {
112        return leftValue;
113    }
114
115    public void setLeftValue(String leftValue) {
116        this.leftValue = leftValue;
117    }
118
119    public String getRightValue() {
120        return rightValue;
121    }
122
123    public void setRightValue(String rightValue) {
124        this.rightValue = rightValue;
125    }
126}