Polarity.java

package com.varnernet.gerb4j;

/**
 * Polarity for aperture operations in Gerber files. Defined by the Load Polarity (LP) command in
 * the Gerber specification.
 */
public enum Polarity {
    /**
     * Dark polarity - normal exposure.
     */
    DARK("D"),
    /**
     * Clear polarity - reverse/clear exposure.
     */
    CLEAR("C");

    private final String code;

    Polarity(final String code) {
        this.code = code;
    }

    /**
     * Get the Gerber specification code for this polarity value.
     *
     * @return the code (D or C)
     */
    public String getCode() {
        return code;
    }

    /**
     * Parse a polarity value from the Gerber specification code.
     *
     * @param code the Gerber code (D or C)
     * @return the corresponding Polarity enum value
     * @throws IllegalArgumentException if the code is not recognized
     */
    public static Polarity fromCode(final String code) {
        return switch (code) {
            case "D" -> DARK;
            case "C" -> CLEAR;
            default -> throw new IllegalArgumentException("Invalid polarity code: " + code);
        };
    }
}