platform35/org.eclipse.core.resources/src/org/eclipse/core/internal/watson/DefaultElementComparator.java
changeset 40 eb3c938c7fef
equal deleted inserted replaced
39:2a03ec4dbf31 40:eb3c938c7fef
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2000, 2005 IBM Corporation and others.
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     IBM Corporation - initial API and implementation
       
    10  *******************************************************************************/
       
    11 package org.eclipse.core.internal.watson;
       
    12 
       
    13 /**
       
    14  * This is what you would expect for an element tree comparator.
       
    15  * Clients of the element tree that want specific comparison behaviour
       
    16  * must define their own element comparator (without subclassing or
       
    17  * otherwise extending this comparator).  Internal element tree operations 
       
    18  * rely on the behaviour of this type, and the ElementTree maintainer 
       
    19  * reserves the right to change its behaviour as necessary.
       
    20  */
       
    21 public final class DefaultElementComparator implements IElementComparator {
       
    22 	private static DefaultElementComparator singleton;
       
    23 
       
    24 	/**
       
    25 	 * Force clients to use the singleton
       
    26 	 */
       
    27 	protected DefaultElementComparator() {
       
    28 		super();
       
    29 	}
       
    30 
       
    31 	/**
       
    32 	 * Returns the type of change.
       
    33 	 */
       
    34 	public int compare(Object oldInfo, Object newInfo) {
       
    35 		if (oldInfo == null && newInfo == null)
       
    36 			return 0;
       
    37 		if (oldInfo == null || newInfo == null)
       
    38 			return 1;
       
    39 		return testEquality(oldInfo, newInfo) ? 0 : 1;
       
    40 	}
       
    41 
       
    42 	/**
       
    43 	 * Returns the singleton instance
       
    44 	 */
       
    45 	public static IElementComparator getComparator() {
       
    46 		if (singleton == null) {
       
    47 			singleton = new DefaultElementComparator();
       
    48 		}
       
    49 		return singleton;
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Makes a comparison based on equality
       
    54 	 */
       
    55 	protected boolean testEquality(Object oldInfo, Object newInfo) {
       
    56 		if (oldInfo == null && newInfo == null)
       
    57 			return true;
       
    58 		if (oldInfo == null || newInfo == null)
       
    59 			return false;
       
    60 
       
    61 		return oldInfo.equals(newInfo);
       
    62 	}
       
    63 }