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;
018
019import java.io.Serializable;
020
021/**
022 * Representation of a property (field) diff.
023 *
024 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
025 * @since 5.6
026 */
027public class PropertyDiff implements Serializable {
028
029    private static final long serialVersionUID = -8458912212588012911L;
030
031    protected String propertyType;
032
033    protected DifferenceType differenceType = DifferenceType.different;
034
035    /**
036     * Checks if is simple type.
037     *
038     * @return true, if is simple type
039     */
040    public boolean isSimpleType() {
041        return PropertyType.isSimpleType(propertyType);
042    }
043
044    /**
045     * Checks if is list type.
046     *
047     * @return true, if is list type
048     */
049    public boolean isListType() {
050        return PropertyType.isListType(propertyType);
051    }
052
053    /**
054     * Checks if is scalar list type.
055     *
056     * @return true, if is scalar list type
057     */
058    public boolean isScalarListType() {
059        return PropertyType.isScalarListType(propertyType);
060    }
061
062    /**
063     * Checks if is complex list type.
064     *
065     * @return true, if is complex list type
066     */
067    public boolean isComplexListType() {
068        return PropertyType.isComplexListType(propertyType);
069    }
070
071    /**
072     * Checks if is content list type.
073     *
074     * @return true, if is content list type
075     */
076    public boolean isContentListType(String propertyType) {
077        return PropertyType.isContentListType(propertyType);
078    }
079
080    /**
081     * Checks if is complex type.
082     *
083     * @return true, if is complex type
084     */
085    public boolean isComplexType() {
086        return PropertyType.isComplexType(propertyType);
087    }
088
089    /**
090     * Checks if is content type.
091     *
092     * @return true, if is content type
093     */
094    public boolean isContentType() {
095        return PropertyType.isContentType(propertyType);
096    }
097
098    public String getPropertyType() {
099        return propertyType;
100    }
101
102    public void setPropertyType(String propertyType) {
103        this.propertyType = propertyType;
104    }
105
106    public DifferenceType getDifferenceType() {
107        return differenceType;
108    }
109
110    public void setDifferenceType(DifferenceType differenceType) {
111        this.differenceType = differenceType;
112    }
113
114    @Override
115    public boolean equals(Object other) {
116
117        if (this == other) {
118            return true;
119        }
120        if (other == null || !(other instanceof PropertyDiff)) {
121            return false;
122        }
123        String otherPropertyType = ((PropertyDiff) other).getPropertyType();
124        DifferenceType otherDifferenceType = ((PropertyDiff) other).getDifferenceType();
125        return differenceType.equals(otherDifferenceType)
126                && ((propertyType == null && otherPropertyType == null) || (propertyType != null
127                        && otherPropertyType != null && propertyType.equals(otherPropertyType)));
128    }
129
130    @Override
131    public String toString() {
132
133        StringBuilder sb = new StringBuilder();
134        sb.append(" (");
135        sb.append(propertyType);
136        sb.append(", ");
137        sb.append(differenceType.name());
138        sb.append(")");
139        return sb.toString();
140    }
141}