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.DiffComplexFieldDefinition;
024import org.nuxeo.ecm.diff.model.DiffFieldItemDefinition;
025
026/**
027 * Default implementation of a {@link DiffComplexFieldDefinition}
028 *
029 * @author Antoine Taillefer (ataillefer@nuxeo.com)
030 * @since 5.6
031 */
032public class DiffComplexFieldDefinitionImpl implements DiffComplexFieldDefinition {
033
034    private static final long serialVersionUID = 5289865501066754428L;
035
036    protected String schema;
037
038    protected String name;
039
040    protected List<DiffFieldItemDefinition> includedItems;
041
042    protected List<DiffFieldItemDefinition> excludedItems;
043
044    public DiffComplexFieldDefinitionImpl(String schema, String name) {
045        this(schema, name, new ArrayList<DiffFieldItemDefinition>(), new ArrayList<DiffFieldItemDefinition>());
046    }
047
048    public DiffComplexFieldDefinitionImpl(String schema, String name, List<DiffFieldItemDefinition> includedItems,
049            List<DiffFieldItemDefinition> excludedItems) {
050        this.schema = schema;
051        this.name = name;
052        this.includedItems = includedItems;
053        this.excludedItems = excludedItems;
054    }
055
056    public String getSchema() {
057        return schema;
058    }
059
060    public String getName() {
061        return name;
062    }
063
064    public List<DiffFieldItemDefinition> getIncludedItems() {
065        return includedItems;
066    }
067
068    public List<DiffFieldItemDefinition> getExcludedItems() {
069        return excludedItems;
070    }
071
072    @Override
073    public boolean equals(Object other) {
074
075        if (this == other) {
076            return true;
077        }
078        if (other == null || !(other instanceof DiffComplexFieldDefinition)) {
079            return false;
080        }
081
082        String otherSchema = ((DiffComplexFieldDefinition) other).getSchema();
083        String otherName = ((DiffComplexFieldDefinition) other).getName();
084        if (schema == null && otherSchema == null && name == null && otherName == null) {
085            return true;
086        }
087        if (schema == null || otherSchema == null || name == null || otherName == null
088                || (schema != null && !schema.equals(otherSchema)) || (name != null && !name.equals(otherName))) {
089            return false;
090        }
091
092        List<DiffFieldItemDefinition> otherIncludedItems = ((DiffComplexFieldDefinition) other).getIncludedItems();
093        List<DiffFieldItemDefinition> otherExcludedItems = ((DiffComplexFieldDefinition) other).getExcludedItems();
094        if (CollectionUtils.isEmpty(includedItems) && CollectionUtils.isEmpty(otherIncludedItems)
095                && CollectionUtils.isEmpty(excludedItems) && CollectionUtils.isEmpty(otherExcludedItems)) {
096            return true;
097        }
098        if (CollectionUtils.isEmpty(includedItems) && !CollectionUtils.isEmpty(otherIncludedItems)
099                || !CollectionUtils.isEmpty(includedItems) && CollectionUtils.isEmpty(otherIncludedItems)
100                || !includedItems.equals(otherIncludedItems)) {
101            return false;
102        }
103        if (CollectionUtils.isEmpty(excludedItems) && !CollectionUtils.isEmpty(otherExcludedItems)
104                || !CollectionUtils.isEmpty(excludedItems) && CollectionUtils.isEmpty(otherExcludedItems)
105                || !excludedItems.equals(otherExcludedItems)) {
106            return false;
107        }
108
109        return true;
110    }
111
112    @Override
113    public String toString() {
114        StringBuilder sb = new StringBuilder();
115        sb.append(schema);
116        sb.append(":");
117        sb.append(name);
118        sb.append(!CollectionUtils.isEmpty(includedItems) ? " / " + includedItems : "");
119        sb.append(!CollectionUtils.isEmpty(excludedItems) ? " / " + excludedItems : "");
120        return sb.toString();
121    }
122}