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.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.diff.model.PropertyDiff;
023import org.nuxeo.ecm.diff.model.PropertyType;
024
025/**
026 * Implementation of {@link PropertyDiff} for a complex property.
027 *
028 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
029 * @since 5.6
030 */
031public class ComplexPropertyDiff extends PropertyDiff {
032
033    private static final long serialVersionUID = -1100714461537900354L;
034
035    protected Map<String, PropertyDiff> diffMap;
036
037    /**
038     * Instantiates a new complex property diff with the {@link PropertyType#COMPLEX} property type.
039     */
040    public ComplexPropertyDiff() {
041
042        this.propertyType = PropertyType.COMPLEX;
043        diffMap = new HashMap<String, PropertyDiff>();
044    }
045
046    /**
047     * Instantiates a new complex property diff with a property type.
048     */
049    public ComplexPropertyDiff(String propertyType) {
050
051        this.propertyType = propertyType;
052        diffMap = new HashMap<String, PropertyDiff>();
053    }
054
055    /**
056     * Gets the diff.
057     *
058     * @param item the item
059     * @return the diff
060     */
061    public PropertyDiff getDiff(String item) {
062        return diffMap.get(item);
063    }
064
065    /**
066     * Put diff.
067     *
068     * @param item the item
069     * @param diff the diff
070     * @return the property diff
071     */
072    public PropertyDiff putDiff(String item, PropertyDiff diff) {
073        return diffMap.put(item, diff);
074    }
075
076    /**
077     * Put all diffs.
078     *
079     * @param otherDiff the other diff
080     */
081    public void putAllDiff(ComplexPropertyDiff otherDiff) {
082        diffMap.putAll(otherDiff.getDiffMap());
083    }
084
085    public Map<String, PropertyDiff> getDiffMap() {
086        return diffMap;
087    }
088
089    @Override
090    public boolean equals(Object other) {
091
092        if (!super.equals(other)) {
093            return false;
094        }
095        if (this == other) {
096            return true;
097        }
098        if (other == null || !(other instanceof ComplexPropertyDiff)) {
099            return false;
100        }
101        Map<String, PropertyDiff> otherDiffMap = ((ComplexPropertyDiff) other).getDiffMap();
102        return (diffMap == null && otherDiffMap == null)
103                || (diffMap != null && otherDiffMap != null && diffMap.equals(otherDiffMap));
104
105    }
106
107    @Override
108    public String toString() {
109        return diffMap.toString() + super.toString();
110    }
111}