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.unit; 031 032import java.util.Map; 033import java.util.Objects; 034 035import javax.measure.Dimension; 036import javax.measure.Quantity; 037import javax.measure.Unit; 038import javax.measure.UnitConverter; 039 040import tec.uom.lib.common.function.UnitConverterSupplier; 041import tec.uom.se.AbstractUnit; 042 043/** 044 * <p> 045 * This class represents the units derived from other units using {@linkplain UnitConverter converters}. 046 * </p> 047 * 048 * <p> 049 * Examples of transformed units:<code> 050 * CELSIUS = KELVIN.shift(273.15); 051 * FOOT = METRE.multiply(3048).divide(10000); 052 * MILLISECOND = MILLI(SECOND); 053 * </code> 054 * </p> 055 * 056 * <p> 057 * Transformed units have no symbol. But like any other units, they may have labels attached to them (see 058 * {@link javax.measure.format.UnitFormat#label(Unit, String) UnitFormat.label} 059 * </p> 060 * 061 * <p> 062 * Instances of this class are created through the {@link AbstractUnit#transform} method. 063 * </p> 064 * 065 * @param <Q> 066 * The type of the quantity measured by this unit. 067 * 068 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 069 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 070 * @version 0.7, December 28, 2015 071 */ 072public final class TransformedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> implements UnitConverterSupplier { 073 074 /** 075 * 076 */ 077 private static final long serialVersionUID = 1L; 078 079 /** 080 * Holds the parent unit (always a system unit). 081 */ 082 private final AbstractUnit<Q> parentUnit; 083 084 /** 085 * Holds the converter to the parent unit. 086 */ 087 private final UnitConverter converter; 088 089 /** 090 * Holds the symbol. 091 */ 092 private String symbol; 093 094 /** 095 * Creates a transformed unit from the specified system unit. using the parent as symbol 096 * 097 * @param parentUnit 098 * the system unit from which this unit is derived. 099 * @param converter 100 * the converter to the parent units. 101 * @throws IllegalArgumentException 102 * if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit} 103 */ 104 public TransformedUnit(Unit<Q> parentUnit, UnitConverter unitConverter) { 105 this(parentUnit.getSymbol(), parentUnit, unitConverter); 106 } 107 108 public TransformedUnit(String symbol, Unit<Q> parentUnit, UnitConverter unitConverter) { 109 if (parentUnit instanceof AbstractUnit) { 110 final AbstractUnit<Q> abParent = (AbstractUnit<Q>) parentUnit; 111 if (!abParent.isSystemUnit()) { 112 throw new IllegalArgumentException("The parent unit: " + abParent + " is not a system unit"); 113 } 114 this.parentUnit = abParent; 115 this.converter = unitConverter; 116 this.symbol = symbol; 117 // this.symbol = symbol; //TODO see 118 // https://github.com/unitsofmeasurement/uom-se/issues/54 119 } else { 120 throw new IllegalArgumentException("The parent unit: " + parentUnit + " is not an abstract unit."); 121 } 122 } 123 124 @Override 125 public Dimension getDimension() { 126 return parentUnit.getDimension(); 127 } 128 129 @Override 130 public UnitConverter getSystemConverter() { 131 return parentUnit.getSystemConverter().concatenate(converter); 132 } 133 134 @Override 135 public UnitConverter getConverter() { 136 return converter; 137 } 138 139 @Override 140 public AbstractUnit<Q> toSystemUnit() { 141 return parentUnit.getSystemUnit(); 142 } 143 144 @Override 145 public Map<? extends Unit<?>, Integer> getBaseUnits() { 146 return parentUnit.getBaseUnits(); 147 } 148 149 @Override 150 public int hashCode() { 151 return Objects.hash(parentUnit, converter); 152 } 153 154 @Override 155 public boolean equals(Object obj) { 156 if (this == obj) { 157 return true; 158 } 159 if (obj instanceof TransformedUnit) { 160 TransformedUnit<?> other = (TransformedUnit<?>) obj; 161 return Objects.equals(parentUnit, other.parentUnit) && Objects.equals(converter, other.converter); 162 } 163 return false; 164 } 165 166 @Override 167 public String getSymbol() { 168 return symbol; 169 } 170 171 public Unit<Q> getParentUnit() { 172 return parentUnit; 173 } 174}