1

Is there a better and more efficient way to do this:

I am inputting 2 values then checking them of on a list, as if current = three then it returns true for checking for one, two and three

NOTE: these values (one, two, three) are just placeholders for the example in my use there is no relation between them except that they have a different priority.

enum Foo {
    ONE(1), TWO(2), THREE(3)

    private final int priority;

    public Foo(int priority) {this.priority = priority;}

    public int getPriority() {return priority;}
}

public boolean checker(Foo current, Foo check) {
    if (check == ONE) {
        if (current == ONE || current == TWO
                || current == THREE) {
            return true;
        }
    }
    if (check == TWO) {
        if (current == TWO || current == THREE) {
            return true;
        }
    }
    if (check == THREE) {
        if (current == THREE) {
            return true;
        }
    }
    return false;
}
Community
  • 1
  • 1
the133448
  • 75
  • 7
  • 1
    Definitely the most efficient way is to use `int` values, and then compare `if( current > check )` – Stewart Dec 25 '14 at 13:32
  • Use a switch statement with cases that fall through into others. I actually wrote an answer but the question was marked duplicate before I could submit... Oh Well... – initramfs Dec 25 '14 at 13:36

3 Answers3

1

I would implement the enum this way:

enum Foo {
ONE(1), TWO(2), THREE(3);

private final int priority;

private Foo(int priority) {
    this.priority = priority;
}

public int getPriority() {
    return priority;
}

public boolean check(Foo check) {
    return this.getPriority() >= check.getPriority();

}

}

And just call:

Foo current = Foo.THREE;
System.out.println(current.check(Foo.TWO));

This way it is more OOP.

panagdu
  • 2,133
  • 1
  • 21
  • 36
0

Looks like you are always checking if current is equal to or greater than check. If so, then you can just use ints. If you need to name them, you can use some constant ints, such as public final static int ONE = 1;.

public boolean checker(int current, int check) {
    return current >= check;
}

If you want to restrict to specific numbers, such as 1, 2, 3 in your example, just check the parameters first:

public boolean checker(int current, int check) {
    int MIN = 1;
    int MAX = 3;
    if(current < MIN || current > MAX || check < MIN || check > MAX) {
        return false;
    }
    return current >= check;
}
forgivenson
  • 4,394
  • 2
  • 19
  • 28
0

demostene's answer is the way to go if your actual use case allows it.

That being said, the following is efficient, clear, and logically equivalent to the code you posted:

public boolean checker(Foo current, Foo check) {
    switch (check) {
        case THREE:
            if (current == TWO)
              return false;
        case TWO:
            if (current == ONE)
                return false;
        default:
            return true;
    }
}
Floegipoky
  • 3,087
  • 1
  • 31
  • 47