/*
 * Copyright 2014 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.examples.cheaptime.solver;

import java.math.BigDecimal;

public class CheapTimeCostCalculator {

    public static final long ONE_MILLION_LONG = 100000000000L;
    public static final double ONE_MILLION_DOUBLE = 100000000000.0;
    public static final BigDecimal ONE_MILLION_BIG_DECIMAL = new BigDecimal(ONE_MILLION_LONG);

    public static long toMicroCost(long cost) {
        return cost * ONE_MILLION_LONG;
    }

    public static long parseMicroCost(String costString) {
        BigDecimal costBigDecimal = new BigDecimal(costString);
        if (costBigDecimal.scale() > 11) {
            throw new IllegalArgumentException("The costString (" + costString + ") has a scale ("
                    + costBigDecimal.scale() + ") higher than 10.");
        }
        costBigDecimal = costBigDecimal.setScale(11);
        return costBigDecimal.multiply(ONE_MILLION_BIG_DECIMAL).longValueExact();
    }

    public static long multiplyTwoMicros(long aMicros, long bMicros) {
        // Long arithmetic overflows because maxPowerConsumption (675.4800000000) * maxPowerCost (0.0228608333)
        double aDouble = ((double) (aMicros)) / ONE_MILLION_DOUBLE;
        double bDouble = ((double) (bMicros)) / ONE_MILLION_DOUBLE;
        double result = aDouble * bDouble;
        return Math.round(result * ONE_MILLION_DOUBLE);
    }

    public static long divideTwoMicros(long aMicros, long bMicros) {
        double aDouble = ((double) (aMicros)) / ONE_MILLION_DOUBLE;
        double bDouble = ((double) (bMicros)) / ONE_MILLION_DOUBLE;
        double result = aDouble / bDouble;
        return Math.round(result * ONE_MILLION_DOUBLE);
    }

    private CheapTimeCostCalculator() {}

}

@@ -739,27 +739,33 @@
 al long 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _LONG = 
@@ -810,27 +810,33 @@
  double 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE 
@@ -888,27 +888,33 @@
 Decimal 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _BIG_DEC
@@ -935,27 +935,33 @@
 Decimal(
-ONE_MILLION
+MICROS_PER_ONE_AS
 _LONG);%0A
@@ -1031,27 +1031,33 @@
  cost * 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _LONG;%0A 
@@ -1499,27 +1499,33 @@
 ultiply(
-ONE_MILLION
+MICROS_PER_ONE_AS
 _BIG_DEC
@@ -1782,35 +1782,41 @@
 aMicros)) / 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE;%0A   
@@ -1856,35 +1856,41 @@
 bMicros)) / 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE;%0A   
@@ -1960,35 +1960,41 @@
 nd(result * 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE);%0A  
@@ -2111,35 +2111,41 @@
 aMicros)) / 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE;%0A   
@@ -2189,27 +2189,33 @@
 ros)) / 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOUBLE;
@@ -2297,19 +2297,25 @@
 t * 
-ONE_MILLION
+MICROS_PER_ONE_AS
 _DOU

rename to PER_ONE