001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core;
018
019import org.apache.logging.log4j.status.StatusLogger;
020
021/**
022 * A life cycle to be extended.
023 * <p>
024 * Wraps a {@link LifeCycle.State}.
025 * </p>
026 */
027public class AbstractLifeCycle implements LifeCycle {
028
029    /**
030     * Allow subclasses access to the status logger without creating another instance.
031     */
032    protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
033
034    private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
035
036    protected boolean equalsImpl(final Object obj) {
037        if (this == obj) {
038            return true;
039        }
040        if (obj == null) {
041            return false;
042        }
043        if (getClass() != obj.getClass()) {
044            return false;
045        }
046        final LifeCycle other = (LifeCycle) obj;
047        if (state != other.getState()) {
048            return false;
049        }
050        return true;
051    }
052
053    @Override
054    public LifeCycle.State getState() {
055        return this.state;
056    }
057
058    protected int hashCodeImpl() {
059        final int prime = 31;
060        int result = 1;
061        result = prime * result + ((state == null) ? 0 : state.hashCode());
062        return result;
063    }
064
065    public boolean isInitialized() {
066        return this.state == LifeCycle.State.INITIALIZED;
067    }
068
069    @Override
070    public boolean isStarted() {
071        return this.state == LifeCycle.State.STARTED;
072    }
073
074    public boolean isStarting() {
075        return this.state == LifeCycle.State.STARTING;
076    }
077
078    @Override
079    public boolean isStopped() {
080        return this.state == LifeCycle.State.STOPPED;
081    }
082
083    public boolean isStopping() {
084        return this.state == LifeCycle.State.STOPPING;
085    }
086
087    protected void setStarted() {
088        this.setState(LifeCycle.State.STARTED);
089    }
090
091    protected void setStarting() {
092        this.setState(LifeCycle.State.STARTING);
093    }
094
095    protected void setState(final LifeCycle.State newState) {
096        this.state = newState;
097        // Need a better string than this.toString() for the message
098        // LOGGER.trace("{} {}", this.state, this);
099    }
100
101    protected void setStopped() {
102        this.setState(LifeCycle.State.STOPPED);
103    }
104
105    protected void setStopping() {
106        this.setState(LifeCycle.State.STOPPING);
107    }
108
109    @Override
110    public void initialize() {
111        this.state = State.INITIALIZED;
112    }
113
114    @Override
115    public void start() {
116        this.setStarted();
117    }
118
119    @Override
120    public void stop() {
121        this.state = LifeCycle.State.STOPPED;
122    }
123
124}