001/*
002 * Copyright 2006-2007 the original author or authors.
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 *      https://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
017package org.springframework.batch.repeat;
018
019
020/**
021 * Interface for batch completion policies, to enable batch operations to
022 * strategise normal completion conditions. Stateful implementations of batch
023 * iterators should <em>only</em> update state using the update method. If you
024 * need custom behaviour consider extending an existing implementation or using
025 * the composite provided.
026 * 
027 * @author Dave Syer
028 * 
029 */
030public interface CompletionPolicy {
031
032        /**
033         * Determine whether a batch is complete given the latest result from the
034         * callback. If this method returns true then
035         * {@link #isComplete(RepeatContext)} should also (but not necessarily vice
036         * versa, since the answer here depends on the result).
037         * 
038         * @param context the current batch context.
039         * @param result the result of the latest batch item processing.
040         * 
041         * @return true if the batch should terminate.
042         * 
043         * @see #isComplete(RepeatContext)
044         */
045        boolean isComplete(RepeatContext context, RepeatStatus result);
046
047        /**
048         * Allow policy to signal completion according to internal state, without
049         * having to wait for the callback to complete.
050         * 
051         * @param context the current batch context.
052         * 
053         * @return true if the batch should terminate.
054         */
055        boolean isComplete(RepeatContext context);
056
057        /**
058         * Create a new context for the execution of a batch. N.B. implementations
059         * should <em>not</em> return the parent from this method - they must
060         * create a new context to meet the specific needs of the policy. The best
061         * way to do this might be to override an existing implementation and use
062         * the {@link RepeatContext} to store state in its attributes.
063         * 
064         * @param parent the current context if one is already in progress.
065         * @return a context object that can be used by the implementation to store
066         * internal state for a batch.
067         */
068        RepeatContext start(RepeatContext parent);
069
070        /**
071         * Give implementations the opportunity to update the state of the current
072         * batch. Will be called <em>once</em> per callback, after it has been
073         * launched, but not necessarily after it completes (if the batch is
074         * asynchronous).
075         * 
076         * @param context the value returned by start.
077         */
078        void update(RepeatContext context);
079
080}