Skip to content

Commit c77514b

Browse files
committed
Add ImageFormats
Reuse ArrayUtils
1 parent 49bd1b9 commit c77514b

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

src/main/java/org/apache/commons/imaging/AbstractImageParser.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.commons.imaging.formats.webp.WebPImageParser;
5353
import org.apache.commons.imaging.formats.xbm.XbmImageParser;
5454
import org.apache.commons.imaging.formats.xpm.XpmImageParser;
55+
import org.apache.commons.lang3.ArrayUtils;
5556

5657
/**
5758
* Provides the abstract base class for all image reading and writing utilities. ImageParser implementations are expected to extend this class providing logic
@@ -135,32 +136,23 @@ public final boolean canAcceptExtension(final String fileName) {
135136
if (extensions == null) {
136137
return true;
137138
}
138-
139139
final int index = fileName.lastIndexOf('.');
140140
if (index >= 0) {
141-
final String fileNameExtension = fileName.substring(index + 1).toLowerCase(Locale.ROOT);
142-
for (final String extension : extensions) {
143-
if (extension.equals(fileNameExtension)) {
144-
return true;
145-
}
141+
if (ArrayUtils.contains(extensions, fileName.substring(index + 1).toLowerCase(Locale.ROOT))) {
142+
return true;
146143
}
147144
}
148145
return false;
149146
}
150147

151148
/**
152-
* Indicates whether the ImageParser implementation can accept the specified format
149+
* Tests whether the ImageParser implementation can accept the specified format.
153150
*
154-
* @param type An instance of ImageFormat.
151+
* @param imageFormat An instance of ImageFormat.
155152
* @return If the parser can accept the format, true; otherwise, false.
156153
*/
157-
public boolean canAcceptType(final ImageFormat type) {
158-
for (final ImageFormat format : getAcceptedTypes()) {
159-
if (format.equals(type)) {
160-
return true;
161-
}
162-
}
163-
return false;
154+
public boolean canAcceptType(final ImageFormat imageFormat) {
155+
return ArrayUtils.contains(getAcceptedTypes(), imageFormat);
164156
}
165157

166158
/**

src/main/java/org/apache/commons/imaging/ImageFormats.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@
1616
*/
1717
package org.apache.commons.imaging;
1818

19+
import java.util.Objects;
20+
1921
/**
2022
* Enumerates known image formats.
2123
*/
2224
public enum ImageFormats implements ImageFormat {
2325

2426
// @formatter:off
25-
UNKNOWN(),
27+
UNKNOWN("bin"),
2628
BMP("bmp", "dib"),
2729
DCX("dcx"),
2830
GIF("gif"),
2931
ICNS("icns"),
3032
ICO("ico"),
31-
JBIG2(),
33+
JBIG2("jbig2"),
3234
JPEG("jpg", "jpeg"),
3335
PAM("pam"),
3436
PSD("psd"),
@@ -39,7 +41,7 @@ public enum ImageFormats implements ImageFormat {
3941
PCX("pcx", "pcc"),
4042
PNG("png"),
4143
RGBE("hdr", "pic"),
42-
TGA(),
44+
TGA("tga"),
4345
TIFF("tif", "tiff"),
4446
WBMP("wbmp"),
4547
WEBP("webp"),
@@ -50,12 +52,12 @@ public enum ImageFormats implements ImageFormat {
5052
private final String[] extensions;
5153

5254
ImageFormats(final String... extensions) {
53-
this.extensions = extensions;
55+
this.extensions = Objects.requireNonNull(extensions);
5456
}
5557

5658
@Override
5759
public String getDefaultExtension() {
58-
return this.extensions != null ? this.extensions[0] : null;
60+
return extensions[0];
5961
}
6062

6163
@Override
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.imaging;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.EnumSource;
26+
27+
/**
28+
* Tests {@link ImageFormats}.
29+
*/
30+
public class ImageFormatsTest {
31+
32+
@ParameterizedTest
33+
@EnumSource(ImageFormats.class)
34+
public void testDefaultExtension(final ImageFormats imageFormats) {
35+
assertNotNull(imageFormats.getDefaultExtension());
36+
assertFalse(imageFormats.getDefaultExtension().isEmpty());
37+
}
38+
39+
@ParameterizedTest
40+
@EnumSource(ImageFormats.class)
41+
public void testExtensions(final ImageFormats imageFormats) {
42+
assertNotNull(imageFormats.getExtensions());
43+
assertNotEquals(0, imageFormats.getExtensions().length);
44+
}
45+
46+
@ParameterizedTest
47+
@EnumSource(ImageFormats.class)
48+
public void testName(final ImageFormats imageFormats) {
49+
assertNotNull(imageFormats.getName());
50+
assertFalse(imageFormats.getName().isEmpty());
51+
}
52+
}

0 commit comments

Comments
 (0)