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