This discussion is archived
1 Reply Latest reply: Nov 26, 2013 10:16 AM by James_D RSS

Replace complex calculation as NumberBinding

user8929955 Newbie
Currently Being Moderated

I have the following calculation defined:

public double computePrice(int min, int max, int available, int saturation,
        int productionRate, IPopulationStructure pop, ECityState state) {
    if (available==0) return max;
    if (available>=saturation) return min;
    // The cosine is defined on [0,PI] therefore we must stretch it to
    // saturation and then retrieve the value at the available amount
    double val =  Math.cos(available*Math.PI/saturation);
    // This gives us a value between [-1,1]
    val += 1;
    val *= 0.5;
    // Now between [0,1]
    // Stretch it
    val *= (max-min);
    // place it
    val += min;
    return val;
}

I would like the convert this into a bound value, as the input values may change. This means that the int values become IntegerProperty.

My problem is the calculation of the cosinus on line 7, as there is no equivalent method for NumberBindings.

How can I achieve my aim to get a Binding, which will change whenever one of the input properties change.

  • 1. Re: Replace complex calculation as NumberBinding
    James_D Guru
    Currently Being Moderated
    public DoubleBinding createPriceBinding(final IntegerProperty min, final IntegerProperty max, final IntegerProperty available, final IntegerProperty saturation,
         final IntegerProperty productionRate, final IPopulationStructure pop, final ECityState state) {
         return Bindings.createDoubleBinding(
              new Callable<Double>() {
                   @Override
                   public Double call() {
                         return computePrice(min.get(), max.get(), available.get(), saturation.get(), productionRate.get(), pop, state);
                   }
              }, min, max, available, saturation, productionRate);
    }
    

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points