Class ObjectMerger

java.lang.Object
org.apache.juneau.marshall.objecttools.ObjectMerger

public class ObjectMerger extends Object
POJO merger.

Useful in cases where you want to define beans with 'default' values.

For example, given the following bean classes...

public interface IA { String getX(); void setX(String x); } public class A implements IA { private String x; public A(String x) { this.x = x; } public String getX() { return x; } public void setX(String x) { this.x = x; } }

The getters will be called in order until the first non-null value is returned...

merge = ObjectMerger.merge(IA.class, new A("1"), new A("2")); assertEquals("1", merge.getX()); merge = ObjectMerger.merge(IA.class, new A(null), new A("2")); assertEquals("2", merge.getX()); merge = ObjectMerger.merge(IA.class, new A(null), new A(null)); assertEquals(null, merge.getX());

Notes:
  • Null POJOs are ignored.
  • Non-getter methods are either invoked on the first POJO or all POJOs depending on the callAllNonGetters flag passed into the constructor.
  • For purposes of this interface, a getter is any method with zero arguments and a non-void return type.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    merge(Class<T> interfaceClass, boolean callAllNonGetters, T... pojos)
    Create a proxy interface on top of zero or more POJOs.
    static <T> T
    merge(Class<T> interfaceClass, T... pojos)
    Create a proxy interface on top of zero or more POJOs.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • merge

      public static <T> T merge(Class<T> interfaceClass, boolean callAllNonGetters, T... pojos)
      Create a proxy interface on top of zero or more POJOs.
      Type Parameters:
      T - The pojo types.
      Parameters:
      interfaceClass - The common interface class.
      callAllNonGetters - If true, when calling a method that's not a getter, the method will be invoked on all POJOs.
      Otherwise, the method will only be called on the first POJO.
      pojos - Zero or more POJOs to merge.
      Can contain nulls.
      Returns:
      A proxy interface over the merged POJOs.
    • merge

      public static <T> T merge(Class<T> interfaceClass, T... pojos)
      Create a proxy interface on top of zero or more POJOs.

      This is a shortcut to calling merge(interfaceClass, false, pojos);

      Type Parameters:
      T - The pojo types.
      Parameters:
      interfaceClass - The common interface class.
      pojos - Zero or more POJOs to merge.
      Can contain nulls.
      Returns:
      A proxy interface over the merged POJOs.