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 a given amount computation time.
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 ComputingTimeLimitHalter<I> implements Halter<I>, Stoppable {
038 private static final long serialVersionUID = 1L;
039 private static final Log log = LogFactory
040 .getLog(ComputingTimeLimitHalter.class);
041
042 private final long limit;
043 private transient long firstCycle;
044
045 private volatile boolean stop = false;
046
047 /**
048 * Creates a new {@link ComputingTimeLimitHalter} halting after the
049 * specified number of nanoseconds.
050 *
051 * @param limit
052 * the allowed computation time in nanoseconds
053 */
054 public ComputingTimeLimitHalter(final long limit) {
055 if (log.isDebugEnabled()) {
056 log.debug(format(
057 "Constructing new EvaluationLimitHalter which halts after %d ns.",
058 limit));
059 }
060
061 this.limit = limit;
062 }
063
064 @Override
065 public boolean halt(final Collection<EvaluationResult<I>> evaluationResults) {
066 final long now = System.nanoTime();
067 if (firstCycle <= 0) {
068 firstCycle = now;
069 }
070 return stop || limit < now - firstCycle;
071 }
072
073 @Override
074 public void stop() {
075 stop = true;
076 }
077
078 @Override
079 public String toString() {
080 return "EvaluationLimitHalter [limit=" + limit + "]";
081 }
082 }