Java simplifying repetitive code involving boolean comparison - java

I am trying to find a way to reduce the length and simplify the following repetitive methods :
boolean circleFlag, squareFlag, diamondFlag;
public void shapeButtonPressed(String shapeType) {
if (shapeType.equals("Circle")) {
circlePressed();
} else if (shapeType.equals("Square")) {
squarePressed();
} else if (shapeType.equals("Diamond")) {
diamondPressed();
}
}
public void circlePressed() {
if(!circleFlag){
//set only circleFlag true and the rest false.
circleFlag = true;
squareFlag = false;
diamondFlag = false;
//(... some code)
} else {
//set all flags false.
circleFlag = false;
diamondFlag = false
squareFlag = false;
//(... some different code)
}
}
public void squarePressed() {
if(!squareFlag){
//set only squareFlag true and the rest false.
squareFlag = true;
circleFlag = false;
diamondFlag = false;
//(... some code)
} else {
//set all flags false.
circleFlag = false;
diamondFlag = false
squareFlag = false;
//(... some different code)
}
}
public void diamondPressed() {
if(!diamondFlag){
//set only diamondFlag true and the rest false.
diamondFlag = true;
squareFlag = false;
circleFlag = false;
//(... some code)
} else {
//set all flags false.
circleFlag = false;
diamondFlag = false
squareFlag = false;
//(... some different code)
}
}
Things I have tried
I have tried to set all my values to Boolean type, set them in a ArrayList<Boolean> and change the shapePressed(String shapeType) method to
public void shapePressed(String shapeType) {
Boolean currFlag = false;
if (shapeType.equals("Circle")) {
currFlag = circleFlag;
} else if (shapeType.equals("Square")) {
currFlag = squareFlag;
} else if (shapeType.equals("Diamond")) {
currFlag = diamondFlag;
}
if (!currFlag){
for (Boolean flag : shapeFlag) flag = ( flag == currFlag ) ? true : false;
//(...)
} else {
for (Boolean flag : shapeFlag) flag = false;
//(...)
}
}
but the line ( flag == currFlag ) compares the booleans as values and not as individual objects. So my currFlag is pointless in this above method.
I then though of using a HashMap<String ,Boolean> but whenever I compare the values given a key (String shapeType from the method parameter), I encounter the same problem as above.
What is a way to simplify this code ?

When a given shape is activated, you just invert that flag. Then the other flags get set to false.
So, trivially, you could simplify your circlePressed() logic to:
public void circlePressed() {
circleFlag = !circleFlag;
squareFlag = false;
diamondFlag = false;
}
Of course there's still a lot of repetition. You could refactor this further to an enum and track the state there.
public enum Flag {
CIRCLE( false ),
SQUARE( false ),
DIAMOND( false ); // default state is false for all
private boolean state;
private Flag(boolean state) {
this.state = state;
}
public void flipState() {
this.state = !this.state;
}
public void setState(boolean state) {
this.state = state;
}
}
// notice this method takes the Flag not a string
public void shapeButtonPressed(Flag selected) {
// iterate through all the flags ...
for( Flag flag : Flag.values() ) {
if (flag == selected) {
// invert the "pressed" flag state
flag.flipState();
} else {
// ... and set the rest to false
flag.setState(false);
}
}
}
The built-in values method on enums returns a list of all of the defined enums, so we can just iterate across them.
It's a bit gimmicky, I admit, since it's not really what enums are intended for, but it simplifies your logic quite a bit.

You could use an enum.
public enum Shape {
CIRCLE, SQUARE, DIAMOND
}
Then, use that in your code like so;
Shape shape;
public void shapeButtonPressed(Shape selectedShape) {
shape = selectedShape;
}
If you can't change the method signature of shapeButtonPressed and it has to take a String, you can stil do
Shape shape;
public void shapeButtonPressed(String shapeType) {
if (shapeType.equals("Circle")) {
shape = Shape.CIRCLE;
} else if (shapeType.equals("Square")) {
shape = Shape.SQUARE;
} else if (shapeType.equals("Diamond")) {
shape = Shape.DIAMOND;
}
}

As an alternative to my approach with enums above (that I highly recommend over this one), you can do a more "C-style" solution using a bitmask instead of boolean flags.
A bitmask is essentially a numeric (or binary for that matter) value of which each bit represents a boolean value.
int shapeFlags;
public void shapeButtonPressed(String shapeType) {
if (shapeType.equals("Circle")) {
shapeFlags = 1;
} else if (shapeType.equals("Square")) {
shapeFlags = 2;
} else if (shapeType.equals("Diamond")) {
shapeFlags = 4;
}
}
This still leaves you the option to set more than one shape to true while being able to override all flags in a single operation.
Mappings from numeric values to shapes would look like this:
0 : no shape
1 : circle
2 : square
3 : circle & square
4 : diamond
5 : diamond & circle
6 : diamond & square
7 : all three

Related

Why does the counter inside if statement not work?

Hello friends I am trying to build a class Car for a project. There are many methods inside the following code as well as an if statement that I am having trouble building, consider the following code
public class Car extends Vehicle {
private boolean isDriving;
private final int horsepower;
private boolean needsMaintenance = false;
private int tripsSinceMaintenance = 0;
Car() {
super();
this.horsepower = 0;
this.isDriving = false;
this.needsMaintenance = false;
this.tripsSinceMaintenance = 0;
}
public int getHorsepower() {
return this.horsepower;
}
public boolean getDrive() {
return this.isDriving;
}
public boolean getMain() {
return this.needsMaintenance;
}
public int getTRIP() {
return this.tripsSinceMaintenance;
}
public void drive() {
this.isDriving = true;
}
public void stop() {
this.isDriving = false;
}
public void repair() {
this.needsMaintenance = false;
this.tripsSinceMaintenance = 0;
}
/**
* #param args
*/
public static void main(String[] args) {
Car auto = new Car();
auto.drive();
auto.stop();
if (auto.isDriving == true) {
if (auto.isDriving == false)
auto.tripsSinceMaintenance = auto.tripsSinceMaintenance + 1;
}
if (auto.tripsSinceMaintenance > 100)
auto.needsMaintenance = true;
System.out.println("Drive: " + auto.getDrive());
System.out.println("trip: " + auto.getTRIP());
}
}
What I want to do is whenever the attribute isDriving goes from true to false the tripsSinceMaintenance should increase by 1 and also when tripsSinceMaintenanceis greater than 100,needsMaintenanceshould becometrue`.
here I expected trips to be 1 but the result is the following:
Drive: false
trip: 0
I have tried this.isDriving==true; and basicaly wherever auto is inside the if statement I put this but the following error appears
non static variable cannot be referenced from static context
help me please!
What i want to do is whenever the attribute isDriving goes from true to false the tripsSinceMaintenance should increase by 1 and also when tripsSinceMaintenance is greater than 100 needsMaintenance should become true
Do this inside stop() method
fun stop() {
if (isDriving) {
tripsSinceMaintenance++;
}
if (tripsSinceMaintenance > 100) {
needsMaintenance = true;
}
isDriving = false;
}
You don't need to put == true inside of an if statement, it's doing that already,
if(someCondition) { // <-- this executes if the condition is true.
Also, you have conflicting conditions nested, meaning...
if (thisIsTrue) {
if (!thisIsTrue) {
// <--- unreachable statements
where you should be incrementing your variable is where you're setting "isDriving = true"
So your code would look like this:
public void drive() {
this.isDriving=true;
auto.tripsSinceMaintenance++;
}

Why is this boolean coming out false even when one of the conditions is true?

So I have this simple boolean method for checking whether my game has ended. In the main method, I then have a big loop that starts with the statement while (isGameEnd() == false) {...}. This loop never seems to break, even when one, two or all of the conditions in the method becomes true. I don't get it.
public static boolean isGameEnd() {
if (lives == 0 || steps == 0 || isMazeCompleted()) { return true; }
else { return false; }
}
The use of static in the function definition is a red flag to me. If a class is defined with default field values, then those default values will be what is checked rather than the particular implementation of the class:
class Game {
int lives = 3;
int steps = 10;
public boolean isMazeCompleted() {
return false;
}
public void doStuff() {
lives--;
}
public static boolean isGameEnd() {
if (lives == 0 || steps == 0 || isMazeCompleted()) {
return true;
} else {
return false;
}
}
}
public static void main(String[] args){
Game a;
while(!a.isGameEnd()){ // check 'isGameEnd' for the static class
a.doStuff(); // This does *not* update the static class
}
}
Most Java editors will complain about the use of static functions in a non-static context, so will suggest Game.isGameEnd() instead of a.isGameEnd(), which makes it a bit more obvious to the programmer where the error is.

my deterministic turing machine won't work, because my equals and indexof method throw no source error

I have the problem, that my equals method doesnt work as i want it to. I want to implement a deterministic turing machine, so I want to add the method findCommand(), which searchs through a arraylist of commands. So I decided to create a searchDummy to find all Transitions that are available for the Configuration I have.
Class States:
public class States {
private int stateId;
private boolean rejState;
private boolean accState;
private boolean stopState;
private List<Commands> commands = new ArrayList<Commands>();
equals in class States:
#Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof States) {
States otherState = (States) other;
return (stateId == otherState.stateId);
} else {
return false;
}
}
hashCode:
#Override public int hashCode() {
StringBuilder b = new StringBuilder(stateId);
return b.toString().hashCode();
}
this is the findCommand method in States:
public Commands findCommand(States state, char inputTapeChar,
char[] tapeChars) {
Commands searchDummy = new Commands(state, inputTapeChar, tapeChars,
null, null, null, null);
int pos = commands.indexOf(searchDummy);
return pos >= 0 ? commands.get(pos) : null;
}
commands is my arraylist, so I want to find the searchDummy with indexOf().
I have the class Commands, which holds the attribute Configuration configuration, the class Configuration, which holds the attributes of a Configuration and the attribute Transition transition and the class transition that holds the attributes for itself.
Class Commands:
public class Commands implements Comparable<Commands> {
private Configuration configuration;
Class Configuration:
public class Configuration {
private Transition transition;
private States state;
private char inputTapeChar;
private char[] tapeChars;
Class Transition:
public class Transition {
private States targetState;
private Direction inputTapeHeadMove;
private char[] newTapeChars;
private Direction[] tapeHeadMoves;
i have this equals method in Commands:
#Override public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Commands) {
Commands otherCmd = (Commands) other;
return (configuration.equals(otherCmd.configuration));
} else {
return false;
}
}
and this hashcode
#Override
public int hashCode() {
StringBuilder b = new StringBuilder(configuration.getState() + ","
+ configuration.getInputTapeChar());
for (char c : configuration.getTapeChars()) {
b.append("," + c);
}
return b.toString().hashCode();
}
then almost the same in Configuration:
#Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Configuration) {
Configuration otherConfi = (Configuration) other;
return (state.equals(otherConfi.state))
&& (inputTapeChar == otherConfi.inputTapeChar)
&& (Arrays.equals(tapeChars, otherConfi.tapeChars));
} else {
return false;
}
}
hashcode:
#Override
public int hashCode() {
StringBuilder b = new StringBuilder(state + "," + inputTapeChar);
for (char c : tapeChars) {
b.append("," + c);
}
return b.toString().hashCode();
}
equales in class State:
#Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof States) {
States otherState = (States) other;
return (stateId == otherState.stateId);
} else {
return false;
}
}
so my question:
when I debug this it goes through until it's finished with the checks but when it should return the value it stucks at Configuration.equals(...) and shows the error no source found!
what is the problem? Are the hashcodes wrong? Or are the equals wrong?
I never used equals before so I dont know when i need to use it or how i need to fix this. thanks for your help.
Your hashCode implementation looks suspect - all that String stuff is not standard.
For example for your Transition class should be something like this:
#Override
public int hashCode() {
int result = 17;
result = 31 * result + targetState.hashCode();
result = 31 * result + inputTapeHeadMove.hashCode();
result = 31 * result + newTapeChars.hashCode();
result = 31 * tapeHeadMoves.hashCode();
return result;
}
Most IDEs will offer autogen of hashCode and equals methods.

Java boolean returning true when declared false

I am currently making a plugin from the 1.8 Bukkit API. This question however, has to do with booleans. From the beginning of my class file, I have this declaration of a boolean
public static boolean lockchat = false;
Then I have another boolean in the class file that is used for Bukkit commands:
public boolean onCommand(CommandSender s, Command cmd, String label, String[] args)
This boolean returns true at the end, which I think is making the lockchat boolean return true also. If I return false, I am pretty sure that the command code will not return to the user.
My problem is that in this part of my code:
if(lockchat == true)
{
s.sendMessage("unlocked.")
lockchat = false;
}
else
{
s.sendMessage("locked.");
lockchat = true;
}
The declaration at the beginning does not seem to matter here, because this always sends me the message unlocked.
I have tried to put the declaration inside of the second boolean, but it throws me errors and warnings.
Since the second boolean is returning true, I think that the lockchat boolean is returning too. If I would change it to return false, lockchat would probably return false also, resulting in another problem.
I want to find a way to have the boolean declaration stay false, while having it changed to true/false inside of the second boolean, as shown. How would I do this?
NOTE: This variable is not used anywhere else in my code.
EDIT: I don't think this will make a difference, but I am testing for the label string to be "lockchat", the same as the boolean name. This probably wouldn't change anything, but just giving more information.
FULL CLASS FILE CODE:
package dev.td6.duocraft.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import dev.td6.duocraft.main.Duocraft;
public class DCCommandLockChat implements CommandExecutor, Listener
{
Duocraft plugin;
public DCCommandLockChat(Duocraft instance)
{
plugin = instance;
}
public String colorize(String msg)
{
String coloredMsg = "";
for(int i = 0; i < msg.length(); i++)
{
if(msg.charAt(i) == '&')
coloredMsg += 'ยง';
else
coloredMsg += msg.charAt(i);
}
return coloredMsg;
}
public static boolean lockchat = false;
#SuppressWarnings("deprecation")
public boolean onCommand(CommandSender s, Command cmd, String label, String[] args)
{
if(s instanceof Player)
{
Player p = (Player) s;
if(label.equalsIgnoreCase("lockchat"))
{
if(p.hasPermission("duocraft.lockchat"))
{
if(args.length >= 1)
{
if(args.length >= 2)
{
s.sendMessage("Too many arguments. </lockchat [time]>");
}
else
{
if(lockchat == true)
{
int time = Integer.valueOf(args[0]);
s.sendMessage("locked");
lockchat = true;
plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
{
#Override
public void run()
{
Bukkit.broadcastMessage("unlocked.");
lockchat = false;
plugin.getServer().getScheduler().cancelTasks(plugin);
}
}
, time*20, time*20);
}
else
{
int time = Integer.valueOf(args[0]);
s.sendMessage("locked.");
lockchat = true;
plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
{
#Override
public void run()
{
Bukkit.broadcastMessage("unlocked.");
lockchat = false;
plugin.getServer().getScheduler().cancelTasks(plugin);
}
}
, time*20, time*20);
}
}
}
else
{
if(lockchat == true)
{
s.sendMessage("unlocked");
lockchat = false;
}
else
{
s.sendMessage("unlocked");
lockchat = true;
}
}
}
else
{
p.sendMessage("no access");
}
}
}
else
{
if(label.equalsIgnoreCase("lockchat"))
{
if(args.length >= 1)
{
if(args.length >= 2)
{
s.sendMessage("Too many args. </lockchat [time]>");
}
else
{
if(lockchat == true)
{
int time = Integer.valueOf(args[0]);
s.sendMessage("locked.");
lockchat = true;
plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
{
#Override
public void run()
{
Bukkit.broadcastMessage("unlocked.");
lockchat = false;
plugin.getServer().getScheduler().cancelTasks(plugin);
}
}
, time*20, time*20);
}
else
{
int time = Integer.valueOf(args[0]);
s.sendMessage("locked");
lockchat = true;
plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
{
#Override
public void run()
{
Bukkit.broadcastMessage("unlocked");
lockchat = false;
plugin.getServer().getScheduler().cancelTasks(plugin);
}
}
, time*20, time*20);
}
}
}
else
{
if(lockchat == true)
{
s.sendMessage("unlocked");
lockchat = false;
}
else
{
s.sendMessage("unlocked");
lockchat = true;
}
}
}
}
return true;
}
#EventHandler
public void chatLocked(AsyncPlayerChatEvent e)
{
if(lockchat==false)return;
Player p = e.getPlayer();
if(p.hasPermission("duocraft.lockchat.bypass"))return;
p.sendMessage("chat is locked.");
e.setCancelled(true);
}
}
EDIT: Also public static boolean lockchat = false; Is not being modified in any way by any other class files.
EDIT: I am using Java 7 for this.
Just so you know, in your full source you use the following code:
if(lockchat == true)
{
s.sendMessage("unlocked");
lockchat = false;
}
else
{
s.sendMessage("unlocked");
lockchat = true;
}
more specifically, you are sending "unlocked" no matter which path the code follows.
Edit: I reformatted your code to reduce some of the duplication. This version fails fast if the CommandSender is a player without permission or the label is not "lockchat". I inferred that the intention is that executing "/lockchat" without an argument should toggle locking immediately, while executing it with an argument should make it toggle for the specified number of seconds and then toggle back. The code below should do this (at least as far as ensuring lockchat always has the intended value, but I haven't tested it.
Also, I don't know if the Runnable will be called on a different thread, but if it is you should synchronize all accesses to the shared lockchat variable. At the very least, making it volatile (as I do below) may prevent some confusion amongst the threads as to its actual value.
public static volatile boolean lockchat = false;
public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
// If this is not the 'lockchat' command, then fail fast
if (!label.equalsIgnoreCase("lockchat")) return true;
// If s is a Player then check the player has permission and fail fast
// if not.
if (s instanceof Player) {
Player p = (Player) s;
if (!p.hasPermission("duocraft.lockchat")) {
p.sendMessage("no access");
return true;
}
}
switch (args.length) {
case 0:
lockchat = !lockchat;
s.sendMessage(lockchatStatus());
break;
case 1:
int ticks = Integer.valueOf(args[0]) * 20;
final boolean originalLockChat = lockchat;
lockchat = !originalLockChat;
s.sendMessage(lockchatStatus());
plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
#Override
public void run() {
lockchat = originalLockChat;
Bukkit.broadcastMessage(lockchatStatus() + ".");
plugin.getServer().getScheduler().cancelTasks(plugin);
}
}, ticks, ticks);
break;
default:
s.sendMessage("Too many arguments. </lockchat [time]>");
break;
}
return true;
}
private String lockchatStatus() {
return lockchat ? "locked" : "unlocked";
}
As stated in Julian's answer, your code is returning unlocked no matter what the value :
if(lockchat == true)
{
s.sendMessage("unlocked");
lockchat = false;
}
else
{
s.sendMessage("unlocked");
lockchat = true;
}
As per your comment regarding as to why it is not blocking the chat, are you sure you registered your listener? To register your listener, put this line in your onEnable() method in your Main class :
getServer().getPluginManager().registerEvents(new DCCommandLockChat(), this);
Where DCCommandLockChat() is your Listener class and 'this' is your class that extends JavaPlugin.
What this basically does is register your listener for your plugin because otherwise, the server wouldn't pass any events to your listener and so your listener wouldn't know what would be happening on the server.
Also, as for the method itself returning true or false, both values will still run the command. As far as I know, the only time the return value of the onCommand method matters is when you're using aliases in your plugin.yml. If the method returns false, then the server will send the player a message with the aliases. Aside from that, it doesn't really matter.
You should give the boolean (lockchat) the value you want in the constructor of your class.
To answer your question:
I want to find a way to have the boolean declaration stay false, while having it changed to true/false inside of the second boolean, as shown. How would I do this?
At the beginning of the body of whichever statement you want, make a temporary variable to store lockChat's value.
boolean lockChatTemp = lockChat;
Then, use and modify that value within your function. This way, lockChat will keep its value throughout.
Also,
if (lockChat == true)
can be replaced with
if (lockChat) since the statement inside the parentheses evaluates to a boolean, and lockChat is already a boolean.

Long list of if comparisons in java

I need to compare two Objects. If there is a difference I need to log it corresponding to particular difference and return the 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 I want to avoid multiple if statements in the methods. Also I want to log the messages corresponding to particular difference.
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.
I want to refactor my code to get rid of series of if's.
Have a system of comparators, backed by generics. Every comparer will also know what is next in line. For example:
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;
}
}
You could setup a comparer chain now, and bail out when comparison fails. Otherwise, comparison goes to the comparer next in the chain.
The client code will finally look like:
Object one = new Object();
Object two = new Object();
new ObjectComparer().areDifferent(one, two);
Have you considered enums?
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;
}
}
You could also use reflection as described here. Also look into introspection as described here
Fundamentally you are trying to do a series of comparisons, so there is little choice but to do a series of comparisons.
What you could do is define an interface/enum/abstract class which is a FieldChecker. That FieldChecker would have an abstract method implemented differently in each FieldChecker:
String performCheck(Font a, Font b) {
if (DO CHECK HERE) {
return "failure message";
}
return null;
}
Then your check function just becomes:
for (FieldChecker fc: fieldCheckers) {
String res = fc.performCheck(a,b);
if (res != null) {
return res;
}
}
return "All ok";

Categories