Adaptable Interface

[vc_row][vc_column width=”2/3″][vc_column_text el_class=”documentation”]Java objects implementing the org.apache.sling.api.adapter.Adaptable interface provide a way to be adapted to an object that implements a different interface. The Adaptable interface can provide the adapted object itself within its adaptTo() method. It is highly suggested that implementations of the Adaptable interface use the org.apache.sling.api.adapter.AdapterManager service to provide an adapted object if a match is not found.

The easiest way to do this is to extend the org.apache.sling.adapter.SlingAdaptable abstract class and call the super.adaptTo method if a suitable, adapted, object was not found. See SlingAdaptable for examples.

Methods


adaptTo()

<AdapterType> AdapterType adaptTo(Class<AdapterType> type);
Parameters

type: The Class object to adapt to

Result

An object that is an instance of the class specified in the type parameter. If not able to adapt to the requested type, null is returned.

Note: AdapterType is not an interface or class. It is a type parameter for the generic type, such as the conventional symbols, E, K, N, T, V.

Example

public class MyAdaptable implements Adaptable {

private AdapterManager adapterManager;

     private Node node;

     public MyAdaptable(Node node, AdapterManager adapterManager) {
          this.adapterManager = adapterManager;
          this.node = node;
     }
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
          if (type == Node.class) {
               return this.node;
          }
          return adapterManager.getAdapter(this, type);
     }
}

See Also

SlingAdaptable

Generic Types

Adapter JavaDoc

GitHub: org.apache.sling.api.adapter Package Trunk -[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_widget_sidebar sidebar_id=”woocommerce”][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]Previous: Adapters: Apache Sling API Bundle[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

Next: AdapterManager Interface

[/vc_column_text][/vc_column][/vc_row]

No Comments on "Adaptable Interface"

Leave a Comment

Your email address will not be published. Required fields are marked *