package interview;

/** Records, sealed interfaces and pattern matching for switch (Java 21). */
public final class Shapes {
    private Shapes() {}

    public sealed interface Shape permits Circle, Rectangle, Square {}
    public record Circle(double radius) implements Shape {
        public Circle {
            if (radius < 0) throw new IllegalArgumentException("radius < 0"); // compact constructor validation
        }
    }
    public record Rectangle(double width, double height) implements Shape {}
    public record Square(double side) implements Shape {}

    /** Exhaustive switch: no default needed because Shape is sealed. */
    public static double area(Shape shape) {
        return switch (shape) {
            case Circle c -> Math.PI * c.radius() * c.radius();
            case Rectangle(double w, double h) -> w * h;          // record pattern
            case Square s when s.side() == 0 -> 0;                 // guarded pattern
            case Square s -> s.side() * s.side();
        };
    }
}
