From 3399551cec01888f15fc7ea449695471212d90b3 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 20 Jan 2024 19:23:55 +0100 Subject: [PATCH] Add ImageEncoding enum --- .../org/pgpainless/algorithm/ImageEncoding.kt | 25 +++++++++++++++++++ .../pgpainless/algorithm/ImageEncodingTest.kt | 24 ++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 pgpainless-core/src/main/kotlin/org/pgpainless/algorithm/ImageEncoding.kt create mode 100644 pgpainless-core/src/test/kotlin/org/pgpainless/algorithm/ImageEncodingTest.kt diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/algorithm/ImageEncoding.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/algorithm/ImageEncoding.kt new file mode 100644 index 00000000..dbef9f35 --- /dev/null +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/algorithm/ImageEncoding.kt @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm + +/** + * Image encoding schemes for user attribute image headers. + * Currently, only [JPEG] is defined. + */ +enum class ImageEncoding(val id: Int) { + + /** JPEG File Interchange Format (JFIF). */ + JPEG(1) + ; + + companion object { + @JvmStatic + fun requireFromId(id: Int): ImageEncoding = + fromId(id) ?: throw NoSuchElementException("No ImageEncoding found for id $id") + + @JvmStatic + fun fromId(id: Int): ImageEncoding? = values().firstOrNull { id == it.id } + } +} diff --git a/pgpainless-core/src/test/kotlin/org/pgpainless/algorithm/ImageEncodingTest.kt b/pgpainless-core/src/test/kotlin/org/pgpainless/algorithm/ImageEncodingTest.kt new file mode 100644 index 00000000..a5352a77 --- /dev/null +++ b/pgpainless-core/src/test/kotlin/org/pgpainless/algorithm/ImageEncodingTest.kt @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2024 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class ImageEncodingTest { + + @Test + fun parseJpeg() { + assertEquals(ImageEncoding.JPEG, ImageEncoding.requireFromId(1)) + } + + @Test + fun parseUnknown() { + assertNull(ImageEncoding.fromId(11)) + assertThrows { ImageEncoding.requireFromId(11) } + } +}