kinow commented on a change in pull request #116:
URL: https://github.com/apache/commons-imaging/pull/116#discussion_r760907698



##########
File path: src/main/java/org/apache/commons/imaging/ImageParser.java
##########
@@ -898,7 +892,7 @@ public boolean canAcceptType(final ImageFormat type) {
      * @param file An valid file reference.
      * @return If the parser can accept the format, true; otherwise, false.
      */
-    protected final boolean canAcceptExtension(final File file) {
+    public boolean canAcceptExtension(final File file) {

Review comment:
       Made it public because I created methods in the new internal `Util` 
class. Similar to the already public `canAcceptType(ImageFormat)` of this class.

##########
File path: src/main/java/org/apache/commons/imaging/ImageParser.java
##########
@@ -123,12 +115,18 @@
                 new TiffImageParser(),
                 new WbmpImageParser(),
                 new XbmImageParser(),
-                new XpmImageParser(),
+                new XpmImageParser()
                 // new JBig2ImageParser(),
                 // new TgaImageParser(),
-        };
+        );
     }
 
+    /**
+     * Get a default parameters instance for this parser.
+     * @return default parameters instance
+     */
+    public abstract T getDefaultParameters();

Review comment:
       Copied from @darkma773r 's PR. By moving it here from the `ImageFormat` 
(i.e. responsible for defining the default parameter is the parser, not the 
format) several generics warnings don't happen anymore.

##########
File path: src/main/java/org/apache/commons/imaging/internal/Util.java
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.internal;
+
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImagingParameters;
+
+/**
+ * Internal utilities.
+ *
+ * @since 1.0-alpha3
+ */
+public class Util {
+
+    private Util() {}
+
+    public static <T extends ImagingParameters> ImageParser<T> 
getImageParser(ImageFormat format) {
+        return getImageParser((parser) -> parser.canAcceptType(format), () -> 
new RuntimeException("Unknown Format: " + format));
+    }
+
+    public static <T extends ImagingParameters> ImageParser<T> 
getImageParser(String fileExtension) {
+        return getImageParser((parser) -> 
parser.canAcceptExtension(fileExtension), () -> new RuntimeException("Unknown 
Extension: " + fileExtension));
+    }
+
+    // This generics suppression is as good as the predicate given. If the 
predicate violates a generics design,
+    // then there will be an error during runtime.
+    @SuppressWarnings("unchecked")
+    private static <T extends ImagingParameters> ImageParser<T> 
getImageParser(Predicate<ImageParser<?>> pred, Supplier<? extends 
RuntimeException> supl) {
+        return (ImageParser<T>) ImageParser
+                .getAllImageParsers()
+                .stream()
+                .filter((parser) -> pred.test(parser))
+                .findFirst()

Review comment:
       This is the only place where we have an issue with the generics used in 
this PR now. The `Predicate` given to this function should return `true` only 
if the parser matches something like a file format, file extension, etc.
   
   Assuming the predicate is correct, there should be no runtime errors for the 
parameters :+1: that's the best we can achieve with Java generics IMO

##########
File path: src/main/java/org/apache/commons/imaging/Imaging.java
##########
@@ -16,9 +16,6 @@
  */
 package org.apache.commons.imaging;
 
-import static org.apache.commons.imaging.ImagingConstants.PARAM_KEY_FILENAME;
-import static org.apache.commons.imaging.ImagingConstants.PARAM_KEY_FORMAT;

Review comment:
       N.B. no generics warnings being suppressed in `Imaging` now :tada: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to