View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model.meta;
17  
18  import org.fourthline.cling.model.Validatable;
19  import org.fourthline.cling.model.ValidationError;
20  
21  import java.util.List;
22  import java.util.ArrayList;
23  import java.util.logging.Logger;
24  
25  /**
26   * Integrity rule for a state variable, restricting its values to a range with steps.
27   * <p/>
28   * TODO: The question here is: Are they crazy enough to use this for !integer (e.g. floating point) numbers?
29   *
30   * @author Christian Bauer
31   */
32  public class StateVariableAllowedValueRange implements Validatable {
33  
34      final private static Logger log = Logger.getLogger(StateVariableAllowedValueRange.class.getName());
35  
36      final private long minimum;
37      final private long maximum;
38      final private long step;
39  
40      public StateVariableAllowedValueRange(long minimum, long maximum) {
41          this(minimum, maximum, 1);
42      }
43  
44      public StateVariableAllowedValueRange(long minimum, long maximum, long step) {
45          if (minimum > maximum) {
46              log.warning("UPnP specification violation, allowed value range minimum '" + minimum
47                                  + "' is greater than maximum '" + maximum + "', switching values.");
48              this.minimum = maximum;
49              this.maximum = minimum;
50          } else {
51              this.minimum = minimum;
52              this.maximum = maximum;
53          }
54          this.step = step;
55      }
56  
57      public long getMinimum() {
58          return minimum;
59      }
60  
61      public long getMaximum() {
62          return maximum;
63      }
64  
65      public long getStep() {
66          return step;
67      }
68  
69      public boolean isInRange(long value) {
70          return value >= getMinimum() && value <= getMaximum() && (value % step) == 0;
71      }
72  
73      public List<ValidationError> validate() {
74          return new ArrayList<>();
75      }
76  
77      @Override
78      public String toString() {
79          return "Range Min: " + getMinimum() + " Max: " + getMaximum() + " Step: " + getStep();
80      }
81  }