2

I'm working on a project in which I need to represent conditional statements on object attributes and [booleans|integers|float...] because I need to store the results of these conditions (whether they are true or false) so that I don't have to check them again in the future (there are many conditions, it's actually an optimization).

class Foo {
    public String getThing() {
        return this.thing;
    }
    private String thing;

    public int getBob() {
        return this.bob;
    }
    private int bob;
}

class Bar {
    public String getStuff() {
        return this.stuff
    }
    private String stuff;
}

Foo foo = new Foo();
Bar bar = new Bar();

Here I would like to store the condition that foo.thing != bar.stuff and the condition that foo.bob < 3. I would like to be able to store all 4 main operations <, >, ==, != (the objects compared will implement Java's Comparable interface).

I imagined I could store a conditional statement in a

Triplet<getAttr1(), Operator.[Equal|GreaterThan...], getAttr2()

(with an evalution function and where Operator is a class I would have defined)

The attributes could be stored as functions since the value of the attribute will always be given by its getter. Thing is I also want to compare with other types, like boolean, int, float...

How can I represent these conditional statements?

valentin
  • 2,596
  • 6
  • 28
  • 48
  • This smells a little like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why are you wanting to do this? – Duncan Jones May 23 '14 at 07:56
  • Also, premature optimization. – Mikkel Løkke May 23 '14 at 07:57
  • @MikkelLøkke: that's exactly what I said to 'the guy who decides'. Sadly, he's not listening. – valentin May 23 '14 at 07:58
  • @Duncan: yup I know. Actually I can't see the interest of what they are asking me to do. – valentin May 23 '14 at 08:00
  • 1
    Well run it through a profiler. Yourkit is really nice, and will show you where the performance bottle necks really are. I can tell you with absolute certainty that it's not the evaluation of the boolean expression that is taking time. That is it it's not the line `if(a>b)` that is the problem, but it might be calculating a or b (or both), and time would be much better spent optimizing that (particularly if it is used in other places in the code also), rather than attempting to roll your own method caching mechanism. If you truly must tho', the expressions answer is the way forward. – Mikkel Løkke May 23 '14 at 08:08

1 Answers1

3

Consider using Expressions, e.g.

interface Expression<T> {
    public T evaluate();
}

class BinaryDoubleExpression implements Expression<Double> {
    DoubleExpression left, right;
    DoubleOperator operator;

    BinaryDoubleExpression(double left, double right, BinaryDoubleOperator operator) {
        this.left = left;
        this.right = right;
    }

    Double evaluate() {
        return operator.operate(left.evaluate(), right.evaluate());
    }
}

enum BinaryDoubleOperator {
    ...
    Double operate();
}

It's a way to model literals, unary, binary, and ternary operators, and support different types. Each implementation then knows how to evaluate itself, leading to a traversal of the expression's parse tree.

David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • This is really promising but looks like reinventing the wheel, isn't there any implementation out there that allows us to represent literals? – valentin May 23 '14 at 07:51
  • 1
    @toogy DoubleLiteralExpression takes about three lines of code, but you would have to repeat it for different types since the Number class doesn't have any arithmetic methods. – David Ehrmann May 23 '14 at 07:52
  • Yes but I'm also working with different kinds of objects. I want to be able to say that **object A's attribute foo value is inferior to 3** or **object A's attribute foo value is equal to object B's attribute foo value** – valentin May 23 '14 at 07:55
  • Here's another option if you need to look at "attributes:" [using el in a standalone manner](http://stackoverflow.com/questions/91692/jstl-jsp-el-expression-language-in-a-non-jsp-standalone-context). There's a continuum between expressions and full-blown templating languages. What you're looking for is somewhere on it. You just need to figure out exactly what your requirements are. – David Ehrmann May 23 '14 at 07:57
  • Just a warning: I've been down this road before, and there's probably 20 workable solutions that aren't insane, 10 that are workable, and three that are appropriate. It gets a bit absurd when you consider that you could use Rhino to do parts of this. – David Ehrmann May 23 '14 at 08:04