001/* 002 * Units of Measurement Enum Implementation 003 * Copyright © 2005-2021, Werner Keil and others. 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-385, Unit-API nor the names of their 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 tech.uom.impl.enums.quantity; 031 032import static java.lang.Double.NaN; 033 034import javax.measure.Quantity; 035import javax.measure.Unit; 036import javax.measure.quantity.Time; 037 038import tech.uom.impl.enums.AbstractQuantity; 039import tech.uom.impl.enums.format.UnitStyle; 040import tech.uom.impl.enums.unit.TimeUnit; 041 042/** 043 * @author Werner Keil 044 * @version 0.8, $Date: 2018-07-21 $ 045 */ 046public class TimeQuantity extends AbstractQuantity<Time> implements Time { 047 private final double scalar; // value in reference unit 048 049 private final Double value; // value in unit (Unit unit) 050 private final TimeUnit unit; 051 052 public TimeQuantity(Number val, TimeUnit un) { 053 value = val.doubleValue(); 054 unit = un; 055 if (val != null && un != null) { 056 scalar = val.doubleValue() * un.getFactor(); 057 } else 058 scalar = NaN; 059 } 060 061 public TimeQuantity(Number val, @SuppressWarnings("rawtypes") Unit un) { 062 this(val.doubleValue(), (TimeUnit) un); 063 } 064 065 public boolean isZero() { 066 return (value != null) && 0d == (value.doubleValue()); 067 } 068 069 public TimeQuantity add(TimeQuantity d1) { 070 final TimeQuantity dn = new TimeQuantity(Double.valueOf(this.value.doubleValue() + d1.value.doubleValue()), 071 this.unit); 072 return dn; 073 } 074 075 public TimeQuantity subtract(TimeQuantity d1) { 076 final TimeQuantity dn = new TimeQuantity(this.value.doubleValue() - d1.value.doubleValue(), this.unit); 077 return dn; 078 } 079 080 public boolean eq(TimeQuantity dq) { 081 return dq != null && dq.getValue().equals(getValue()) && dq.getUnit().equals(getUnit()) 082 && dq.getScalar().equals(getScalar()); 083 } 084 085 public boolean ne(TimeQuantity d1) { 086 return ne((TimeQuantity) d1); 087 } 088 089 public boolean gt(TimeQuantity d1) { 090 return gt((TimeQuantity) d1); 091 } 092 093 public boolean lt(TimeQuantity d1) { 094 return lt((TimeQuantity) d1); 095 } 096 097 public boolean ge(TimeQuantity d1) { 098 return ge((TimeQuantity) d1); 099 } 100 101 public boolean le(TimeQuantity d1) { 102 return le((TimeQuantity) d1); 103 } 104 105 public TimeQuantity divide(Double v) { 106 return new TimeQuantity(value.doubleValue() / v.doubleValue(), unit); 107 } 108 109 protected TimeQuantity convert(TimeUnit newUnit) { 110 return new TimeQuantity(value.doubleValue() * (this.unit.getFactor() / newUnit.getFactor()), newUnit); 111 } 112 113 public Number getScalar() { 114 return scalar; 115 } 116 117 public String toString(boolean withUnit, boolean withSpace, int precision) { 118 final StringBuilder sb = new StringBuilder(); 119 sb.append(getValue()); 120 if (withUnit) { 121 if (withSpace) 122 sb.append(" "); 123 sb.append(getUnit().getSymbol()); 124 } 125 return sb.toString(); 126 } 127 128 public String showInUnit(Unit<?> u, int precision, UnitStyle style) { 129 return showInUnit(u, value, precision, style); 130 } 131 132 public Number getValue() { 133 return value; 134 } 135 136 public Unit<Time> getUnit() { 137 return unit; 138 } 139 140 public Quantity<Time> multiply(Number that) { 141 return new TimeQuantity(value.doubleValue() * that.doubleValue(), unit); 142 } 143 144 public Quantity<Time> to(Unit<Time> unit) { 145 if (unit instanceof TimeUnit) { 146 return convert((TimeUnit) unit); 147 } else { 148 throw new ArithmeticException("Cannot convert " + this.unit + " to " + unit); 149 } 150 } 151 152 public boolean eq(AbstractQuantity<Time> dq) { 153 return eq((TimeQuantity) dq); 154 } 155 156 public Quantity<?> divide(Quantity<?> that) { 157 // TODO Auto-generated method stub 158 return null; 159 } 160 161 public Quantity<Time> subtract(Quantity<Time> that) { 162 // TODO Auto-generated method stub 163 return null; 164 } 165 166 public Quantity<Time> add(Quantity<Time> that) { 167 // TODO Auto-generated method stub 168 return null; 169 } 170 171 public Quantity<Time> divide(Number that) { 172 // TODO Auto-generated method stub 173 return null; 174 } 175 176 public Quantity<Time> inverse() { 177 // TODO Auto-generated method stub 178 return null; 179 } 180 181 public Quantity<?> multiply(Quantity<?> that) { 182 return new TimeQuantity(value.doubleValue() * that.getValue().doubleValue(), unit); 183 } 184 185 public int compareTo(Quantity<Time> o) { 186 // TODO Auto-generated method stub 187 return 0; 188 } 189 190 @Override 191 public Quantity<Time> negate() { 192 return new TimeQuantity(-value, unit); 193 } 194}