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    import java.util.Date;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    import de.kumpe.hadooptimizer.EvaluationResult;
027    import de.kumpe.hadooptimizer.Halter;
028    import de.kumpe.hadooptimizer.Stoppable;
029    
030    /**
031     * A {@link Halter} implementation halting a the specified {@link Date}.
032     * 
033     * @param <I>
034     *            the individuals' type
035     * 
036     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
037     */
038    public final class TimeBasedHalter<I> implements Halter<I>, Stoppable {
039            private static final long serialVersionUID = 1L;
040            private static final Log log = LogFactory.getLog(TimeBasedHalter.class);
041    
042            private final Date limit;
043    
044            private volatile boolean stop = false;
045    
046            /**
047             * Creates a new {@link TimeBasedHalter} halting at the given {@link Date}.
048             * 
049             * @param limit
050             *            the {@link Date} after which the evolution cycle should be
051             *            finished
052             */
053            public TimeBasedHalter(final Date limit) {
054                    if (null == limit) {
055                            throw new NullPointerException("limit may not be null");
056                    }
057                    if (log.isDebugEnabled()) {
058                            log.debug(format(
059                                            "Constructing new TimeBasedHalter which halts after %d ns.",
060                                            limit));
061                    }
062    
063                    this.limit = limit;
064            }
065    
066            @Override
067            public boolean halt(final Collection<EvaluationResult<I>> evaluationResults) {
068                    return stop || new Date().after(limit);
069            }
070    
071            @Override
072            public void stop() {
073                    stop = true;
074            }
075    
076            @Override
077            public String toString() {
078                    return "TimeBasedHalter [limit=" + limit + "]";
079            }
080    }