alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

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;
    }
    
}












... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.