Long list of comparisons in java

I need to compare two objects. If there is a difference, I need to write it according to the specific difference and return true.

For example:

private boolean compTwoObjects(Object objA, Object ObjB) {
   if(objA.getType() != objB.getType()) {
      logTheDifference("getType is differing");
      return true;
   }
                   .
                   .
                   .
   // Now this could invoke other composite methods
   if(checkFont(objA.getFont(), objB.getFont()) {
      logTheDifference("Font is differing");
      return true;
   }
}


private boolean checkFont(Font fontObjA, Font fontObjB) {
   if(fontObjA.getBold() != fontObjB.getBold()) {
      logTheDifference("font bold formatting differs");
      return true;
   }
                   .
                   .
                   .
   if(fontObjA.getAllCaps() != fontObjB.getAllCaps()) {
      logTheDifference("font all caps formatting differs");
      return true;
   }
                   .
                   .
                   .
   if(checkBorderDiff(fontObjA.getBorder(), fontObjB.getBorder())) {
      logTheDifference("border diff");
      return true;
   }
}

private boolean checkBorderDiff(Border borderObjA, Border borderObjB) {
    if (borderObjA.getColor() != null || borderObjB.getColor() != null) {
       if (!borderObjA.getColor().equals(borderObjB.getColor())) {
            logIt("border color differing");
            return true;
        }
    }

    if (borderObjA.getDistanceFromText() != borderObjB.getDistanceFromText()) {
        logIt("distance of the border from text or from the page edge in points differing");
        return true;
    }

    if (borderObjA.isVisible() != borderObjB.isVisible()) {
        logIt("border visibility differing");
        return true;
    }

    if (borderObjA.getLineStyle() != borderObjB.getLineStyle()) {
        logIt("line style differing for border");
        return true;
    }

    if (borderObjA.getLineWidth() != borderObjB.getLineWidth()) {
        logIt("border width in points differing");
        return true;
    }

    if (borderObjA.getShadow() != borderObjB.getShadow()) {
        logIt("border shadow differing");
        return true;
    }
}

//And it is going like this.

My problem is that I want to avoid multiple if statements in methods. I also want to register messages corresponding to a specific difference.

I read several similar problems in stackoverflow that are solved either using the command template or using the HashMap. But they do not include comparisons.

I want to reorganize my code to get rid of the if series.

+4
source share
4 answers

, . , . :

interface IComparer<T> {
    boolean areDifferent (T first, T second);
}

class FontComparer implements IComparer<Font> {
    @Override
    public boolean areDifferent(Font first, Font second) {
        // Compare fonts start
        // ..
        // Compare fonts end
        return new BorderComparer().areDifferent(first.getBorder(), second.getBorder());
    }
}


class BorderComparer implements IComparer<Border> {

    @Override
    public boolean areDifferent(Border first, Border second) {
        //Do border comparison alone
        return false;
    }
}

. , .

:

    Object one = new Object();
    Object two = new Object();
    new ObjectComparer().areDifferent(one, two);
+4

?

private enum FontCmp {

    Bold {
                @Override
                boolean cmp(Font a, Font b) {
                    return a.getBold() != b.getBold();
                }
            },
    AllCaps {
                @Override
                boolean cmp(Font a, Font b) {
                    return a.getAllCaps() != b.getAllCaps();
                }
            },
    Border {
                @Override
                boolean cmp(Font a, Font b) {
                    return BorderCmp.compare(a.getBorder(), b.getBorder());
                }
            };

    // Each enum has one of these.
    abstract boolean cmp(Font a, Font b);

    // Compare them all and log any failures.
    static boolean compare(Font a, Font b) {
        for (FontCmp c : FontCmp.values()) {
            if (c.cmp(a, b)) {
                logIt("FontCmp-" + c + " failed");
                return false;
            }
        }
        return true;
    }
}
+2

, . ,

+1

, , .

, /enum/abstract class, FieldChecker. FieldChecker , - FieldChecker:

 String performCheck(Font a, Font b) {
     if (DO CHECK HERE) {
         return "failure message";
     }
     return null;
 }

Then your validation function will simply become:

for (FieldChecker fc: fieldCheckers) {
    String res = fc.performCheck(a,b);
    if (res != null) {
       return res;
    }
}
return "All ok";
0
source

All Articles