Beispiel #1
0
    /**
     * @param map1
     * @param map2
     * @return
     */
    public static <
            K1,
            V1,
            K2,
            V2,
            L extends Map<? super K1, ? super V1>,
            R extends Map<? super K2, ? super V2>,
            D extends Map<?, Pair0<V1, V2>>>
        MapDifference<L, R, D> of(Map<K1, V1> map1, Map<K2, V2> map2) {
      final L common = (L) new LinkedHashMap<>();
      final L leftOnly = (L) new LinkedHashMap<>();
      final R rightOnly = (R) new LinkedHashMap<>();
      final Map<Object, Pair0<V1, V2>> diff = new LinkedHashMap<>();

      if (N.isNullOrEmpty(map1)) {
        if (N.isNullOrEmpty(map2)) {
          // Do nothing. All empty.
        } else {
          rightOnly.putAll(map2);
        }
      } else if (N.isNullOrEmpty(map2)) {
        leftOnly.putAll(map1);
      } else {
        V2 val2 = null;
        for (Entry<K1, V1> entry1 : map1.entrySet()) {
          val2 = map2.get(entry1.getKey());

          if (val2 == null) {
            if (map2.containsKey(entry1.getKey())) {
              if (entry1.getValue() == null) {
                common.put(entry1.getKey(), entry1.getValue());
              } else {
                diff.put(entry1.getKey(), Pair0.of(entry1.getValue(), val2));
              }
            } else {
              leftOnly.put(entry1.getKey(), entry1.getValue());
            }
          } else if (N.equals(entry1.getValue(), val2)) {
            common.put(entry1.getKey(), entry1.getValue());
          } else {
            diff.put(entry1.getKey(), Pair0.of(entry1.getValue(), val2));
          }
        }

        for (Entry<K2, V2> entry2 : map2.entrySet()) {
          if (common.containsKey(entry2.getKey()) || diff.containsKey(entry2.getKey())) {
            continue;
          }

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

      return new MapDifference<L, R, D>(common, leftOnly, rightOnly, (D) diff);
    }
Beispiel #2
0
  /**
   * @param a
   * @param b
   * @return
   */
  public static <T1, T2, L extends List<? super T1>, R extends List<? super T2>>
      Difference<L, R> of(Collection<T1> a, Collection<T2> b) {
    List<T1> common = new ArrayList<>();
    List<T1> leftOnly = new ArrayList<>();
    List<T2> 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 {
      common = N.intersect(a, b);
      leftOnly = N.except(a, b);
      rightOnly = N.except(b, a);
    }

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