package za.redbridge.experiment.NEATM.sensor.parameter.spec;

/**
 * Created by jamie on 2014/11/28.
 */
public final class Limiters {

    private Limiters() {
    }

    public static Limiter wrap() {
        return new Limiter() {
            @Override
            public float limitValue(float oldValue, float newValue, Range range) {
                if ((range.inclusiveMin && newValue < range.min)
                        || (!range.inclusiveMin && newValue <= range.min)) {
                    return range.inclusiveMax ?
                            range.max : Math.nextAfter(range.max, Double.NEGATIVE_INFINITY);
                }

                if ((range.inclusiveMax && newValue > range.max)
                        || (!range.inclusiveMax && newValue >= range.max)) {
                    return range.inclusiveMin ?
                            range.min : Math.nextAfter(range.min, Double.POSITIVE_INFINITY);
                }

                return newValue;
            }
        };
    }

    public static Limiter clamp() {
        return new Limiter() {
            @Override
            public float limitValue(float oldValue, float newValue, Range range) {
                if (range.inclusiveMin) {
                    if (newValue < range.min) {
                        return range.min;
                    }
                } else {
                    if (newValue <= range.min) {
                        return Math.nextAfter(range.min, Double.POSITIVE_INFINITY);
                    }
                }

                if (range.inclusiveMax) {
                    if (newValue > range.max) {
                        return range.max;
                    }
                } else {
                    if (newValue >= range.max) {
                        return Math.nextAfter(range.max, Double.NEGATIVE_INFINITY);
                    }
                }

                return newValue;
            }
        };
    }

}

@@ -352,45 +352,38 @@
     
-if ((range.inclusiveMin && newValue %3C
+final float diff = range.max -
  ran
@@ -388,17 +388,17 @@
 ange.min
-)
+;
 %0A       
@@ -402,37 +402,62 @@
             
-        %7C%7C
+newValue %25= diff;%0A                if
  (
-!
 range.inclus
@@ -468,33 +468,32 @@
 in && newValue %3C
-=
  range.min)) %7B%0A 
@@ -486,20 +486,16 @@
 ange.min
-)) %7B
 %0A       
@@ -499,39 +499,40 @@
                 
-return 
+    %7C%7C !
 range.inclusiveM
@@ -535,104 +535,75 @@
 iveM
-ax ?%0A                            range.max : Math.nextAfter(range.max, Double.NEGATIVE_INFINITY)
+in && newValue %3C= range.min) %7B%0A                    newValue += diff
 ;%0A  
@@ -617,38 +617,25 @@
        %7D
-%0A%0A               
+ else
  if 
-(
 (range.i
@@ -669,17 +669,16 @@
 ange.max
-)
 %0A       
@@ -697,17 +697,16 @@
      %7C%7C 
-(
 !range.i
@@ -742,17 +742,16 @@
 nge.max)
-)
  %7B%0A     
@@ -769,127 +769,24 @@
     
-return range.inclusiveMin ?%0A                            range.min : Math.nextAfter(range.min, Double.POSITIVE_INFINITY)
+newValue -= diff
 ;%0A  
@@ -793,33 +793,32 @@
               %7D%0A
-%0A
                 

Fix wrap limiter