[ 
https://issues.apache.org/jira/browse/IMAGING-159?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14295899#comment-14295899
 ] 

Emmanuel Bourg commented on IMAGING-159:
----------------------------------------

I agree that we need something better than a Map for the parameters, something 
that is typed and self documenting to make the API easier to use.

I experimented with an ImagingParams class that looked like this:

{code:java}
package org.apache.commons.imaging;

import org.apache.commons.imaging.common.IBufferedImageFactory;

public class ImagingParams {
    
    private boolean verbose;
    private boolean strict;
    private String filename;
    private IBufferedImageFactory bufferedImageFactory;
    private PixelDensity pixelDensity;

    public boolean isVerbose() {
        return verbose;
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    /**
     * Indicates whether to throw exceptions when parsing invalid
     * files, or whether to tolerate small problems.
     */
    public boolean isStrict() {
        return strict;
    }

    public void setStrict(boolean strict) {
        this.strict = strict;
    }

    public String getFilename() {
        return filename;
    }

    /**
     * Used to hint the filename when reading from a byte array
     * or InputStream. The filename hint can help disambiguate what file the
     * image format.
     * <p>
     * Applies to read operations.
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }

    public IBufferedImageFactory getBufferedImageFactory() {
        return bufferedImageFactory;
    }

    public void setBufferedImageFactory(IBufferedImageFactory 
bufferedImageFactory) {
        this.bufferedImageFactory = bufferedImageFactory;
    }

    public PixelDensity getPixelDensity() {
        return pixelDensity;
    }

    /**
     * Used in write operations to indicate the desired pixel
     * density (DPI), and/or aspect ratio.
     */
    public void setPixelDensity(PixelDensity pixelDensity) {
        this.pixelDensity = pixelDensity;
    }
}
{code}

The class contains only the common parameters, I wanted to build a class 
hierarchy to specialize the parameters per format, but I haven't figured how to 
modelize that properly.


> There should be a Parameters class
> ----------------------------------
>
>                 Key: IMAGING-159
>                 URL: https://issues.apache.org/jira/browse/IMAGING-159
>             Project: Commons Imaging
>          Issue Type: Improvement
>          Components: imaging.*
>            Reporter: Benedikt Ritter
>             Fix For: Patch Needed
>
>
> Currently options for image I/O are defined as Maps. The leads to the problem 
> that our code has to validate parameter types when they are used:
> {code:java}
> final Object value = params.get(PARAM_KEY_COMPRESSION);
> if (value != null) {
>   if (!(value instanceof Number)) {
>     throw new ImageWriteException(
>       "Invalid compression parameter, must be numeric: "
>          + value);
>   }
>   compression = ((Number) value).intValue();
> }
> {code}
> This can be simplified if we define a Parameters class that provides 
> additional methods like {{public int getInt(String key)}}. The implementation 
> could then look up the value from the map through an exception if it is null 
> or not a number.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to