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

package tsptest.enumservice.models;

/**
 * Defines values for Unit.
 */
public enum Unit {
    /**
     * Enum value 1.
     */
    GRAMS(1.0),

    /**
     * Enum value 0.001.
     */
    KILO_GRAMS(0.001),

    /**
     * Enum value 1000.
     */
    MILLIGRAM(1000.0);

    /**
     * The actual serialized value for a Unit instance.
     */
    private final double value;

    Unit(double value) {
        this.value = value;
    }

    /**
     * Parses a serialized value to a Unit instance.
     * 
     * @param value the serialized value to parse.
     * @return the parsed Unit object, or null if unable to parse.
     */
    public static Unit fromDouble(double value) {
        Unit[] items = Unit.values();
        for (Unit item : items) {
            if (Double.doubleToLongBits(item.toDouble()) == Double.doubleToLongBits(value)) {
                return item;
            }
        }
        return null;
    }

    /**
     * De-serializes the instance to double value.
     * 
     * @return the double value.
     */
    public double toDouble() {
        return this.value;
    }
}
