/*--------------------------------------------------------------*/
/* Licensed to the Apache Software Foundation (ASF) under one   */
/* or more contributor license agreements.  See the NOTICE file */
/* distributed with this work for additional information        */
/* regarding copyright ownership.  The ASF licenses this file   */
/* to you 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.corehunter.listener;

import java.io.PrintStream;

import org.corehunter.CoreHunterListener;
import org.jamesframework.core.problems.constraints.validations.Validation;
import org.jamesframework.core.problems.objectives.evaluations.Evaluation;
import org.jamesframework.core.search.Search;
import org.jamesframework.core.subset.SubsetSolution;

public class SimpleCoreHunterListener implements CoreHunterListener {

    private static final String DEFAULT_PREFIX = "";

    private String prefix;
    private PrintStream printStream;

    public SimpleCoreHunterListener() {
        this(System.err);
    }

    public SimpleCoreHunterListener(PrintStream printStream) {
        super();

        this.printStream = printStream;
        prefix = DEFAULT_PREFIX;
    }

    public final String getPrefix() {
        return prefix;
    }

    public final void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public void searchStarted(Search<? extends SubsetSolution> search) {
        printStream.println(String.format("%sSearch : %s started.", prefix, search.getName()));
    }

    @Override
    public void searchStopped(Search<? extends SubsetSolution> search) {
        double t = search.getRuntime() / 1000;
        long s = search.getSteps();
        printStream.println(
                String.format("%sSearch : %s stopped after %f seconds and %d steps", prefix, search.getName(), t, s));
        printStream.println(String.format("%sBest solution with evaluation : %f", prefix,
                search.getBestSolutionEvaluation().getValue()));
        printStream.println(String.format("%sBest solution with evaluation : %s", prefix, search.getBestSolution()));
    }

    @Override
    public void newBestSolution(Search<? extends SubsetSolution> search, SubsetSolution newBestSolution,
            Evaluation newBestSolutionEvaluation, Validation newBestSolutionValidation) {
        printStream.format("%sCurrent value: %f%n", prefix, newBestSolutionEvaluation.getValue());
    }

    @Override
    public void preprocessingStarted(String message) {
        printStream.println(String.format("%s%s", prefix, message));
    }

    @Override
    public void preprocessingStopped(String message) {
        printStream.println(String.format("%s%s", prefix, message));
    }
}

@@ -1261,16 +1261,41 @@
 tStream;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -2267,38 +2267,34 @@
 tStream.
-println(String.format(
+format(Locale.US, 
 %22%25sSearc
@@ -2307,17 +2307,18 @@
  started
-.
+%25n
 %22, prefi
@@ -2333,25 +2333,24 @@
 h.getName())
-)
 ;%0A    %7D%0A%0A   
@@ -2536,23 +2536,22 @@
 tStream.
-println
+format
 (%0A      
@@ -2560,30 +2560,43 @@
         
-String.format(
+Locale.US,%0A                
 %22%25sSearc
@@ -2639,18 +2639,36 @@
 %25d steps
-%22,
+%25n%22,%0A               
  prefix,
@@ -2690,17 +2690,25 @@
 (), t, s
-)
+%0A        
 );%0A     
@@ -2722,38 +2722,67 @@
 tStream.
-println(String.format(
+format(%0A                Locale.US,%0A                
 %22%25sBest 
@@ -2814,19 +2814,14 @@
 : %25f
-%22, prefix,%0A
+%25n%22,%0A 
     
@@ -2831,16 +2831,23 @@
         
+prefix,
  search.
@@ -2884,17 +2884,25 @@
 tValue()
-)
+%0A        
 );%0A     
@@ -2916,38 +2916,34 @@
 tStream.
-println(String.format(
+format(Locale.US, 
 %22%25sBest 
@@ -2971,16 +2971,18 @@
 ion : %25s
+%25n
 %22, prefi
@@ -3009,17 +3009,16 @@
 ution())
-)
 ;%0A    %7D%0A
@@ -3254,16 +3254,27 @@
 .format(
+Locale.US, 
 %22%25sCurre
@@ -3437,35 +3437,33 @@
 eam.
-println(String.format(
+format(Locale.US, 
 %22%25s%25s
+%25n
 %22, p
@@ -3469,33 +3469,32 @@
 prefix, message)
-)
 ;%0A    %7D%0A%0A    @Ov
@@ -3579,23 +3579,8 @@
 eam.
-println(String.
 form
@@ -3587,16 +3587,18 @@
 at(%22%25s%25s
+%25n
 %22, prefi
@@ -3608,17 +3608,16 @@
 message)
-)
 ;%0A    %7D%0A

use US locale for printed messages