Alias for expressions in java

Viewed 184

I have a if condition in java that reoccurs at many places in the code. I want to avoid writing the whole condition again and again. In C, I could have done this with #define

#define cond ((i==2) && (j==5) && (k==8))

int main() {
    if(cond)
}

How can I achieve the same in java? I can probably create another method that evaluates this condition -

  main() {
        if(cond())
    }

cond() {
    return (i==2) && (j==5) && (k==8);
}

but I wanted to know if I can avoid creating another function.


UPDATE -

I realized I should add more details/edit to support my argument. Lets say I have 2 conditions and I want to check both-

 #define cond258 ((i==2) && (j==5) && (k==8))
 #define cond369 ((i==3) && (j==6) && (k==9))

I can create 2 functions -

cond258(i, j, k) {
        return (i==2) && (j==5) && (k==8);
    }
cond369(i, j, k) {
        return (i==3) && (j==6) && (k==9);
    }

this doesn't look like a good approach to me. Both functions are doing sort of similar things so they should be converted to single function -

cond(i, j, k, first, second, third) {
        return (i==first) && (j==second) && (k==third);
}

but then that makes my if condition unreadable -

if(cond(i, j, k, 2, 5, 8) || cond(i, j, k, 3, 6, 9))

so instead if I could have some aliases, I could simply write this as

if(cond258 || cond369)
3 Answers

I believe you can't do so without writing another function, or at least, not advisable/practical. Writing another method for conditional statement is actually refactoring your code, Decompose conditional in this case.

You could do something like this.

private static int i = 2;
private static int j = 5;
private static int k = 8;
private static Supplier<Boolean> cond  = ()->i == 2 && j == 5 && k == 8;    
public static void main(String[] args) {
    System.out.println(cond.get()); // prints true
    k = 11;
    System.out.println(cond.get()); // prints false
}
  • I made them static so they could be used in any context (static or instance).
  • The variables can't be local since local values in a lambda must be effectively final.
  • And they will be shared with other classes that instantiate the class that contains them.

Here is an example using instance fields.

int ii = 2;
int jj = 5;
int kk = 8;

Supplier<Boolean> cond = () -> ii == 2 && jj == 5 && kk == 8;
    
public static void main(String[] args) {

    ThisClass tc = new ThisClass();

    // static context so they need to be qualified.
    System.out.println(tc.cond.get()); // prints true
    tc.kk = 11;
    System.out.println(tc.cond.get()); // prints false
    tc.foo();
}
public void foo() {
    // instance method so cond and kk do not need to be qualified
    kk = 8;
    System.out.println(cond.get()); // true
}

This is how I solved this -

created a new class Triplet -

public class Triplet <F, S, T> {
    
    public F first;
    public S second;
    public T third;
    
    @Override
    public boolean equals(Object obj) {

        if (obj instanceof Triplet <?,?,?>) {
            
            Triplet <?, ?, ?> triplet = (Triplet <?, ?, ?>) obj;            
            return (this.first.equals(triplet.first) && this.second.equals(triplet.second) && this.third.equals(triplet.third));
        } 
        else 
            return false;
    }

    
    public Triplet (F first, S second, T third) throws NullPointerException {
        if(first==null|| second==null || third==null)
            throw new NullPointerException("Can't create object with null values for first or second or third");
        this.first = first;
        this.second = second;
        this.third = third;
    }   
}

Created static triplets -

public static final Triplet<Integer, Integer, Integer> triplet258 = new Triplet<Integer, Integer, Integer>(2, 5, 8);
public static final Triplet<Integer, Integer, Integer> triplet369 = new Triplet<Integer, Integer, Integer>(3, 6, 9);

my if condition changes to -

Triplet<Integer, Integer, Integer> myTriplet = new Triplet<Integer, Integer, Integer>(i, j, k);

if(myTriplet.equals(triplet258) || myTriplet.equals(triplet369))
Related