001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
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 */
017package org.nuxeo.ecm.platform.importer.queue;
018
019import java.util.concurrent.atomic.AtomicLong;
020
021/**
022 * @since 8.3
023 */
024public abstract class AbstractTaskRunner implements TaskRunner {
025
026    protected volatile boolean completed = false;
027
028    protected volatile Exception error;
029
030    protected final AtomicLong nbProcessed = new AtomicLong(0);
031
032    protected volatile boolean mustStop;
033
034    protected volatile boolean canStop;
035
036    protected volatile boolean started = false;
037
038
039    protected void incrementProcessed() {
040        nbProcessed.incrementAndGet();
041    }
042
043    @Override
044    public boolean isCompleted() {
045        return completed;
046    }
047
048    @Override
049    public boolean isTerminated() {
050        return !started || (completed || getError() != null);
051    }
052
053    @Override
054    public Exception getError() {
055        return error;
056    }
057
058    @Override
059    public long getNbProcessed() {
060        return nbProcessed.get();
061    }
062
063    @Override
064    public void mustStop() {
065        canStop = true;
066        mustStop = true;
067        started = false;
068    }
069
070    @Override
071    public void canStop() {
072        canStop = true;
073    }
074
075}