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.spi.Range; 037 038/** 039 * A Quantity Range is a pair of {@link Quantity} items that represent a range of values. 040 * <p> 041 * Range limits MUST be presented in the same scale and have the same unit as measured data values.<br/> 042 * Subclasses of Range should be immutable. 043 * 044 * @param <T> 045 * The value of the range. 046 * 047 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 048 * @version 0.2, December 17, 2014 049 * @see <a href="http://www.botts-inc.com/SensorML_1.0.1/schemaBrowser/SensorML_QuantityRange.html"> SensorML: QuantityRange</a> 050 */ 051public class QuantityRange<Q extends Quantity<Q>> extends Range<Quantity<Q>> { 052 private Quantity<Q> res; 053 054 protected QuantityRange(Quantity<Q> min, Quantity<Q> max, Quantity<Q> resolution) { 055 super(min, max); 056 this.res = resolution; 057 } 058 059 protected QuantityRange(Quantity<Q> min, Quantity<Q> max) { 060 super(min, max); 061 } 062 063 /** 064 * Returns an {@code QuantityRange} with the specified values. 065 * 066 * @param minimum 067 * The minimum value for the measurement range. 068 * @param maximum 069 * The maximum value for the measurement range. 070 * @param resolution 071 * The resolution of the measurement range. 072 * @return an {@code MeasurementRange} with the given values 073 */ 074 @SuppressWarnings({ "rawtypes", "unchecked" }) 075 public static QuantityRange of(Quantity minimum, Quantity maximum, Quantity resolution) { 076 return new QuantityRange(minimum, maximum, resolution); 077 } 078 079 /** 080 * Returns the resolution of the measurement range. The value is the same as that given as the constructor parameter for the largest value. 081 * 082 * @return resolution of the range, the value is the same as that given as the constructor parameter for the resolution 083 */ 084 public Quantity<Q> getResolution() { 085 return res; 086 } 087 088 /* 089 * (non-Javadoc) 090 * 091 * @see tec.units.ri.util.Range#contains() 092 */ 093 @Override 094 public boolean contains(Quantity<Q> q) { 095 if (q != null && q.getValue() != null) { 096 // TODO use hasMinimum() and hasMaximum() to avoid null of the 097 // range, too 098 if (q.getValue().doubleValue() >= getMinimum().getValue().doubleValue() && q.getValue().doubleValue() <= getMaximum().getValue().doubleValue()) { 099 return true; 100 } 101 } 102 return false; 103 } 104 105 /* 106 * (non-Javadoc) 107 * 108 * @see java.lang.Object#equals() 109 */ 110 @Override 111 public boolean equals(Object obj) { 112 if (this == obj) { 113 return true; 114 } 115 if (obj instanceof QuantityRange<?>) { 116 @SuppressWarnings("unchecked") 117 final QuantityRange<Q> other = (QuantityRange<Q>) obj; 118 return Objects.equals(getMinimum(), other.getMinimum()) && Objects.equals(getMaximum(), other.getMaximum()) 119 && Objects.equals(getResolution(), 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}