/**
 * Copyright (c) 2016, Ecole des Mines de Nantes
 * All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 * This product includes software developed by the <organization>.
 * 4. Neither the name of the <organization> nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * <p>
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.chocosolver.solver.search.measure;

import org.chocosolver.solver.objective.IBoundsManager;
import org.chocosolver.solver.search.SearchState;

import java.util.Objects;
import java.util.function.LongSupplier;

/**
 * Object which stores resolution information to get statistics
 *
 * @author Charles Prud'Homme, Arnaud Malapert
 * @since 3.0.0
 */
public final class MeasuresRecorder extends Measures {

    /**
     * When the clock watch starts
     */
    private long startingTime;

    private LongSupplier currentNanoTime;

    /**
     * Create a measures recorder
     */
    public MeasuresRecorder(String modelName) {
        super(modelName);
        stopStopwatch();
    }

    @Override
    public long getTimeCountInNanoSeconds() {
        timeCount = currentNanoTime.getAsLong();
        return super.getTimeCountInNanoSeconds();
    }

    /**
     * Start the stopwatch, to compute resolution time
     *
     */
    public void startStopwatch() {
        startingTime = System.nanoTime();
        currentNanoTime = () -> System.nanoTime() - startingTime;
    }

    /**
     * Stop the stopwatch, the resolution time is fixed.
     */
    public void stopStopwatch() {
        currentNanoTime = () -> timeCount;
    }

    //****************************************************************************************************************//
    //**************************************** SETTERS ***************************************************************//
    //****************************************************************************************************************//


    /**
     * indicates whether or not the optimum has been found and proved
     * @param objectiveOptimal <tt>true</tt> if the objective is proven to be optimal
     */
    public final void setObjectiveOptimal(boolean objectiveOptimal) {
        this.objectiveOptimal = objectiveOptimal;
    }

    /**
     * Reset every measure to its default value (mostly 0)
     */
    public void reset() {
        state = SearchState.NEW;
        objectiveOptimal = false;
        solutionCount = 0;
        timeCount = 0;
        stopStopwatch();
        nodeCount = 0;
        backtrackCount = 0;
        failCount = 0;
        restartCount = 0;
        depth = 0;
        maxDepth = 0;
    }

    //****************************************************************************************************************//
    //**************************************** INCREMENTERS **********************************************************//
    //****************************************************************************************************************//

    /**
     * increment node counter
     */
    public final void incNodeCount() {
        nodeCount++;
        if (depth > maxDepth) {
            maxDepth = depth;
        }
    }

    /**
     * increment backtrack counter
     */
    public final void incBackTrackCount() {
        backtrackCount++;
    }

    /**
     * increment fail counter
     */
    public final void incFailCount() {
        failCount++;
    }

    /**
     * increment restart counter
     */
    public final void incRestartCount() {
        restartCount++;
    }

    /**
     * increment solution counter
     */
    public final void incSolutionCount() {
        solutionCount++;
    }

    /**
     * Increments current depth
     */
    public final void incDepth() {
        depth++;
    }

    /**
     * Decrements current depth
     */
    public final void decDepth() {
        depth--;
    }

    /**
     * Update the current search state
     * @param state new search state
     */
    public final void setSearchState(SearchState state) {
        Objects.requireNonNull(state);
        this.state = state;
    }

    /**
     * Update the bounds managed
     * @param boundsManager new bound manager
     */
    public final void setBoundsManager(IBoundsManager boundsManager) {
        Objects.requireNonNull(boundsManager);
        this.boundsManager = boundsManager;
    }

    public final void setTimeCount(long timeCount) {
        this.timeCount = timeCount;
    }

    public final void setReadingTimeCount(long readingTimeCount) {
        this.readingTimeCount = readingTimeCount;
    }

}

@@ -68,28 +68,24 @@
 reserved.%0A *
- %3Cp%3E
 %0A * Redistri
@@ -285,32 +285,35 @@
 ove copyright%0A *
+   
  notice, this li
@@ -433,16 +433,19 @@
 right%0A *
+   
  notice,
@@ -507,16 +507,19 @@
 n the%0A *
+   
  documen
@@ -658,16 +658,19 @@
 tware%0A *
+   
  must di
@@ -708,16 +708,19 @@
 ment:%0A *
+   
  This pr
@@ -831,16 +831,19 @@
 r the%0A *
+   
  names o
@@ -906,16 +906,19 @@
 ducts%0A *
+   
  derived
@@ -986,12 +986,8 @@
 .%0A *
- %3Cp%3E
 %0A * 

update header