001/*
002 * VM-Operator
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.runner.qemu.events;
020
021import org.jgrapes.core.Channel;
022import org.jgrapes.core.Components;
023import org.jgrapes.core.Event;
024
025/**
026 * The Class RunnerStateChange.
027 */
028@SuppressWarnings("PMD.DataClass")
029public class RunnerStateChange extends Event<Void> {
030
031    /**
032     * The state.
033     */
034    public enum RunState {
035        INITIALIZING, STARTING, RUNNING, TERMINATING, STOPPED
036    }
037
038    private final RunState state;
039    private final String reason;
040    private final String message;
041    private final boolean failed;
042
043    /**
044     * Instantiates a new runner state change.
045     *
046     * @param state the state
047     * @param reason the reason
048     * @param message the message
049     * @param channels the channels
050     */
051    public RunnerStateChange(RunState state, String reason, String message,
052            Channel... channels) {
053        this(state, reason, message, false, channels);
054    }
055
056    /**
057     * Instantiates a new runner state change.
058     *
059     * @param state the state
060     * @param reason the reason
061     * @param message the message
062     * @param failed the failed
063     * @param channels the channels
064     */
065    public RunnerStateChange(RunState state, String reason, String message,
066            boolean failed, Channel... channels) {
067        super(channels);
068        this.state = state;
069        this.reason = reason;
070        this.failed = failed;
071        this.message = message;
072    }
073
074    /**
075     * Returns the new state.
076     *
077     * @return the state
078     */
079    public RunState runState() {
080        return state;
081    }
082
083    /**
084     * Gets the reason.
085     *
086     * @return the reason
087     */
088    public String reason() {
089        return reason;
090    }
091
092    /**
093     * Gets the message.
094     *
095     * @return the message
096     */
097    public String message() {
098        return message;
099    }
100
101    /**
102     * Checks if is failed.
103     *
104     * @return the failed
105     */
106    public boolean failed() {
107        return failed;
108    }
109
110    @Override
111    public String toString() {
112        StringBuilder builder = new StringBuilder(50);
113        builder.append(Components.objectName(this))
114            .append(" [").append(state).append(": ").append(reason);
115        if (failed) {
116            builder.append(" (failed)");
117        }
118        if (channels() != null) {
119            builder.append(", channels=").append(Channel.toString(channels()));
120        }
121        builder.append(']');
122        return builder.toString();
123    }
124
125}