2 * Copyright (c) 2010-2021 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.io.neeo.internal.models;
15 import java.util.Objects;
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.items.Item;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.types.StateDescription;
24 * The model representing a Neeo Channel Range used for sliders to specify the min/max and unit (serialize/deserialize
27 * Note that the unit is just a label that is put on the slider itself for the user
29 * @author Tim Roberts - Initial Contribution
32 public class NeeoDeviceChannelRange {
33 /** The range unit representing a number */
34 private static final String UNIT_NUMBER = "";
36 /** The range unit representing a percentage */
37 private static final String UNIT_PERCENT = "%";
40 * The default range with a minimum of 0, maximum of 100 and a unit of {@link #UNIT_PERCENT}
42 static final NeeoDeviceChannelRange DEFAULT = new NeeoDeviceChannelRange(0, 100, UNIT_PERCENT);
44 /** The minimum value for the range */
45 private final int minValue;
47 /** The maximum value for the range */
48 private final int maxValue;
50 /** The unit of the range */
51 private final String unit;
54 * Create the channel range from the given values
56 * @param minValue the miminmum value
57 * @param maxValue the maximum value
58 * @param unit a unit for the range. Defaults to {@link #UNIT_NUMBER} if null or empty
60 public NeeoDeviceChannelRange(int minValue, int maxValue, String unit) {
61 if (minValue > maxValue) {
62 throw new IllegalArgumentException(
63 "maxValue (" + maxValue + ") is smaller than minValue (" + minValue + ")");
65 this.minValue = minValue;
66 this.maxValue = maxValue;
67 this.unit = StringUtils.isEmpty(unit) ? UNIT_NUMBER : unit;
71 * Returns the minimum value for the range
73 * @return the minimum value for the range
75 public int getMinValue() {
80 * Returns the maximum value for the range
82 * @return the maximum value for the range
84 public int getMaxValue() {
89 * Returns the range unit
91 * @return a non-null, non-empty range unit
93 public String getUnit() {
98 * Helper function to create a {@link NeeoDeviceChannelRange} from a given {@link Item}
100 * @param item a non-null item
101 * @return a non-null {@link NeeoDeviceChannelRange} representing the {@link Item}
103 public static NeeoDeviceChannelRange from(Item item) {
104 Objects.requireNonNull(item, "item cannot be null");
106 final boolean supportsPercent = item.getAcceptedDataTypes().contains(PercentType.class);
108 final StateDescription sd = item.getStateDescription();
109 int min = 0, max = 100;
111 if (sd != null && sd.getMinimum() != null) {
112 min = sd.getMinimum().intValue();
115 if (sd != null && sd.getMaximum() != null) {
116 max = sd.getMaximum().intValue();
125 return new NeeoDeviceChannelRange(min, max, supportsPercent ? UNIT_PERCENT : UNIT_NUMBER);
129 public String toString() {
130 return "NeeoDeviceChannelRange [minValue=" + minValue + ", maxValue=" + maxValue + ", unit=" + unit + "]";