001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2016, 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 0.2, December 17, 2014
047 * @see <a href="http://www.botts-inc.com/SensorML_1.0.1/schemaBrowser/SensorML_QuantityRange.html"> SensorML: QuantityRange</a>
048 */
049public class QuantityRange<Q extends Quantity<Q>> extends Range<Quantity<Q>> {
050  private Quantity<Q> res;
051
052  protected QuantityRange(Quantity<Q> min, Quantity<Q> max, Quantity<Q> resolution) {
053    super(min, max);
054    this.res = resolution;
055  }
056
057  protected QuantityRange(Quantity<Q> min, Quantity<Q> max) {
058    super(min, max);
059  }
060
061  /**
062   * Returns an {@code QuantityRange} with the specified values.
063   *
064   * @param minimum
065   *          The minimum value for the measurement range.
066   * @param maximum
067   *          The maximum value for the measurement range.
068   * @param resolution
069   *          The resolution of the measurement range.
070   * @return an {@code MeasurementRange} with the given values
071   */
072  @SuppressWarnings({ "rawtypes", "unchecked" })
073  public static QuantityRange of(Quantity minimum, Quantity maximum, Quantity resolution) {
074    return new QuantityRange(minimum, maximum, resolution);
075  }
076
077  /**
078   * Returns the resolution of the measurement range. The value is the same as that given as the constructor parameter for the largest value.
079   * 
080   * @return resolution of the range, the value is the same as that given as the constructor parameter for the resolution
081   */
082  public Quantity<Q> getResolution() {
083    return res;
084  }
085
086  /*
087   * (non-Javadoc)
088   * 
089   * @see tec.units.ri.util.Range#contains()
090   */
091  @Override
092  public boolean contains(Quantity<Q> q) {
093    if (q != null && q.getValue() != null) {
094      // TODO use hasMinimum() and hasMaximum() to avoid null of the
095      // range, too
096      if (q.getValue().doubleValue() >= getMinimum().getValue().doubleValue() && q.getValue().doubleValue() <= getMaximum().getValue().doubleValue()) {
097        return true;
098      }
099    }
100    return false;
101  }
102
103  /*
104   * (non-Javadoc)
105   * 
106   * @see java.lang.Object#equals()
107   */
108  @Override
109  public boolean equals(Object obj) {
110    if (this == obj) {
111      return true;
112    }
113    if (obj instanceof QuantityRange<?>) {
114      @SuppressWarnings("unchecked")
115      final QuantityRange<Q> other = (QuantityRange<Q>) obj;
116      return getMinimum() == other.getMinimum() || (getMinimum() != null && getMinimum().equals(other.getMinimum()))
117          && getMaximum() == other.getMaximum() || (getMaximum() != null && getMaximum().equals(other.getMaximum()))
118          && getResolution() == other.getResolution() || (getResolution() != null && getResolution().equals(other.getResolution()));
119    }
120    return false;
121  }
122
123  /*
124   * (non-Javadoc)
125   * 
126   * @see java.lang.Object#toString()
127   */
128  @Override
129  public String toString() {
130    final StringBuilder sb = new StringBuilder().append("min= ").append(getMinimum()).append(", max= ").append(getMaximum());
131    if (res != null) {
132      sb.append(", res= ").append(getResolution());
133    }
134    return sb.toString();
135  }
136}