// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package tsptest.armstreamstyleserialization.models;

import com.azure.core.util.ExpandableEnum;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Defines values for Priority.
 */
public final class Priority implements ExpandableEnum<Integer>, JsonSerializable<Priority> {
    private static final Map<Integer, Priority> VALUES = new ConcurrentHashMap<>();

    private static final Function<Integer, Priority> NEW_INSTANCE = Priority::new;

    /**
     * high priority.
     */
    public static final Priority HIGH = fromValue(0);

    /**
     * low priority.
     */
    public static final Priority LOW = fromValue(1);

    private final Integer value;

    private Priority(Integer value) {
        this.value = value;
    }

    /**
     * Creates or finds a Priority.
     * 
     * @param value a value to look for.
     * @return the corresponding Priority.
     * @throws IllegalArgumentException if value is null.
     */
    public static Priority fromValue(Integer value) {
        if (value == null) {
            throw new IllegalArgumentException("'value' cannot be null.");
        }
        return VALUES.computeIfAbsent(value, NEW_INSTANCE);
    }

    /**
     * Gets known Priority values.
     * 
     * @return Known Priority values.
     */
    public static Collection<Priority> values() {
        return new ArrayList<>(VALUES.values());
    }

    /**
     * Gets the value of the Priority instance.
     * 
     * @return the value of the Priority instance.
     */
    @Override
    public Integer getValue() {
        return this.value;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        return jsonWriter.writeInt(getValue());
    }

    /**
     * Reads an instance of Priority from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of Priority if the JsonReader was pointing to an instance of it, or null if the JsonReader
     * was pointing to JSON null.
     * @throws IOException If an error occurs while reading the Priority.
     * @throws IllegalStateException If unexpected JSON token is found.
     */
    public static Priority fromJson(JsonReader jsonReader) throws IOException {
        JsonToken nextToken = jsonReader.nextToken();
        if (nextToken == JsonToken.NULL) {
            return null;
        }
        if (nextToken != JsonToken.NUMBER) {
            throw new IllegalStateException(
                String.format("Unexpected JSON token for %s deserialization: %s", JsonToken.NUMBER, nextToken));
        }
        return Priority.fromValue(jsonReader.getInt());
    }

    @Override
    public String toString() {
        return Objects.toString(this.value);
    }

    @Override
    public boolean equals(Object obj) {
        return this == obj;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this.value);
    }
}
