Note that there are some explanatory texts on larger screens.

plurals
  1. POLong list of if comparisons in java
    primarykey
    data
    text
    <p>I need to compare two Objects. If there is a difference I need to log it corresponding to particular difference and return the true.</p> <p>For example:</p> <pre><code>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. </code></pre> <p>My problem is I want to avoid multiple if statements in the methods. Also I want to log the messages corresponding to particular difference.</p> <p>I have read few similar type of problems on stackoverflow solved either by command pattern or HashMap. But they don't include comparisons in that.</p> <p>I want to refactor my code to get rid of series of if's. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload