2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.insteon.internal.device;
15 import static org.openhab.binding.insteon.internal.InsteonBindingConstants.*;
17 import java.text.DecimalFormat;
18 import java.util.Arrays;
19 import java.util.Comparator;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
28 * The {@link RampRate} represents a ramp rate for Insteon dimmer products
30 * @author Jeremy Setton - Initial contribution
33 public enum RampRate {
67 private static final Map<Integer, RampRate> VALUE_MAP = Arrays.stream(values())
68 .collect(Collectors.toUnmodifiableMap(rate -> rate.value, Function.identity()));
70 private final int value;
71 private final double time;
73 private RampRate(int value, double time) {
78 public int getValue() {
82 public double getTimeInSeconds() {
86 public long getTimeInMilliseconds() {
87 return (long) (time * 1000);
91 public String toString() {
92 double time = getTimeInSeconds();
98 return new DecimalFormat("0.#").format(time) + unit;
102 * Factory method for determining if a given feature type supports ramp rate
104 * @param featureType the feature type
105 * @return true if supported
107 public static boolean supportsFeatureType(String featureType) {
108 return FEATURE_TYPE_GENERIC_DIMMER.equals(featureType);
112 * Factory method for getting a RampRate from a ramp rate value
114 * @param value the ramp rate value
115 * @return the ramp rate
117 public static RampRate valueOf(int value) {
118 return VALUE_MAP.getOrDefault(value, RampRate.DEFAULT);
122 * Factory method for getting a RampRate from the closest ramp time
124 * @param time the ramp time
125 * @return the ramp rate
127 public static RampRate fromTime(double time) {
128 return VALUE_MAP.values().stream().min(Comparator.comparingDouble(rate -> Math.abs(rate.time - time))).get();
132 * Factory method for getting a RampRate from a ramp rate string
134 * @param string the ramp rate string
135 * @return the ramp rate
137 public static @Nullable RampRate fromString(String string) {
139 return fromTime(Double.parseDouble(string));
140 } catch (NumberFormatException e) {
141 return VALUE_MAP.values().stream().filter(rate -> rate.toString().equals(string)).findAny().orElse(null);