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.unit; 031 032import java.util.Map; 033 034import javax.measure.Dimension; 035import javax.measure.Quantity; 036import javax.measure.Unit; 037import javax.measure.UnitConverter; 038 039import tec.units.ri.AbstractUnit; 040 041/** 042 * <p> 043 * This class represents units used in expressions to distinguish between quantities of a different nature but of the same dimensions. 044 * </p> 045 * 046 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 047 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 048 * @version 0.2, May 22, 2014 049 */ 050public final class AlternateUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> { 051 052 /** 053 * 054 */ 055 // private static final long serialVersionUID = 4696690756456282705L; 056 057 /** 058 * Holds the parent unit (a system unit). 059 */ 060 private final AbstractUnit<?> parentUnit; 061 062 /** 063 * Holds the symbol for this unit. 064 */ 065 private final String symbol; 066 067 /** 068 * Creates an alternate unit for the specified system unit identified by the specified name and symbol. 069 * 070 * @param parent 071 * the system unit from which this alternate unit is derived. 072 * @param symbol 073 * the symbol for this alternate unit. 074 * @throws IllegalArgumentException 075 * if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit} 076 */ 077 public AlternateUnit(AbstractUnit<?> parentUnit, String symbol) { 078 if (!parentUnit.isSystemUnit()) 079 throw new IllegalArgumentException("The parent unit: " + parentUnit + " is not an unscaled SI unit"); 080 this.parentUnit = (parentUnit instanceof AlternateUnit) ? ((AlternateUnit<?>) parentUnit).getParentUnit() : parentUnit; 081 this.symbol = symbol; 082 083 // Checks if the symbol is associated to a different unit. TODO verify 084 // if we want these checks 085 /* 086 * synchronized (AbstractUnit.SYMBOL_TO_UNIT) { AbstractUnit<?> unit = 087 * (AbstractUnit<?>) AbstractUnit.SYMBOL_TO_UNIT.get(symbol); if (unit 088 * == null) { AbstractUnit.SYMBOL_TO_UNIT.put(symbol, this); return; } 089 * if (unit instanceof AlternateUnit<?>) { AlternateUnit<?> existingUnit 090 * = (AlternateUnit<?>) unit; if 091 * (symbol.equals(existingUnit.getSymbol()) && 092 * this.parentUnit.equals(existingUnit.parentUnit)) return; // OK, same 093 * unit. } throw new IllegalArgumentException("Symbol " + symbol + 094 * " is associated to a different unit"); } 095 */ 096 } 097 098 /** 099 * Creates an alternate unit for the specified system unit identified by the specified name and symbol. 100 * 101 * @param parent 102 * the system unit from which this alternate unit is derived. 103 * @param symbol 104 * the symbol for this alternate unit. 105 * @throws IllegalArgumentException 106 * if the specified parent unit is not an {@link AbstractUnit#isSystemUnit() system unit} 107 * @throws ClassCastException 108 * if parentUnit is not a valid {@link Unit} implementation 109 */ 110 public AlternateUnit(Unit<?> parentUnit, String symbol) { 111 this((AbstractUnit<?>) parentUnit, symbol); 112 } 113 114 /** 115 * Returns the parent unit of this alternate unit, always a system unit and never an alternate unit. 116 * 117 * @return the parent unit. 118 */ 119 public AbstractUnit<?> getParentUnit() { 120 return parentUnit; 121 } 122 123 @Override 124 public String getSymbol() { 125 return symbol; 126 } 127 128 @Override 129 public Dimension getDimension() { 130 return parentUnit.getDimension(); 131 } 132 133 @Override 134 public UnitConverter getSystemConverter() { 135 return parentUnit.getSystemConverter(); 136 } 137 138 @Override 139 public AbstractUnit<Q> toSystemUnit() { 140 return this; // Alternate units are SI units. 141 } 142 143 @Override 144 public Map<? extends Unit<?>, Integer> getBaseUnits() { 145 return parentUnit.getBaseUnits(); 146 } 147 148 @Override 149 public int hashCode() { 150 return symbol.hashCode(); 151 } 152 153 @Override 154 public boolean equals(Object obj) { 155 if (this == obj) 156 return true; 157 if (!(obj instanceof AlternateUnit)) 158 return false; 159 AlternateUnit<?> that = (AlternateUnit<?>) obj; 160 return this.parentUnit.equals(that.parentUnit) && this.symbol.equals(that.symbol); 161 } 162 163}