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