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

com.landawn.abacus.util.Difference Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.3.16
Show newest version
/*
 * Copyright (C) 2016 HaiYang Li
 *
 * 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 com.landawn.abacus.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiPredicate;

/**
 *
 * @author Haiyang Li
 * @param 
 * @param 
 * @since 0.8
 * @see N#difference(Collection, Collection)
 * @see N#differentSet(Collection, Collection)
 * @see N#symmetricDifference(Collection, Collection)
 * @see N#symmetricDifferentSet(Collection, Collection)
 * @see N#excludeAll(Collection, Collection)
 * @see N#excludeAllToSet(Collection, Collection)
 * @see N#removeAll(Collection, Collection)
 * @see N#intersection(Collection, Collection)
 * @see N#commonSet(Collection, Collection)
 */
public class Difference {

    final L common;

    final L leftOnly;

    final R rightOnly;

    Difference(L common, L leftOnly, R rightOnly) {
        this.common = common;
        this.leftOnly = leftOnly;
        this.rightOnly = rightOnly;
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(boolean[] a, boolean[] b) {
        return of(BooleanList.of(a), BooleanList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(char[] a, char[] b) {
        return of(CharList.of(a), CharList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(byte[] a, byte[] b) {
        return of(ByteList.of(a), ByteList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(short[] a, short[] b) {
        return of(ShortList.of(a), ShortList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(int[] a, int[] b) {
        return of(IntList.of(a), IntList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(long[] a, long[] b) {
        return of(LongList.of(a), LongList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(float[] a, float[] b) {
        return of(FloatList.of(a), FloatList.of(b));
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(double[] a, double[] b) {
        return of(DoubleList.of(a), DoubleList.of(b));
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param 
     * @param a
     * @param b
     * @return
     * @see N#difference(Collection, Collection)
     * @see N#differentSet(Collection, Collection)
     * @see N#symmetricDifference(Collection, Collection)
     * @see N#symmetricDifferentSet(Collection, Collection)
     * @see N#excludeAll(Collection, Collection)
     * @see N#excludeAllToSet(Collection, Collection)
     * @see N#removeAll(Collection, Collection)
     * @see N#intersection(Collection, Collection)
     * @see N#commonSet(Collection, Collection)
     */
    public static , R extends List> Difference of(T1[] a, T2[] b) {
        return of(Arrays.asList(a), Arrays.asList(b));
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param 
     * @param a
     * @param b
     * @return
     * @see N#difference(Collection, Collection)
     * @see N#differentSet(Collection, Collection)
     * @see N#symmetricDifference(Collection, Collection)
     * @see N#symmetricDifferentSet(Collection, Collection)
     * @see N#excludeAll(Collection, Collection)
     * @see N#excludeAllToSet(Collection, Collection)
     * @see N#removeAll(Collection, Collection)
     * @see N#intersection(Collection, Collection)
     * @see N#commonSet(Collection, Collection)
     */
    @SuppressWarnings("unlikely-arg-type")
    public static , R extends List> Difference of(Collection a, Collection b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly.addAll(b);
            }
        } else if (N.isEmpty(b)) {
            leftOnly.addAll(a);
        } else {
            final Multiset bOccurrences = Multiset.create(b);

            for (T1 e : a) {
                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (T2 e : b) {
                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>((L) common, (L) leftOnly, (R) rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(BooleanList a, BooleanList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            boolean e = false;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(CharList a, CharList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            char e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(ByteList a, ByteList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            byte e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(ShortList a, ShortList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            short e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(IntList a, IntList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            int e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(LongList a, LongList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            long e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(FloatList a, FloatList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            float e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static Difference, List> of(DoubleList a, DoubleList b) {
        List common = new ArrayList<>();
        List leftOnly = new ArrayList<>();
        List rightOnly = new ArrayList<>();

        if (N.isEmpty(a)) {
            if (N.isEmpty(b)) {
                // Do nothing. All empty.
            } else {
                rightOnly = b.toList();
            }
        } else if (N.isEmpty(b)) {
            leftOnly = a.toList();
        } else {
            final Multiset bOccurrences = b.toMultiset();

            double e = 0;
            for (int i = 0, len = a.size(); i < len; i++) {
                e = a.get(i);

                if (bOccurrences.remove(e)) {
                    common.add(e);
                } else {
                    leftOnly.add(e);
                }
            }

            for (int i = 0, len = b.size(); i < len; i++) {
                e = b.get(i);

                if (bOccurrences.remove(e)) {
                    rightOnly.add(e);
                }

                if (bOccurrences.isEmpty()) {
                    break;
                }
            }
        }

        return new Difference<>(common, leftOnly, rightOnly);
    }

    /**
     *
     *
     * @return
     */
    public L inCommon() {
        return common;
    }

    /**
     * On left only.
     *
     * @return
     */
    public L onLeftOnly() {
        return leftOnly;
    }

    /**
     * On right only.
     *
     * @return
     */
    public R onRightOnly() {
        return rightOnly;
    }

    /**
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public boolean areEqual() {
        return (leftOnly instanceof Collection && (((Collection) leftOnly).isEmpty() && ((Collection) rightOnly).isEmpty()))
                || (leftOnly instanceof Map && (((Map) leftOnly).isEmpty() && ((Map) rightOnly).isEmpty()));
    }

    /**
     *
     *
     * @return
     */
    @Override
    public String toString() {
        return "{inCommon=" + common + ", onLeftOnly=" + leftOnly + ", onRightOnly=" + rightOnly + "}";
    }

    /**
     * The Class MapDifference.
     *
     * @param 
     * @param 
     * @param 
     *
     * @see N#difference(Collection, Collection)
     * @see N#differentSet(Collection, Collection)
     * @see N#symmetricDifference(Collection, Collection)
     * @see N#symmetricDifferentSet(Collection, Collection)
     * @see N#excludeAll(Collection, Collection)
     * @see N#excludeAllToSet(Collection, Collection)
     * @see N#removeAll(Collection, Collection)
     * @see N#intersection(Collection, Collection)
     * @see N#commonSet(Collection, Collection)
     */
    public static final class MapDifference extends Difference {

        /** The diff values. */
        private final D diffValues;

        /**
         * Instantiates a new map difference.
         *
         * @param common
         * @param leftOnly
         * @param rightOnly
         * @param withDifferentValues
         */
        MapDifference(L common, L leftOnly, R rightOnly, D withDifferentValues) {
            super(common, leftOnly, rightOnly);
            this.diffValues = withDifferentValues;
        }

        /**
         *
         * @param 
         * @param 
         * @param 
         * @param 
         * @param 
         * @param map1
         * @param map2
         * @return
         */
        public static  MapDifference, Map, Map>> of(
                final Map map1, final Map map2) {
            return of(map1, map2, Fn.equal());
        }

        /**
         *
         *
         * @param 
         * @param 
         * @param 
         * @param 
         * @param 
         * @param map1
         * @param map2
         * @param valueEquivalence
         * @return
         * @throws IllegalArgumentException
         */
        @SuppressWarnings("unlikely-arg-type")
        public static  MapDifference, Map, Map>> of(
                final Map map1, final Map map2,
                final BiPredicate valueEquivalence) throws IllegalArgumentException {
            N.checkArgNotNull(valueEquivalence, "valueEquivalence");

            final Map common = new LinkedHashMap<>();
            final Map leftOnly = new LinkedHashMap<>();
            final Map rightOnly = new LinkedHashMap<>();
            final Map> withDifferentValues = new LinkedHashMap<>();

            if (N.isEmpty(map1)) {
                if (N.isEmpty(map2)) {
                    // Do nothing. All empty.
                } else {
                    rightOnly.putAll(map2);
                }
            } else if (N.isEmpty(map2)) {
                leftOnly.putAll(map1);
            } else {
                Object key1 = null;
                V2 val2 = null;
                for (Entry entry1 : ((Map) map1).entrySet()) {
                    key1 = entry1.getKey();
                    val2 = map2.get(key1);

                    if (val2 == null) {
                        if (map2.containsKey(key1)) {
                            if (entry1.getValue() == null) {
                                common.put(entry1.getKey(), entry1.getValue());
                            } else {
                                withDifferentValues.put(entry1.getKey(), Pair.of(entry1.getValue(), val2));
                            }
                        } else {
                            leftOnly.put(entry1.getKey(), entry1.getValue());
                        }
                    } else if (valueEquivalence.test(entry1.getValue(), val2)) {
                        common.put(entry1.getKey(), entry1.getValue());
                    } else {
                        withDifferentValues.put(entry1.getKey(), Pair.of(entry1.getValue(), val2));
                    }
                }

                for (Entry entry2 : ((Map) map2).entrySet()) {
                    if (common.containsKey(entry2.getKey()) || withDifferentValues.containsKey(entry2.getKey())) {
                        continue;
                    }

                    rightOnly.put(entry2.getKey(), entry2.getValue());
                }
            }

            return new MapDifference<>(common, leftOnly, rightOnly, withDifferentValues);
        }

        /**
         *
         * @param bean1
         * @param bean2
         * @return
         */
        public static MapDifference, Map, Map>> of(final Object bean1, final Object bean2) {
            if (!ClassUtil.isBeanClass(bean1.getClass()) || !ClassUtil.isBeanClass(bean2.getClass())) {
                throw new IllegalArgumentException(bean1.getClass().getCanonicalName() + " or " + bean2.getClass().getCanonicalName() + " is not a bean class");
            }

            return of(Maps.bean2Map(bean1), Maps.bean2Map(bean2));
        }

        /**
         *
         * @param bean1
         * @param bean2
         * @param valueEquivalence
         * @return
         */
        public static MapDifference, Map, Map>> of(final Object bean1, final Object bean2,
                final BiPredicate valueEquivalence) {
            if (!ClassUtil.isBeanClass(bean1.getClass()) || !ClassUtil.isBeanClass(bean2.getClass())) {
                throw new IllegalArgumentException(bean1.getClass().getCanonicalName() + " or " + bean2.getClass().getCanonicalName() + " is not a bean class");
            }

            final Map map1 = Maps.bean2Map(bean1);
            final Map map2 = Maps.bean2Map(bean2);
            final BiPredicate valueEquivalenceToUse = (BiPredicate) valueEquivalence;

            return of(map1, map2, valueEquivalenceToUse);
        }

        /**
         * Returns the entries that appear in both maps, but with different values.
         *
         * @return
         */
        public D withDifferentValues() {
            return diffValues;
        }

        /**
         *
         * @return
         */
        @Override
        @SuppressWarnings("rawtypes")
        public boolean areEqual() {
            return super.areEqual() && ((Map) diffValues).isEmpty();
        }

        /**
         *
         * @return
         */
        @Override
        public String toString() {
            return "{inCommon=" + common + ", onLeftOnly=" + leftOnly + ", onRightOnly=" + rightOnly + ", withDifferentValues=" + diffValues + "}";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy