Newer
Older
gradle-lectures / buildSrc / src / main / java / nz / stanger / lecture / ImageFormat.java
package nz.stanger.lecture;

/**
 * Image formats generated by the image generation tasks.
 */
public enum ImageFormat {
    EPS("application/postscript"),
    JPEG("image/jpeg"),
    JPG("image/jpeg"),
    PDF("application/pdf"),
    PNG("image/png"),
    SVG("image/svg+xml");

    /**
     * The MIME type of this image format.
     */
    private final String mimeType;

    /**
     * Constructs an image format.
     *
     * @param mimeType the MIME type of this image format
     */
    ImageFormat(String mimeType) {
        this.mimeType = mimeType;
    }

    /**
     * Returns the MIME type of this image format.
     *
     * @return the MIME type
     */
    public String getMimeType() {
        return mimeType;
    }

    /**
     * Returns a file suffix corresponding to this image format, suitable for
     * appending to a file name.
     *
     * @return the file suffix prefixed with a ".", for example {@code .svg}
     */
    public String getFileSuffix() {
        String suffix = "";
        switch (this) {
            case EPS ->
                suffix = ".eps";
            case JPEG, JPG ->
                suffix = ".jpg";
            case PDF ->
                suffix = ".pdf";
            case PNG ->
                suffix = ".png";
            case SVG ->
                suffix = ".svg";
            default ->
                throw new IllegalArgumentException("unknown image type: " + this.name());
        }
        return suffix;
    }
}