com.landawn.abacus.util.Difference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-util-se Show documentation
Show all versions of abacus-util-se Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* 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;
/**
* The Class Difference.
*
* @author Haiyang Li
* @param
* @param
* @since 0.8
*/
public class Difference {
/** The common. */
final L common;
/** The left only. */
final L leftOnly;
/** The right only. */
final R rightOnly;
/**
* Instantiates a new difference.
*
* @param common
* @param leftOnly
* @param rightOnly
*/
Difference(L common, L leftOnly, R rightOnly) {
this.common = common;
this.leftOnly = leftOnly;
this.rightOnly = rightOnly;
}
/**
*
* @param
* @param
* @param
* @param
* @param a
* @param b
* @return
*/
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
*/
public static , R extends List> Difference of(Collection extends T1> a, Collection extends T2> b) {
List common = new ArrayList<>();
List leftOnly = new ArrayList<>();
List rightOnly = new ArrayList<>();
if (N.isNullOrEmpty(a)) {
if (N.isNullOrEmpty(b)) {
// Do nothing. All empty.
} else {
rightOnly.addAll(b);
}
} else if (N.isNullOrEmpty(b)) {
leftOnly.addAll(a);
} else {
final Multiset bOccurrences = Multiset.from(b);
for (T1 e : a) {
if (bOccurrences.getAndRemove(e) > 0) {
common.add(e);
} else {
leftOnly.add(e);
}
}
for (T2 e : b) {
if (bOccurrences.getAndRemove(e) > 0) {
rightOnly.add(e);
}
if (bOccurrences.isEmpty()) {
break;
}
}
}
return new Difference<>((L) common, (L) leftOnly, (R) 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 true, if successful
*/
@SuppressWarnings("rawtypes")
public boolean areEqual() {
return (leftOnly instanceof Map && (((Map) leftOnly).isEmpty() && ((Map) rightOnly).isEmpty()))
|| (leftOnly instanceof Collection && (((Collection) leftOnly).isEmpty() && ((Collection) rightOnly).isEmpty()));
}
/**
*
* @return
*/
@Override
public String toString() {
return "{inCommon=" + common + ", onLeftOnly=" + leftOnly + ", onRightOnly=" + rightOnly + "}";
}
/**
* The Class MapDifference.
*
* @param
* @param
* @param
*/
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 diff
*/
MapDifference(L common, L leftOnly, R rightOnly, D diff) {
super(common, leftOnly, rightOnly);
this.diffValues = diff;
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param map1
* @param map2
* @return
*/
@SuppressWarnings("unlikely-arg-type")
public static , R extends Map, D extends Map, Pair>> MapDifference of(
final Map extends K1, ? extends V1> map1, final Map extends K2, ? extends V2> map2) {
final L common = (L) new LinkedHashMap<>();
final L leftOnly = (L) new LinkedHashMap<>();
final R rightOnly = (R) new LinkedHashMap<>();
final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy