StatusInfo.java 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2016 the original author or authors.
 *
 * 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.
 */
16 17 18
package de.codecentric.boot.admin.model;

import java.io.Serializable;
19 20 21
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
22

23 24
import com.fasterxml.jackson.annotation.JsonIgnore;

25 26 27 28 29 30
/**
 * Represents a certain status a certain time.
 *
 * @author Johannes Stelzer
 */
public class StatusInfo implements Serializable {
31
	private static final long serialVersionUID = 2L;
32 33 34

	private final String status;
	private final long timestamp;
35
	private final Map<String, Serializable> details;
36

37 38
	protected StatusInfo(String status, long timestamp,
			Map<String, ? extends Serializable> details) {
39
		this.status = status.toUpperCase();
40
		this.timestamp = timestamp;
41 42
		this.details = details != null ? Collections.unmodifiableMap(new HashMap<>(details))
				: Collections.<String, Serializable>emptyMap();
43 44
	}

45 46 47
	public static StatusInfo valueOf(String statusCode,
			Map<String, ? extends Serializable> details) {
		return new StatusInfo(statusCode, System.currentTimeMillis(), details);
48 49
	}

50 51 52 53
	public static StatusInfo valueOf(String statusCode) {
		return valueOf(statusCode, null);
	}

54
	public static StatusInfo ofUnknown() {
55
		return valueOf("UNKNOWN", null);
56 57 58
	}

	public static StatusInfo ofUp() {
59
		return ofUp(null);
60 61 62
	}

	public static StatusInfo ofDown() {
63
		return ofDown(null);
64 65 66
	}

	public static StatusInfo ofOffline() {
67 68 69 70 71 72 73 74 75 76 77 78 79
		return ofOffline(null);
	}

	public static StatusInfo ofUp(Map<String, ? extends Serializable> details) {
		return valueOf("UP", details);
	}

	public static StatusInfo ofDown(Map<String, ? extends Serializable> details) {
		return valueOf("DOWN", details);
	}

	public static StatusInfo ofOffline(Map<String, ? extends Serializable> details) {
		return valueOf("OFFLINE", details);
80 81 82 83 84 85 86 87 88 89
	}

	public String getStatus() {
		return status;
	}

	public long getTimestamp() {
		return timestamp;
	}

90
	public Map<String, Serializable> getDetails() {
91 92 93
		return details;
	}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	@JsonIgnore
	public boolean isUp() {
		return "UP".equals(status);
	}

	@JsonIgnore
	public boolean isOffline() {
		return "OFFLINE".equals(status);
	}

	@JsonIgnore
	public boolean isDown() {
		return "DOWN".equals(status);
	}

	@JsonIgnore
	public boolean isUnknown() {
		return "UNKNOWN".equals(status);
	}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		StatusInfo other = (StatusInfo) obj;
		if (status == null) {
			if (other.status != null) {
				return false;
			}
		} else if (!status.equals(other.status)) {
			return false;
		}
		return true;
	}

144 145 146 147 148
	@Override
	public String toString() {
		return "StatusInfo [status=" + status + ", timestamp=" + timestamp + "]";
	}

149
}