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.spi; 031 032import tec.uom.lib.common.function.MaximumSupplier; 033import tec.uom.lib.common.function.MinimumSupplier; 034 035/** 036 * A Range is a pair of <code>T</code> items that represent a range of values. 037 * <p> 038 * Subclasses of Range should be immutable. 039 * 040 * @param <T> 041 * The value of the range. 042 * 043 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 044 * @version 1.0, October 6, 2016 045 * @since 1.0 046 */ 047public abstract class Range<T> implements MinimumSupplier<T>, MaximumSupplier<T> { 048 // TODO do we keep null for min and max to represent infinity? 049 // Java 8 Optional was evaluated, but caused conflict with the type-safe 050 // Quantity feature of this API, plus it won't work in CLDC8 051 private final T min; 052 private final T max; 053 054 /** 055 * Construct an instance of Range with a min and max value. 056 * 057 * @param min 058 * The minimum value for the measurement range. 059 * @param max 060 * The maximum value for the measurement range. 061 */ 062 protected Range(T min, T max) { 063 this.min = min; 064 this.max = max; 065 } 066 067 /** 068 * Returns the smallest value of the range. The value is the same as that given as the constructor parameter for the smallest value. 069 * 070 * @return the minimum value 071 */ 072 public T getMinimum() { 073 return min; 074 } 075 076 /** 077 * Returns the largest value of the measurement range. The value is the same as that given as the constructor parameter for the largest value. 078 * 079 * @return the maximum value 080 */ 081 public T getMaximum() { 082 return max; 083 } 084 085 /** 086 * Method to easily check if {@link #getMinimum()} is not {@code null}. 087 * 088 * @return {@code true} if {@link #getMinimum()} is not {@code null} . 089 */ 090 public boolean hasMinimum() { 091 return min != null; 092 } 093 094 /** 095 * Method to easily check if {@link #getMaximum()} is not {@code null}. 096 * 097 * @return {@code true} if {@link #getMaximum()} is not {@code null}. 098 */ 099 public boolean hasMaximum() { 100 return max != null; 101 } 102 103 /** 104 * Checks whether the given <code>T</code> is within this range 105 * 106 * @param t 107 * @return true if the value is within the range 108 */ 109 public abstract boolean contains(T t); 110 111 /* 112 * (non-Javadoc) 113 * 114 * @see java.lang.Object#hashCode() 115 */ 116 @Override 117 public int hashCode() { 118 final int prime = 31; 119 int result = 1; 120 result = prime * result + ((max == null) ? 0 : max.hashCode()); 121 result = prime * result + ((min == null) ? 0 : min.hashCode()); 122 return result; 123 } 124 125 /* 126 * (non-Javadoc) 127 * 128 * @see java.lang.Object#equals() 129 */ 130 @Override 131 public boolean equals(Object obj) { 132 if (this == obj) 133 return true; 134 if (obj == null) 135 return false; 136 if (getClass() != obj.getClass()) 137 return false; 138 @SuppressWarnings("rawtypes") 139 Range other = (Range) obj; 140 if (max == null) { 141 if (other.max != null) 142 return false; 143 } else if (!max.equals(other.max)) 144 return false; 145 if (min == null) { 146 if (other.min != null) 147 return false; 148 } else if (!min.equals(other.min)) 149 return false; 150 return true; 151 } 152 153 /* 154 * (non-Javadoc) 155 * 156 * @see java.lang.Object#toString() 157 */ 158 @Override 159 public String toString() { 160 final StringBuilder sb = new StringBuilder().append("min= ").append(getMinimum()).append(", max= ").append(getMaximum()); 161 return sb.toString(); 162 } 163}