Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010 the original author or authors.
   * 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
  * 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 org.callbackparams;

Instead of implementing a certain callback-interface it is also possible for a callback-value to provide a callback-interface implementation by implementing this interface and have the method getCallback() return an arbitrary object, which will work as a "backup" callback if it implements the callback-interface in case the original callback-value does not. This code-example shows how it works ...
 interface Foo {
   void printFooOutput();

 interface Bar {
   void printBarOutput();

 public class MyTest {

   public test(Foo f, Bar b) {
     f.printFooOutput();                                                 //(1)
     b.printBarOutput();                                                 //(2)

   enum MyEnum implements Foo, CallbackFactory {
     SUPPLIES_BAR(new Bar() {
       public void printBarOutput() {
         System.out.println("Output from supplied bar");                 //(3)
     SUPPLIES_FOO(new Foo() {
       public void printFooOutput() {
         System.out.println("Output from supplied foo is shielded ..."); //(4)

     Object supplied;

     MyEnum(Object supplied) {
       this.supplied = supplied;

     public void printFooOutput() {
       System.out.println("Output from enum-constant " + this            //(5)
           + " that implements interface Foo");

     public Object getCallback() {
       return this.supplied;
The test-class will produce three test-runs ...
Output from "test[SUPPLIES_NONE]":
 Output from enum-constant SUPPLIES_NONE that implements interface Foo
Output from "test[SUPPLIES_BAR]":
 Output from enum-constant SUPPLIES_BAR that implements interface Foo
 Output from supplied bar
Output from "test[SUPPLIES_FOO]":
 Output from enum-constant SUPPLIES_FOO that implements interface Foo
The code f.printFooOutput() (1) consistently produces output through the statement at (5). On the other hand, b.printBarOutput (2) only produces output when the callback-record contains MyEnum-constant SUPPLIES_BAR, which, unlike the other MyEnum-constants, offers a supplied instance that happens to implement callback-interface Bar.
Please also note that f.printFooOutput() (1) does not produce any output from statement (4) when SUPPLIES_FOO is in the the callback-record. This is because SUPPLIES_FOO itself implements Foo and therefore its supplied callback will never be considered when a Foo-method is invoked.

Henrik Kaipe
public interface CallbackFactory {

Returns the supplied callback
    public Object getCallback();
New to GrepCode? Check out our FAQ X