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.List;
021
022import org.apache.commons.collections.CollectionUtils;
023import org.nuxeo.ecm.diff.model.DiffFieldDefinition;
024import org.nuxeo.ecm.diff.model.DiffFieldItemDefinition;
025
026/**
027 * Default implementation of a {@link DiffFieldDefinition}.
028 *
029 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
030 * @since 5.6
031 */
032public class DiffFieldDefinitionImpl implements DiffFieldDefinition {
033
034    private static final long serialVersionUID = 6192730067253949180L;
035
036    protected String category;
037
038    protected String schema;
039
040    protected String name;
041
042    protected boolean displayContentDiffLinks;
043
044    protected List<DiffFieldItemDefinition> items;
045
046    public DiffFieldDefinitionImpl(String category, String schema, String name) {
047        this(category, schema, name, false);
048    }
049
050    public DiffFieldDefinitionImpl(String category, String schema, String name, boolean displayContentDiffLinks) {
051        this(category, schema, name, displayContentDiffLinks, new ArrayList<DiffFieldItemDefinition>());
052    }
053
054    public DiffFieldDefinitionImpl(String category, String schema, String name, List<DiffFieldItemDefinition> items) {
055        this(category, schema, name, false, items);
056    }
057
058    public DiffFieldDefinitionImpl(String category, String schema, String name, boolean displayContentDiffLinks,
059            List<DiffFieldItemDefinition> items) {
060        this.category = category;
061        this.schema = schema;
062        this.name = name;
063        this.displayContentDiffLinks = displayContentDiffLinks;
064        this.items = items;
065    }
066
067    public String getCategory() {
068        return category;
069    }
070
071    public String getSchema() {
072        return schema;
073    }
074
075    public String getName() {
076        return name;
077    }
078
079    public boolean isDisplayContentDiffLinks() {
080        return displayContentDiffLinks;
081    }
082
083    public List<DiffFieldItemDefinition> getItems() {
084        return items;
085    }
086
087    @Override
088    public boolean equals(Object other) {
089
090        if (this == other) {
091            return true;
092        }
093        if (other == null || !(other instanceof DiffFieldDefinition)) {
094            return false;
095        }
096
097        String otherCategory = ((DiffFieldDefinition) other).getCategory();
098        String otherSchema = ((DiffFieldDefinition) other).getSchema();
099        String otherName = ((DiffFieldDefinition) other).getName();
100        boolean otherDisplayContentDiffLinks = ((DiffFieldDefinition) other).isDisplayContentDiffLinks();
101        if (category == null && otherCategory == null && schema == null && otherSchema == null && name == null
102                && otherName == null) {
103            return true;
104        }
105        if (schema == null || otherSchema == null || name == null || otherName == null
106                || (category == null && otherCategory != null) || (category != null && otherCategory == null)
107                || (category != null && !category.equals(otherCategory))
108                || (schema != null && !schema.equals(otherSchema)) || (name != null && !name.equals(otherName))
109                || displayContentDiffLinks != otherDisplayContentDiffLinks) {
110            return false;
111        }
112
113        List<DiffFieldItemDefinition> otherItems = ((DiffFieldDefinition) other).getItems();
114        if (CollectionUtils.isEmpty(items) && CollectionUtils.isEmpty(otherItems)) {
115            return true;
116        }
117        if (CollectionUtils.isEmpty(items) && !CollectionUtils.isEmpty(otherItems) || !CollectionUtils.isEmpty(items)
118                && CollectionUtils.isEmpty(otherItems) || !items.equals(otherItems)) {
119            return false;
120        }
121
122        return true;
123    }
124
125    @Override
126    public String toString() {
127        StringBuilder sb = new StringBuilder();
128        sb.append("category=");
129        sb.append(category);
130        sb.append(" / ");
131        sb.append(schema);
132        sb.append(":");
133        sb.append(name);
134        sb.append(" / ");
135        sb.append("displayContentDiffLinks=");
136        sb.append(displayContentDiffLinks);
137        sb.append(!CollectionUtils.isEmpty(items) ? " / " + items : "");
138        return sb.toString();
139    }
140}