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

org.gradle.util.DiffUtil Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed 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.gradle.util;

import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang.ObjectUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class DiffUtil {
    public static  void diff(Set newSet, Set oldSet, ChangeListener changeListener) {
        Set added = new HashSet(newSet);
        added.removeAll(oldSet);
        for (T t : added) {
            changeListener.added(t);
        }

        Set removed = new HashSet(oldSet);
        removed.removeAll(newSet);
        for (T t : removed) {
            changeListener.removed(t);
        }
    }

    public static  void diff(Map newMap, Map oldMap, ChangeListener> changeListener) {
        Map added = new HashMap(newMap);
        added.keySet().removeAll(oldMap.keySet());
        for (Map.Entry entry : added.entrySet()) {
            changeListener.added(entry);
        }

        Map removed = new HashMap(oldMap);
        removed.keySet().removeAll(newMap.keySet());
        for (Map.Entry entry : removed.entrySet()) {
            changeListener.removed(entry);
        }

        Map same = new HashMap(newMap);
        same.keySet().retainAll(oldMap.keySet());
        for (Map.Entry entry : same.entrySet()) {
            if (!checkEquality(entry.getValue(), oldMap.get(entry.getKey()))) {
                changeListener.changed(entry);
            }
        }
    }

    @VisibleForTesting
    static boolean checkEquality(Object obj1, Object obj2) {
        return ObjectUtils.equals(obj1, obj2) || checkEnumEquality(obj1, obj2);
    }

    private static boolean checkEnumEquality(Object obj1, Object obj2) {
        if (!(obj1 instanceof Enum) || !(obj2 instanceof Enum)) {
            return false;
        }

        Enum e1 = (Enum) obj1;
        Enum e2 = (Enum) obj2;

        // Check enum equality without checking loading ClassLoader.
        // There is a slight risk that two versions of the same enum class are compared,
        // (that's why classloaders are used in equality checks), but checking both name
        // and ordinal should make this very unlikely.
        return e1.getDeclaringClass().getCanonicalName().equals(e2.getDeclaringClass().getCanonicalName())
            && e1.ordinal() == e2.ordinal()
            && e1.name().equals(e2.name());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy