You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/09/14 03:15:11 UTC

[GitHub] [commons-imaging] gwlucastrig commented on pull request #116: [IMAGING-159] Add ImagingParameters interface and BaseParameters (POJO)

gwlucastrig commented on pull request #116:
URL: https://github.com/apache/commons-imaging/pull/116#issuecomment-918761710


   I saw the note you posted on the Commons mailing list about the design of this.  I'm wondering if just a simple brute-force approach might do the trick.  So I will throw out a wild idea (which I'd be the first to admit I have not thought through thoroughly).
   
   We have a number of issues:
   
   1. Handle cases where applications pass in null imaging parameters (just use defaults)
   2. Handle cases where applications pass in an instance of the generic ImagingParameters base class.
   3. Handle cases where applications pass in an instance of the subject-matter imaging parameters (i.e. TiffImagingParameters, PngImagingParameters, etc.).
   4. Handle cases where applications pass in the wrong thing.
   
   ```
       TestStuff testStuff = new TestStuff();   // works on TIFFs (for example)
       testStuff.aTypicalMethod(null);
       testStuff.aTypicalMethod(imgParams);
       testStuff.aTypicalMethod(tiffParams);
       testStuff.aTypicalMethod(pngParams);  // throws exception
   ```
   
   And we'd like to make the contents of "aTypicalMethod" be pretty compact.  In this case, using TiffImagingParameters as an example, what if TiffImagingParameters (and all its brothers and sisters) had a static method called resolveParameters(ImagingParameters) that worked like:
   
   ```
     void aTypicalMethod(ImagingParameters imagingParameters) {
       TiffImagingParameters params  = TiffImagingParameters.resolveParameters(imagingParameters);
        // now do some work using the parameters
     }
   ```
   
   **How we make this work**
   
   The base class would define two constructors. A no-argument constructor and a "copy constructor".
   ```
     public ImagingParameters(){
      }
      
     public ImagingParameters(ImagingParameters source) {
       this.bufferedImageFactory = source.bufferedImageFactory;
       this.fileName = source.fileName;
       this.pixelDensity = source.pixelDensity;
       this.strict = source.strict;
     }
   ```
   
   Subject-matter specific parameters classes would implement compatible constructors:
   ```
     public TiffImagingParameters(){
     }
     public TiffImagingParameters(ImagingParameters imagingParameters){
       super(imagingParameters);
     }
   ```
   
   Finally, each parameters class would implement a static method:
   
   ```
     static public TiffImagingParameters resolveParameters(ImagingParameters imagingParameters) {
       TiffImagingParameters params;
       if (imagingParameters == null) {
         return new TiffImagingParameters();
       } else if (imagingParameters instanceof TiffImagingParameters) {
         return  (TiffImagingParameters) imagingParameters;
       } else if (ImagingParameters.class.equals(imagingParameters.getClass())) {
         // the specified class is the generic, base class.
         // so just copy it's content into a new TiffImagingParameters instance
        return new TiffImagingParameters(imagingParameters);
       } else {
         // In this case, the calling application gave us an instance of an
         // incompatible parameters class.  
         throw new IllegalArgumentException(
           "Invalid imaging parameters class " 
             + imagingParameters.getClass().getSimpleName());
       }
     }
   ```
    
   
   


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org