001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package tec.units.ri.quantity;
031
032import javax.measure.Quantity;
033
034import tec.units.ri.spi.Range;
035
036/**
037 * A Quantity Range is a pair of {@link Quantity} items that represent a range of values.
038 * <p>
039 * Range limits MUST be presented in the same scale and have the same unit as measured data values.<br>
040 * Subclasses of Range should be immutable.
041 * 
042 * @param <T>
043 *          The value of the range.
044 * 
045 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
046 * @version 1.0, October 6, 2016
047 * @since 1.0
048 * @see <a href="http://www.botts-inc.com/SensorML_1.0.1/schemaBrowser/SensorML_QuantityRange.html"> SensorML: QuantityRange</a>
049 */
050public class QuantityRange<Q extends Quantity<Q>> extends Range<Quantity<Q>> {
051  private Quantity<Q> res;
052
053  protected QuantityRange(Quantity<Q> min, Quantity<Q> max, Quantity<Q> resolution) {
054    super(min, max);
055    this.res = resolution;
056  }
057
058  protected QuantityRange(Quantity<Q> min, Quantity<Q> max) {
059    super(min, max);
060  }
061
062  /**
063   * Returns an {@code QuantityRange} with the specified values.
064   *
065   * @param minimum
066   *          The minimum value for the measurement range.
067   * @param maximum
068   *          The maximum value for the measurement range.
069   * @param resolution
070   *          The resolution of the measurement range.
071   * @return an {@code MeasurementRange} with the given values
072   */
073  @SuppressWarnings({ "rawtypes", "unchecked" })
074  public static QuantityRange of(Quantity minimum, Quantity maximum, Quantity resolution) {
075    return new QuantityRange(minimum, maximum, resolution);
076  }
077
078  /**
079   * Returns the resolution of the measurement range. The value is the same as that given as the constructor parameter for the largest value.
080   * 
081   * @return resolution of the range, the value is the same as that given as the constructor parameter for the resolution
082   */
083  public Quantity<Q> getResolution() {
084    return res;
085  }
086
087  /*
088   * (non-Javadoc)
089   * 
090   * @see tec.units.ri.util.Range#contains()
091   */
092  @Override
093  public boolean contains(Quantity<Q> q) {
094    if (q != null && q.getValue() != null) {
095      // TODO use hasMinimum() and hasMaximum() to avoid null of the
096      // range, too
097      if (q.getValue().doubleValue() >= getMinimum().getValue().doubleValue() && q.getValue().doubleValue() <= getMaximum().getValue().doubleValue()) {
098        return true;
099      }
100    }
101    return false;
102  }
103
104  /*
105   * (non-Javadoc)
106   * 
107   * @see java.lang.Object#equals()
108   */
109  @Override
110  public boolean equals(Object obj) {
111    if (this == obj) {
112      return true;
113    }
114    if (obj instanceof QuantityRange<?>) {
115      @SuppressWarnings("unchecked")
116      final QuantityRange<Q> other = (QuantityRange<Q>) obj;
117      return getMinimum() == other.getMinimum() || (getMinimum() != null && getMinimum().equals(other.getMinimum()))
118          && getMaximum() == other.getMaximum() || (getMaximum() != null && getMaximum().equals(other.getMaximum()))
119          && getResolution() == other.getResolution() || (getResolution() != null && getResolution().equals(other.getResolution()));
120    }
121    return false;
122  }
123
124  /*
125   * (non-Javadoc)
126   * 
127   * @see java.lang.Object#toString()
128   */
129  @Override
130  public String toString() {
131    final StringBuilder sb = new StringBuilder().append("min= ").append(getMinimum()).append(", max= ").append(getMaximum());
132    if (res != null) {
133      sb.append(", res= ").append(getResolution());
134    }
135    return sb.toString();
136  }
137}