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