001 /* 002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package de.kumpe.hadooptimizer.impl; 017 018 import static java.lang.String.*; 019 020 import java.util.Collection; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import de.kumpe.hadooptimizer.EvaluationResult; 026 import de.kumpe.hadooptimizer.Halter; 027 import de.kumpe.hadooptimizer.Stoppable; 028 029 /** 030 * A {@link Halter} which halts after the specified number of cycles. 031 * 032 * @param <I> 033 * the individuals' type 034 * 035 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a> 036 */ 037 public final class CountingHalter<I> implements Halter<I>, Stoppable { 038 private static final long serialVersionUID = 1L; 039 private static final Log log = LogFactory.getLog(CountingHalter.class); 040 041 private final int nrOfCycles; 042 private int currentCycle; 043 044 private volatile boolean stop = false; 045 046 /** 047 * Creates a new {@link CountingHalter} which halts after the specified 048 * number of cycles. 049 * 050 * @param nrOfCycles 051 * the number of cycles after which the {@link CountingHalter} 052 * will halt 053 */ 054 public CountingHalter(final int nrOfCycles) { 055 if (0 > nrOfCycles) { 056 throw new IllegalArgumentException("nrOfCycles may not be negative"); 057 } 058 059 if (log.isDebugEnabled()) { 060 log.debug(format( 061 "Constructing new CountingHalter which halts after %d cycles.", 062 nrOfCycles)); 063 } 064 065 this.nrOfCycles = nrOfCycles; 066 } 067 068 @Override 069 public boolean halt(final Collection<EvaluationResult<I>> evaluationResults) { 070 if (log.isTraceEnabled()) { 071 log.trace(format("Checking for halt in cycle %d of %d total.", 072 currentCycle, nrOfCycles)); 073 } 074 ++currentCycle; 075 return stop || currentCycle > nrOfCycles; 076 } 077 078 public int getNrOfCycles() { 079 return nrOfCycles; 080 } 081 082 public int getCurrentCycle() { 083 return currentCycle; 084 } 085 086 @Override 087 public void stop() { 088 stop = true; 089 } 090 091 @Override 092 public String toString() { 093 return "CountingHalter [nrOfCycles=" + nrOfCycles + "]"; 094 } 095 }