001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Contributors:
018 *     Anahide Tchertchian
019 */
020
021package org.nuxeo.ecm.platform.forms.layout.api.impl;
022
023/**
024 * Utility class for definitions comparison.
025 * <p>
026 * Excerpts copied here to avoid dependencies to third party libs.
027 *
028 * @since 7.2
029 */
030public class EqualsBuilder {
031
032    protected boolean isEquals = true;
033
034    public boolean isEquals() {
035        return isEquals;
036    }
037
038    protected void setEquals(boolean isEquals) {
039        this.isEquals = isEquals;
040    }
041
042    @SuppressWarnings("rawtypes")
043    public EqualsBuilder append(Object lhs, Object rhs) {
044        if (isEquals == false) {
045            return this;
046        }
047        if (lhs == rhs) {
048            return this;
049        }
050        if (lhs == null || rhs == null) {
051            this.setEquals(false);
052            return this;
053        }
054        Class lhsClass = lhs.getClass();
055        if (!lhsClass.isArray()) {
056            // The simple case, not an array, just test the element
057            isEquals = lhs.equals(rhs);
058        } else if (lhs.getClass() != rhs.getClass()) {
059            // Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
060            this.setEquals(false);
061        } else if (lhs instanceof Object[]) {
062            append((Object[]) lhs, (Object[]) rhs);
063        } else {
064            // assume there are no arrays of primitives
065            append(lhs, rhs);
066        }
067        return this;
068    }
069
070    public EqualsBuilder append(long lhs, long rhs) {
071        if (isEquals == false) {
072            return this;
073        }
074        isEquals = (lhs == rhs);
075        return this;
076    }
077
078    public EqualsBuilder append(boolean lhs, boolean rhs) {
079        if (isEquals == false) {
080            return this;
081        }
082        isEquals = (lhs == rhs);
083        return this;
084    }
085
086    public EqualsBuilder append(Object[] lhs, Object[] rhs) {
087        if (isEquals == false) {
088            return this;
089        }
090        if (lhs == rhs) {
091            return this;
092        }
093        if (lhs == null || rhs == null) {
094            this.setEquals(false);
095            return this;
096        }
097        if (lhs.length != rhs.length) {
098            this.setEquals(false);
099            return this;
100        }
101        for (int i = 0; i < lhs.length && isEquals; ++i) {
102            append(lhs[i], rhs[i]);
103        }
104        return this;
105    }
106
107}