Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Even before Java8 we had discussions about supporting different interfaces with Closure like Runnable and Callable. These two being easy cases, any framework can define a myriad of interfaces and abstract classes. This then requires to "groovify" the library by writing a helper layer capable of transforming Closure objects into something the library then understand. While it is unlikely of this approach to make Groovy Builder surplus, it can still help with a more simple integration.

Meaning of  "Single Abstract Method"

For a SAM type to be a SAM type according to this GEP an abstract class or interface with a single abstract method is required. Any static methods, as well as non-abstract methods are not counted. The abstract method may be defined in the SAM class itself, but it can also be a parent. The required visibility modifier for the method is public though.

SAM examples:

  • Simple Interface:

 

  • Code Block
    themeConfluence
    languagegroovy
    interface 

...

  • SAM {
      def foo()
    }
  • Interface with defender method (aka virtual extension method):

    Code Block
    themeConfluence
    languagegroovy
    interface SAM {
      def foo()
      def bar() default { 1 }
    }
  • Interface inheriting from another interface:

    Code Block
    themeConfluence
    languagegroovy
    interface ParentOfSAM {}
    interface SAM extends ParentOfSAM {
      def foo()
    }
  • Interface inheriting from another interface, but not defining a method on its own:

    Code Block
    themeConfluence
    languagegroovy
    interface ParentOfSAM {
      def foo()
    }
    interface SAM extends ParentOfSAM {}
  • simple abstract class

    Code Block
    themeConfluence
    languagegroovy
    abstract class SAM {
      abstract foo()
    }
  • abstract class with a abstract and a non abstract

    class

    method:

    Code Block
    themeConfluence
    languagegroovy
    abstract class SAM {
      abstract foo()
      def bar() { 1 }
    }
  • abstract class extending other class:

    Code Block
    themeConfluence
    languagegroovy
    abstract class ParentOfSAM1 {
      abstract foo()
    }
    abstract class SAM1 extends ParentOfSAM1 {}
    class ParentOfSAM {
       def bar() { 1 }
    }
    abstract class SAM2 extends  {
      abstract foo()
    }
  • abstract class implementing interface:

    Code Block
    themeConfluence
    languagegroovy
    interface SomeInterface{
      def foo()
    }
    abstract
    class extending other class but not defining an abstract method on its own:
     class SAM1 implements SomeInterface {}
    abstract class SAM2 implements Runnable{}
    interface AnotherInterface {}
    abstract class SAM3 implements AnotherInterface {
      abstract foo()
    }

Non-SAM examples:

  • empty interface:

    Code Block
    themeConfluence
    languagegroovy
    interface SomeMarker {}
  • interface with two methods:

    Code Block
    themeConfluence
    languagegroovy
    interface TwoMethods {
      def foo()
      def bar()
    }
  • abstract class with two abstract methods:

    Code Block
    themeConfluence
    languagegroovy
    abstract class TwoMethods {
      abstract foo()
      abstract bar()
    }
  • empty abstract class:

    Code Block
    themeConfluence
    languagegroovy
    abstract class Empty {}

Influence on method selection

...