All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.netbeans.modules.schema2beans.BeanComparator Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.modules.schema2beans;

import java.util.*;

/**
 *  The class BeanComparator is the default comparator implementation
 *  for comparing and merging schema2beans graphs. This class has mainly two
 *  methods. One for deciding if two BaseBean objects are identicals and
 *  a second one to decide if two properties are identicals.
 *
 *  These methods return either the original value to signal that they are
 *  the same or the second value, to signal that the two elements compared
 *  are not the same.
 *
 *  This default implementation compares both the propertie and attribute
 *  values to decide for equality. This implementation also uses the key
 *  information that might be defined in the mdd file (see user doc for this
 *  file usage). If the key is specified (default is all properties are keys),
 *  then only the properties defined as keys are used during the comparison.
 */
public class BeanComparator {
    //
    //	This is set to true if the processing of compareBean or
    //	compareProperty uses at least one key element.
    //	The caller can use this information to know if the result of a
    //	comparison is due to a real equality or simply a lack of key.
    //
    private boolean hasKey = false;
    
    //
    //	Specify if the bean comparator should use the keys specify
    //	in the mdd file. The default value is true.
    //	If this is set to false, any property is considered a key in
    //	in the comparison, regardless of what has been specified in the mdd.
    //
    private boolean useMddKeys = true;
    
    //	Same as above, but take precedence over it.
    static boolean useComparatorsMddKeys = true;
    
    /**
     *	Default comparison implementation for comparing two beans.
     *	The two beans are considered identical if all its non-bean properties
     *	are identicals.
     */
    public BaseBean compareBean(String 		beanName,
				BaseBean 	curBean,
				BaseBean 	newBean) {
				    
	BaseBean	ret = curBean;
	Iterator 	it = curBean.beanPropsIterator();
	boolean		useKeys = useMddKeys;
	
	this.hasKey = false;
	
	if (!useComparatorsMddKeys)
	    useKeys = false;

	if (curBean.getProperty() != null 
	    && curBean.getProperty().isKey()) {

	    //  Check the attributes first
	    BaseAttribute[] ba = curBean.listAttributes();

	    if (ba != null) {
		for(int j=0; j":curValue) +
                            ((curIndex==-1)?"":("."+curIndex)) + " / " +
                            ((newValue==null)?"":newValue) +
                            ((newIndex==-1)?"":("."+newIndex)) + " " +
                            ((ret == curValue)? "same":"different") +
                            " (" +((isKey)?"Key":"!Key") + ")");
        }
	
        return ret;
    }
    
    //
    //	Returns true if one of the element compared during the last call
    //	of compareBean and/or compareProperty used a key.
    //
    protected boolean hasKey() {
	return this.hasKey;
    }
    
    //
    //	Return if the key should be used comparing this element.
    //
    boolean hasKeyDefined(BeanProp prop) {
	boolean	useKeys = useMddKeys;
	
	if (!useComparatorsMddKeys)
	    useKeys = false;
	
	this.hasKey = Common.isKey(prop.type) || !useKeys;
	return this.hasKey;
    }
    
    public void enableKey(boolean b) {
	this.useMddKeys = b;
    }
    
    public static void enableComparatorsKey(boolean b) {
	useComparatorsMddKeys = b;
    }
    
}
















© 2015 - 2025 Weber Informatics LLC | Privacy Policy