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