001/*
002 * Units of Measurement Implementation for Java SE
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.uom.se.quantity;
031
032import java.util.Objects;
033
034import javax.measure.Quantity;
035
036import tec.uom.se.ComparableQuantity;
037import tec.uom.se.spi.Range;
038
039/**
040 * A Quantity Range is a pair of {@link Quantity} items that represent a range of values.
041 * <p>
042 * Range limits MUST be presented in the same scale and have the same unit as measured data values.<br/>
043 * Subclasses of Range should be immutable.
044 * 
045 * @param <T>
046 *          The value of the range.
047 * 
048 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
049 * @version 0.4, June 30, 2017
050 * @see <a href= "http://www.botts-inc.com/SensorML_1.0.1/schemaBrowser/SensorML_QuantityRange.html"> SensorML: QuantityRange</a>
051 */
052public class QuantityRange<Q extends Quantity<Q>> extends Range<Quantity<Q>> {
053
054  protected QuantityRange(Quantity<Q> min, Quantity<Q> max, Quantity<Q> resolution) {
055    super(min, max, 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 an {@code QuantityRange} with the specified values.
080   *
081   * @param minimum
082   *          The minimum value for the measurement range.
083   * @param maximum
084   *          The maximum value for the measurement range.
085   * @return an {@code MeasurementRange} with the given values
086   */
087  @SuppressWarnings({ "rawtypes", "unchecked" })
088  public static QuantityRange of(Quantity minimum, Quantity maximum) {
089    return new QuantityRange(minimum, maximum);
090  }
091
092  /*
093   * (non-Javadoc)
094   * 
095   * @see tec.uom.se.spi.Range#contains()
096   */
097  @Override
098  public boolean contains(Quantity<Q> q) {
099    if (q != null && q.getValue() != null && q.getUnit() != null) {
100      if (hasMinimum() && hasMaximum()) {
101        if ((q.getUnit().isCompatible(getMinimum().getUnit())) && (q.getUnit().isCompatible(getMaximum().getUnit()))) {
102          if (q instanceof ComparableQuantity) {
103            final ComparableQuantity<Q> cq = (ComparableQuantity<Q>) q;
104            return (cq.isGreaterThanOrEqualTo(getMinimum()) && cq.isLessThanOrEqualTo(getMaximum()));
105          } else {
106            return (q.getValue().doubleValue() >= getMinimum().getValue().doubleValue() && q.getValue().doubleValue() <= getMaximum().getValue()
107                .doubleValue());
108          }
109        }
110      } // TODO other cases with only min or max
111    }
112    return false;
113  }
114
115  /*
116   * (non-Javadoc)
117   * 
118   * @see java.lang.Object#equals()
119   */
120  @Override
121  public boolean equals(Object obj) {
122    if (this == obj) {
123      return true;
124    }
125    if (obj instanceof QuantityRange<?>) {
126      @SuppressWarnings("unchecked")
127      final QuantityRange<Q> other = (QuantityRange<Q>) obj;
128      return Objects.equals(getMinimum(), other.getMinimum()) && Objects.equals(getMaximum(), other.getMaximum())
129          && Objects.equals(getResolution(), other.getResolution());
130    }
131    return false;
132  }
133
134  /*
135   * (non-Javadoc)
136   * 
137   * @see java.lang.Object#toString()
138   */
139  @Override
140  public String toString() {
141    final StringBuilder sb = new StringBuilder().append("min= ").append(getMinimum()).append(", max= ").append(getMaximum());
142    if (getResolution() != null) {
143      sb.append(", res= ").append(getResolution());
144    }
145    return sb.toString();
146  }
147}