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