0

I am binding two labels, calculating the value of the second label via a series of nested calls to Bindings.when(). Since I'm doing the exact same thing 3 times in a row to 3 similar labels, is there a way to simplify this code by overloading one of the Bindings methods or IntegerBinding methods?

private IntegerProperty ipStrength, ipAgility, ipIntelligence; private IntegerProperty ipStBonus, ipAgBonus, ipInBonus; public RolePlayingCharacter() { ... ipStBonus.bind(Bindings.when(ipStrength.lessThan(5)) .then(-1) .otherwise(Bindings.when(ipStrength.lessThan(9)) .then(0) .otherwise(Bindings.when(ipStrength.lessThan(11)) .then(1) .otherwise(2)))); ipAgBonus.bind(Bindings.when(ipAgility.lessThan(5)) .then(-1) .otherwise(Bindings.when(ipAgility.lessThan(9)) .then(0) .otherwise(Bindings.when(ipAgility.lessThan(11)) .then(1) .otherwise(2)))); ipInBonus.bind(Bindings.when(ipIntelligence.lessThan(5)) .then(-1) .otherwise(Bindings.when(ipIntelligence.lessThan(9)) .then(0) .otherwise(Bindings.when(ipIntelligence.lessThan(11)) .then(1) .otherwise(2)))); ... 

I have found I can Override Class IntegerBinding's computeValue method, but this doesn't seem to help any, since I would have to do the some thing 3 times anyway, once for each label:

 IntegerBinding ibStatBonus = new IntegerBinding() { { super.bind(ipStrength); } @Override protected int computeValue() { int iStatValue = ipStrength.get(); if (iStatValue < 5) { return -1; } else if (iStatValue < 9) { return 0; } else if (iStatValue < 11) { return 1; } else { return 2; } } }; 

I want to be able to do something simple like:

 ipStBonus.bind(ipStrength.calculateStatBonus()); ipAgBonus.bind(ipAgility.calculateStatBonus()); ipInBonus.bind(ipIntelligence.calculateStatBonus()); 

How do I implement such a thing? How do I make a caclulateStatBonus method part of the list of methods available to an IntegerProperty?

1 Answer 1

1

It's always possible to create a helper method for this:

public static IntegerBinding createStatBonusBinding(final IntegerProperty source) { return Bindings.createIntegerBinding(() -> { int value = source.get(); if (value < 5) { return -1; } if (value < 9) { return 0; } if (value < 11) { return 1; } return 2; }, source); } 
ipStBonus.bind(createStatBonusBinding(ipStrength)); ipAgBonus.bind(createStatBonusBinding(ipAgility)); ipInBonus.bind(createStatBonusBinding(ipIntelligence)); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.