package com.example.androidplot.logplotandroid;

import android.util.Log;
import android.util.SparseBooleanArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by achaves on 18-09-2016.
 *
 */
public class RulerStepFinder {

    private static final String TAG = "RulerStepFinder";

    public static double find(final double range) {

        if (range <= 0d) {
            Log.e(TAG, "range value must be a positive number bigger than 0 (zero)");
            return -1;
        }

        /** minimum number of rulers in the plot */
        final int N_RULERS = 5;
        final int MAX_CYCLES = 100;
        final double step = range/N_RULERS;

        double s = 1d; // singler
        double d = 2d; // doubler
        double f = 5d; // fiver
        StepType stepType;

        List<Double> steppingList = new ArrayList<>();
        steppingList.add(0, s);
        steppingList.add(1, d);
        steppingList.add(2, f);

        if (step == s) {
            return s;
        }

        if (step == d) {
            return d;
        }

        if (step == f) {
            return f;
        }

        if (step > s) {
            stepType = StepType.BIGGER_1;
        } else {
            stepType = StepType.MINOR_1;
        }

        int fuse = 0;

        switch (stepType) {
            case BIGGER_1:
                while (true) {
                    // returning the biggest stepping number that is smaller than step
                    if (step > Collections.max(steppingList)) {
                        s = s*10d;
                        d = d*10d;
                        f = f*10d;
                        steppingList.add(s);
                        steppingList.add(d);
                        steppingList.add(f);
                    } else {

                        if (step == steppingList.get(steppingList.size() - 1)) {
                            return steppingList.get(steppingList.size() - 1);
                        }
                        if (step == steppingList.get(steppingList.size() - 2)) {
                            return steppingList.get(steppingList.size() - 2);
                        }
                        if (step == steppingList.get(steppingList.size() - 3)) {
                            return steppingList.get(steppingList.size() - 3);
                        }


                        if(step > steppingList.get(steppingList.size() - 1 - 2)){ // compare with singler
                            if (step > steppingList.get(steppingList.size() - 1 - 1)) { // compare with doubler
                                // return a doubler because is not bigger than maximum (the next fiver)
                                return  steppingList.get(steppingList.size() - 1 - 1);
                            } else {
                                return steppingList.get(steppingList.size() - 1 - 2); // return singler
                            }
                        } else {
                            return steppingList.get(steppingList.size() - 1 - 3); // return a fiver
                        }
                    }
                    fuse += 1;

                    if (fuse == MAX_CYCLES) {
                        Log.e(TAG, "number of cycles has been surpassed in BIGGER_1 cycle: returning ZERO");
                        return 0;
                    }
                }

            case MINOR_1:

                steppingList.clear();
                s = s/10d;
                d = d/10d;
                f = f/10d;
                steppingList.add(f);
                steppingList.add(d);
                steppingList.add(s);

                while (true) {
                    if (step < Collections.min(steppingList)) {
                        s = s/10d;
                        d = d/10d;
                        f = f/10d;
                        steppingList.add(f);
                        steppingList.add(d);
                        steppingList.add(s);

                    } else {

                        if (step == steppingList.get(steppingList.size() - 1)) {
                            return steppingList.get(steppingList.size() - 1);
                        }
                        if (step == steppingList.get(steppingList.size() - 2)) {
                            return steppingList.get(steppingList.size() - 2);
                        }
                        if (step == steppingList.get(steppingList.size() - 3)) {
                            return steppingList.get(steppingList.size() - 3);
                        }

                        if (step > steppingList.get(steppingList.size() - 1 - 1)) { // compare with doubler
                            if (step > steppingList.get(steppingList.size() - 1 - 2)) { // compare with fiver
                                return steppingList.get(steppingList.size() - 1 - 2); // return fiver
                            } else {
                                return steppingList.get(steppingList.size() - 1 - 1); // return doubler
                            }
                        } else {
                            return steppingList.get(steppingList.size() - 1); // return singler
                        }
                    }

                    fuse += 1;

                    if (fuse == MAX_CYCLES) {
                        Log.e(TAG, "number of cycles has been surpassed in MINOR_1 cycle: returning ZERO");
                        return 0;
                    }
                }
        }

        return 0;
    }

    private enum StepType{
        BIGGER_1,
        MINOR_1
    }
}

@@ -1,8 +1,614 @@
+/*%0A * Copyright 2016 Alexandre H.P. Chaves%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

Update RulerStepFinder.java