TinyHealthResult.java

/*
 * Copyright 2025 Morimekta Utils Authors
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 net.morimekta.tiny.health;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

/**
 * A health check result containing a status and an optional message.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public record TinyHealthResult(
        @JsonProperty("status") TinyHealthStatus status,
        @JsonProperty("message") String message) {
    /**
     * @param status  The result status.
     * @param message The result status message.
     */
    public TinyHealthResult(TinyHealthStatus status, String message) {
        this.status = Objects.requireNonNull(status, "status == null");
        this.message = message;
    }

    /**
     * @return The OK result.
     */
    public static TinyHealthResult ok() {
        return new TinyHealthResult(TinyHealthStatus.OK, null);
    }

    /**
     * @param message Healthy service message. Optional.
     * @return The OK result.
     */
    public static TinyHealthResult ok(String message) {
        return new TinyHealthResult(TinyHealthStatus.OK, String.valueOf(message));
    }

    /**
     * @param message The unhealthy service message. Required.
     * @return The UNHEALTHY result.
     */
    public static TinyHealthResult unhealthy(String message) {
        return new TinyHealthResult(TinyHealthStatus.UNHEALTHY, message);
    }
}