001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana. 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 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 javax.measure.test.quantity; 031 032import javax.measure.Quantity; 033import javax.measure.Unit; 034import javax.measure.quantity.Area; 035import javax.measure.test.unit.AreaUnit; 036import javax.measure.test.unit.DistanceUnit; 037import javax.measure.test.unit.VolumeUnit; 038 039/** 040 * @author Werner Keil 041 * @version 0.7 042 */ 043public class AreaQuantity extends TestQuantity<Area> implements Area { 044 public AreaQuantity() { 045 super(Area.class); 046 } 047 048 public AreaQuantity(double val, AreaUnit un) { 049 this(); 050 value = val; 051 unit = un; 052 scalar = val * unit.getMultFactor(); 053 } 054 055 public AreaQuantity(Number val, Unit u) { 056 this(val.doubleValue(), (AreaUnit) u); 057 } 058 059 public AreaQuantity add(AreaQuantity d1) { 060 AreaQuantity dn = new AreaQuantity(); 061 Object o = super.add(dn, this, d1, AreaUnit.REF_UNIT); 062 return (AreaQuantity) o; 063 } 064 065 public AreaQuantity subtract(AreaQuantity d1) { 066 AreaQuantity dn = new AreaQuantity(); 067 Object o = super.subtract(dn, this, d1, AreaUnit.REF_UNIT); 068 return (AreaQuantity) o; 069 } 070 071 public boolean eq(AreaQuantity d1) { 072 return super.eq(d1); 073 } 074 075 public boolean ne(AreaQuantity d1) { 076 return super.ne(d1); 077 } 078 079 public boolean gt(AreaQuantity d1) { 080 return super.gt(d1); 081 } 082 083 public boolean lt(AreaQuantity d1) { 084 return super.lt(d1); 085 } 086 087 public boolean ge(AreaQuantity d1) { 088 return super.ge(d1); 089 } 090 091 public boolean le(AreaQuantity d1) { 092 return super.le(d1); 093 } 094 095 public AreaQuantity multiply(double v) { 096 return new AreaQuantity(value * v, (AreaUnit) unit); 097 } 098 099 public AreaQuantity divide(double v) { 100 return new AreaQuantity(value / v, (AreaUnit) unit); 101 } 102 103 // mixed type operations 104 105 public DistanceQuantity divide(DistanceQuantity d1) { 106 AreaQuantity dq0 = convert(AreaUnit.sqmetre); 107 DistanceQuantity dq1 = d1.convert(DistanceUnit.m); 108 return new DistanceQuantity(dq0.value / dq1.value, DistanceUnit.m); 109 } 110 111 public VolumeQuantity multiply(DistanceQuantity d1) { 112 AreaQuantity dq0 = convert(AreaUnit.sqmetre); 113 DistanceQuantity dq1 = d1.convert(DistanceUnit.m); 114 return new VolumeQuantity(dq0.value * dq1.value, VolumeUnit.cumetre); 115 } 116 117 public AreaQuantity convert(AreaUnit newUnit) { 118 return new AreaQuantity(scalar / newUnit.getMultFactor(), newUnit); 119 } 120 121 public String showInUnits(AreaUnit u, int precision) { 122 return super.showInUnits(u, precision); 123 } 124 125 public Quantity<Area> multiply(Number that) { 126 // TODO Auto-generated method stub 127 return null; 128 } 129 130 public Quantity<?> divide(Quantity<?> that) { 131 // TODO Auto-generated method stub 132 return null; 133 } 134 135 public Area to(Unit<Area> unit) { 136 // TODO Auto-generated method stub 137 return null; 138 } 139 140 public Quantity<Area> subtract(Quantity<Area> that) { 141 // TODO Auto-generated method stub 142 return null; 143 } 144 145 public Quantity<Area> add(Quantity<Area> that) { 146 return add((AreaQuantity) that); 147 } 148 149 public Quantity<Area> divide(Number that) { 150 return divide(that.doubleValue()); 151 } 152 153 public Quantity<Area> inverse() { 154 // TODO Auto-generated method stub 155 return null; 156 } 157 158 public Quantity<?> multiply(Quantity<?> that) { 159 if (that.getClass().equals(DistanceQuantity.class)) { 160 return multiply((DistanceQuantity) that); 161 } 162 return null; 163 } 164 165 @SuppressWarnings({ "unchecked", "rawtypes" }) 166 public final <T extends Quantity<T>> Quantity<T> asType(Class<T> type) throws ClassCastException { 167 this.getUnit().asType(type); // Raises ClassCastException is dimension 168 // mismatches. 169 return (Quantity) this; 170 } 171 172 @Override 173 public Quantity<Area> negate() { 174 return new AreaQuantity(-value, getUnit()); 175 } 176}