All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.console.wrappers.StateWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.wrappers;

import org.xlcloud.service.Session;
import org.xlcloud.service.Stack;
import org.xlcloud.service.State;

/**
 * Wraps {@link State} and {@link ExtendedState} together.
 * Presents {@link ExtendedState} if occured, otherwise presents normal stack {@link State}.
 * 
 * @author Konrad Król, AMG.net
 */
public class StateWrapper {

    private State state;
    private ExtendedState extendedState;
    
    private StateWrapper(State state) {
        this.state = state;
    }
    
    private StateWrapper(ExtendedState extendedState) {
        this.extendedState = extendedState;
    }
    
    /**
     * Creates {@link StateWrapper} for the given {@link State}
     * @param state
     *  - state to be wrapped
     * @return state wrapped by {@link StateWrapper}
     */
    public static StateWrapper wrapState(State state) {
        return new StateWrapper(state);
    }
    
    /**
     * Creates {@link StateWrapper} for the given {@link ExtendedState}
     * @param state
     *  - state to be wrapped
     * @return state wrapped by {@link StateWrapper}
     */
    public static StateWrapper wrapState(ExtendedState state) {
        return new StateWrapper(state);
    }
    
    /**
     * Creates {@link StateWrapper} for the state taken from {@link Session}
     * @param session
     *  - {@link Session} that contains state to be wrapped
     * @return {@link Session}' state wrapped by {@link StateWrapper}
     */
    public static StateWrapper wrapSession(Session session) {
        return (session != null) ? (StateWrapper.wrapState(session.getStatus())) : (StateWrapper.wrapState(State.STOPPED));
    }
    
    /**
     * @return text value for the wrapped state
     */
    public String getStateValue() {
        return (extendedState != null) ? extendedState.value() : state.value();  
    }

    /**
     * @return {@code true} if the status represents stopped {@link Stack}, {@code false} otherwise
     */
    public boolean isStopped() {
        return State.STOPPED.equals(state);
    }
    
    /**
     * @return {@code true} if the status represents {@link Stack} failed on starting, stopping or killed {@link Stack}, {@code false} otherwise
     */
    public boolean isFailed() {
        return State.FAILED_ON_STOPPING.equals(state)
                || State.FAILED_ON_STARTING.equals(state)
                || State.FAILED_ON_PAUSE.equals(state)
                || State.FAILED_ON_RESUME.equals(state)
                || State.FAILED_ON_UPDATE.equals(state);
    }
    
    /**
     * @return {@code true} if Stack is currently suspended
     */
    public boolean isSuspend() {
        return State.PAUSED.equals(state)
                || State.PAUSING.equals(state);
    }
    
    /**
     * @return {@code true} if the real {@link Stack} status is available, {@code false} otherwise
     */
    public boolean isCompleteState() {
        return (state != null);
    }
    
    /**
     * @return real {@link Stack} state if available
     */
    public State getCompleteState() {
        return state;
    }
    
    /**
     * @return real {@link Stack} state if available
     */
    public boolean isRunning() {
        return State.RUNNING.equals(state);
    }
    
    /**
     * @return {@code true} if the status represents {@link Stack} that is being started, stopped or if the status is currently being retrieved,
     *  {@code false} otherwise
     */
    public boolean isTransient() {
        return State.STARTING.equals(state)
                || State.UPDATING.equals(state)
                || State.STOPPING.equals(state)
                || State.RESUMING.equals(state)
                || State.PAUSING.equals(state)
                || State.CONFIGURING.equals(state)
                || State.SUSPENDING.equals(state)
                || State.SHUTTING_DOWN.equals(state)
                || ExtendedState.RETRIEVING.equals(extendedState);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy