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