Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support sealed classes #478

Merged
merged 2 commits into from
Jun 13, 2021

Conversation

clementdessoude
Copy link
Contributor

What changed with this PR:

Support sealed classes and interfaces

Example

// Input
public sealed class Rectangle
    implements Shape
    permits Square {

    private final double length;
    private final double height;

    public Rectangle(double length, double height) {
        this.length = length;
        this.height = height;
    }

    @Override
    public double area() {
        return length * height;
    }

}

public non-sealed class RightTriangle implements Triangle {

    private final double adjacent;
    private final double opposite;

    public RightTriangle(double adjacent, double opposite) {
        this.adjacent = adjacent;
        this.opposite = opposite;
    }

    @Override
    public double area() {
        interface People { String name(); }
        record Person(String name) implements People { }
        record Persons(String... names) { }

        People p = new Person("John Doe");

        return adjacent * opposite / 2;
    }

}

public sealed interface Shape
    permits Circle, Rectangle, Triangle, Unicorn {

    double area();

    default Shape rotate(double angle) {
        return this;
    }

    default String areaMessage() {
        if (this instanceof Circle)
            return "Circle: " + area();
        else if (this instanceof Rectangle)
            return "Rectangle: " + area();
        else if (this instanceof RightTriangle)
            return "Triangle: " + area();
        // :(
        throw new IllegalArgumentException();
    }

}

public non-sealed interface Triangle extends Shape {

}

public sealed interface Shape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}
public sealed interface Shape extends AbstractShape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}
public sealed class Shape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}
public sealed class Shape extends AbstractShape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}

// Output
public sealed class Rectangle implements Shape permits Square {

  private final double length;
  private final double height;

  public Rectangle(double length, double height) {
    this.length = length;
    this.height = height;
  }

  @Override
  public double area() {
    return length * height;
  }
}

public non-sealed class RightTriangle implements Triangle {

  private final double adjacent;
  private final double opposite;

  public RightTriangle(double adjacent, double opposite) {
    this.adjacent = adjacent;
    this.opposite = opposite;
  }

  @Override
  public double area() {
    interface People {
      String name();
    }
    record Person(String name) implements People {}
    record Persons(String... names) {}

    People p = new Person("John Doe");

    return adjacent * opposite / 2;
  }
}

public sealed interface Shape permits Circle, Rectangle, Triangle, Unicorn {
  double area();

  default Shape rotate(double angle) {
    return this;
  }

  default String areaMessage() {
    if (this instanceof Circle) return "Circle: " + area(); else if (
      this instanceof Rectangle
    ) return "Rectangle: " + area(); else if (
      this instanceof RightTriangle
    ) return "Triangle: " + area();
    // :(
    throw new IllegalArgumentException();
  }
}

public non-sealed interface Triangle extends Shape {}

public sealed interface Shape
  permits
    ALongVeryLongCircle,
    ALongVeryLongRectangle,
    ALongVeryLongTriangle,
    ALongVeryLongUnicorn {}

public sealed interface Shape
  extends AbstractShape
  permits
    ALongVeryLongCircle,
    ALongVeryLongRectangle,
    ALongVeryLongTriangle,
    ALongVeryLongUnicorn {}

public sealed class Shape
  permits
    ALongVeryLongCircle,
    ALongVeryLongRectangle,
    ALongVeryLongTriangle,
    ALongVeryLongUnicorn {}

public sealed class Shape
  extends AbstractShape
  permits
    ALongVeryLongCircle,
    ALongVeryLongRectangle,
    ALongVeryLongTriangle,
    ALongVeryLongUnicorn {}

Relative issues or prs:

Should fix #435

Unverified

This user has not yet uploaded their public signing key.
@clementdessoude clementdessoude force-pushed the docs/update-references branch 2 times, most recently from 6184f23 to 7993e69 Compare June 13, 2021 11:15
@clementdessoude clementdessoude force-pushed the docs/update-references branch from 7993e69 to f6ef1a4 Compare June 13, 2021 11:23
@clementdessoude clementdessoude merged commit 0daf032 into jhipster:main Jun 13, 2021
@clementdessoude clementdessoude deleted the docs/update-references branch June 13, 2021 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support preview feature: sealed class
1 participant