/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.text;

import com.google.caliper.SimpleBenchmark;
import net.ranides.assira.BenchmarkRunner;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class LexicalCastBenchmark extends SimpleBenchmark {
    
    
    public int timeJCastNumbers(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            jcast(1700.5d, Float.class);
            jcast(1700.5f, Float.class);
            jcast(1700L, Float.class);
            jcast(1700, Float.class);
            jcast((short)1700, Float.class);
            jcast((byte)70, Float.class);

            jcast(1700.5d, Double.class);
            jcast(1700.5f, Double.class);
            jcast(1700L, Double.class);
            jcast(1700, Double.class);
            jcast((short)1700, Double.class);
            jcast((byte)70, Double.class);

            jcast(1700.5d, Short.class);
            jcast(1700.5f, Short.class);
            jcast(1700L, Short.class);
            jcast(1700, Short.class);
            jcast((short)1700, Short.class);
            jcast((byte)70, Short.class);
        }
        return c;
    }

    public int timeJCastText(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            jcast(1700.5d, String.class);
            jcast(1700.5f, String.class);
            jcast(1700L, String.class);
            jcast(1700, String.class);
            jcast((short)1700, String.class);
            jcast((byte)70, String.class);

            jcast("1700.5", Double.class);
            jcast("1700.5", Float.class);
            jcast("1700", Long.class);
            jcast("1700", Integer.class);
            jcast("1700", Short.class);
            jcast("70", Byte.class);
            jcast("1700", String.class);

            jcast("true", Boolean.class);
            jcast("false", Boolean.class);

            jcast(true, String.class);
            jcast(false, String.class);
        }
        return c;
    }

    public int timeFCastNumbers(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            LexicalCast.cast(1700.5d, Float.class);
            LexicalCast.cast(1700.5f, Float.class);
            LexicalCast.cast(1700L, Float.class);
            LexicalCast.cast(1700, Float.class);
            LexicalCast.cast((short)1700, Float.class);
            LexicalCast.cast((byte)70, Float.class);

            LexicalCast.cast(1700.5d, Double.class);
            LexicalCast.cast(1700.5f, Double.class);
            LexicalCast.cast(1700L, Double.class);
            LexicalCast.cast(1700, Double.class);
            LexicalCast.cast((short)1700, Double.class);
            LexicalCast.cast((byte)70, Double.class);

            LexicalCast.cast(1700.5d, Short.class);
            LexicalCast.cast(1700.5f, Short.class);
            LexicalCast.cast(1700L, Short.class);
            LexicalCast.cast(1700, Short.class);
            LexicalCast.cast((short)1700, Short.class);
            LexicalCast.cast((byte)70, Short.class);
        }
        return c;
    }

    public int timeFCastText(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            LexicalCast.cast(1700.5d, String.class);
            LexicalCast.cast(1700.5f, String.class);
            LexicalCast.cast(1700L, String.class);
            LexicalCast.cast(1700, String.class);
            LexicalCast.cast((short)1700, String.class);
            LexicalCast.cast((byte)70, String.class);

            LexicalCast.cast("1700.5", Double.class);
            LexicalCast.cast("1700.5", Float.class);
            LexicalCast.cast("1700", Long.class);
            LexicalCast.cast("1700", Integer.class);
            LexicalCast.cast("1700", Short.class);
            LexicalCast.cast("70", Byte.class);
            LexicalCast.cast("1700", String.class);

            LexicalCast.cast("true", Boolean.class);
            LexicalCast.cast("false", Boolean.class);

            LexicalCast.cast(true, String.class);
            LexicalCast.cast(false, String.class);
        }
        return c;
    }
    
    private static <T> T jcast(Object value, Class<T> clazz) {
        if(clazz.isInstance(value)) {
            return clazz.cast(value);
        }
        return LexicalCast.fromJSON( LexicalCast.toJSON(value), clazz);
    }

    public static void main(String[] args) {
        BenchmarkRunner.run();
    }
}
