Performant functional evaluation for enum value I have a Java enum which represents the side of an order (enum Side {ASK, BID}) and I'm constantly writing code with which I'd like to use the ternary operator, e.g.:

BigDecimal orderPrice = order.getSide() == Side.ASK ? calcAskPrice(order) : calcBidPrice(order)


The issue is that I want to future proof the code so that it fails if the provided value is neither ASK nor BID. The obvious solution seems unnecessarily verbose to me, because the pattern needs to be repeates tens (maybe even hundreds?) of times throughout the code. I'd rather not have this everywhere:

BigDecimal orderPrice;
if (order.getSide() == Side.ASK) {
   orderPrice = calcAskPrice(order);
} else if (side == Side.BID) {
   orderPrice = calcBidPrice(order);
} else {
   throw new IllegalArgumentException("The provided order must have a side that is either ASK or BID");
}


Instead I'd prefer a more functional approach, like the ternary operator.

A safer approach works, but is (i) also verbose and (ii) confusing:

BigDecimal orderPrice = order.getSide() == Side.ASK ? calcAskPrice(order) : order.getSide() == Side.BID ? calcBidPrice(order) : null;
assertNotNull(orderPrice);


Thinking about my desired syntax, I decided to add some methods to the enum such that I could write:

BigDecimal orderPrice = order.getSide().ifAsk(calcAskPrice(order)).ifBid(calcBidPrice(order)).elseFail();


This was implemented via an object that would store the value of the enum along with the two result values, and whose elseFail() method would return the appropriate result or fail. I was extremely satisfied with this, until (in pursuit of a separate issue) I noticed that my little evaluator object had a rather large footprint, but in terms of object creation &amp; CPU time.

In retrospect this makes sense, because the code processes large numbers of updates, and is scattered with checks to the order side.

I'm now left with trying to think up a performant/low overhead way to provide similar syntax. The easiest solution that I can think of is to have a method on the enum:

public &lt;T&gt; T ifAskBid(T askResult, T bidResult) {
   // . . .
}


but I'm curious if anybody can think of a nicer solution.
BigDecimal orderPrice = order.getSide().ifAsk(calcAskPrice(order)).ifBid(calcBidPrice(order)).elseFail();



This seems rather long and complicated.  Consider 

BigDecimal orderPrice = order.getSide().calculatePrice(order);


Or you might hide it as 

BigDecimal orderPrice = order.calculatePrice();


With 

BigDecimal calculatePrice() {
    return getSide().calculatePrice(this);
}


As you can read on Stack Overflow, you can then define different implementations of the method for each value of the enum.  

public enum Side {

    ASK {
        @Override
        public BigDecimal calculatePrice(Order order) {
            return Something.calcAskPrice(order);
        }
    },
    BID {
        @Override
        public BigDecimal calculatePrice(Order order) {
            return Something.calcBidPrice(order);
        }
    };


    public BigDecimal calculatePrice(Order order) {
        throw new IllegalArgumentException("The provided order must have a side that is either ASK or BID");
    }

}


I wrote Something, as you don't provide enough context to write whatever it actually is.  It's possible that the calcAskPrice and calcBidPrice methods should have their contents pulled into the override methods.  Or that you need to pass some kind of additional object to the calculatePrice methods.  

There's an argument that overriding a method like this is bad form.  I'll leave it up to you if that matters.  I did it here to duplicate your original functionality.  But maybe you don't need the original functionality.  Again, without more context, it's difficult to tell.  

Perhaps it should go without saying, but I didn't test this, as there isn't enough context.  I did not go the extra mile and verify syntactical correctness by dummying up a driver for this code.