001/*
002 * Copyright 2012-2018 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 *      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.springframework.boot.actuate.health;
018
019import com.fasterxml.jackson.annotation.JsonInclude;
020import com.fasterxml.jackson.annotation.JsonInclude.Include;
021import com.fasterxml.jackson.annotation.JsonProperty;
022
023import org.springframework.util.Assert;
024import org.springframework.util.ObjectUtils;
025
026/**
027 * Value object to express state of a component or subsystem.
028 * <p>
029 * Status provides convenient constants for commonly used states like {@link #UP},
030 * {@link #DOWN} or {@link #OUT_OF_SERVICE}.
031 * <p>
032 * Custom states can also be created and used throughout the Spring Boot Health subsystem.
033 *
034 * @author Christian Dupuis
035 * @since 1.1.0
036 */
037@JsonInclude(Include.NON_EMPTY)
038public final class Status {
039
040        /**
041         * {@link Status} indicating that the component or subsystem is in an unknown state.
042         */
043        public static final Status UNKNOWN = new Status("UNKNOWN");
044
045        /**
046         * {@link Status} indicating that the component or subsystem is functioning as
047         * expected.
048         */
049        public static final Status UP = new Status("UP");
050
051        /**
052         * {@link Status} indicating that the component or subsystem has suffered an
053         * unexpected failure.
054         */
055        public static final Status DOWN = new Status("DOWN");
056
057        /**
058         * {@link Status} indicating that the component or subsystem has been taken out of
059         * service and should not be used.
060         */
061        public static final Status OUT_OF_SERVICE = new Status("OUT_OF_SERVICE");
062
063        private final String code;
064
065        private final String description;
066
067        /**
068         * Create a new {@link Status} instance with the given code and an empty description.
069         * @param code the status code
070         */
071        public Status(String code) {
072                this(code, "");
073        }
074
075        /**
076         * Create a new {@link Status} instance with the given code and description.
077         * @param code the status code
078         * @param description a description of the status
079         */
080        public Status(String code, String description) {
081                Assert.notNull(code, "Code must not be null");
082                Assert.notNull(description, "Description must not be null");
083                this.code = code;
084                this.description = description;
085        }
086
087        /**
088         * Return the code for this status.
089         * @return the code
090         */
091        @JsonProperty("status")
092        public String getCode() {
093                return this.code;
094        }
095
096        /**
097         * Return the description of this status.
098         * @return the description
099         */
100        @JsonInclude(Include.NON_EMPTY)
101        public String getDescription() {
102                return this.description;
103        }
104
105        @Override
106        public boolean equals(Object obj) {
107                if (obj == this) {
108                        return true;
109                }
110                if (obj != null && obj instanceof Status) {
111                        return ObjectUtils.nullSafeEquals(this.code, ((Status) obj).code);
112                }
113                return false;
114        }
115
116        @Override
117        public int hashCode() {
118                return this.code.hashCode();
119        }
120
121        @Override
122        public String toString() {
123                return this.code;
124        }
125
126}