001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2018, 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, Indriya 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.units.indriya.unit; 031 032import javax.measure.Dimension; 033import javax.measure.Quantity; 034import javax.measure.Unit; 035import javax.measure.UnitConverter; 036 037import tech.units.indriya.AbstractConverter; 038import tech.units.indriya.AbstractUnit; 039import tech.units.indriya.quantity.QuantityDimension; 040 041import java.util.Map; 042 043/** 044 * <p> 045 * This class represents the building blocks on top of which all others physical units are created. Base units are always unscaled SI units. 046 * </p> 047 * 048 * <p> 049 * When using the {@link tech.units.indriya.spi.StandardModel standard model}, all seven <b>SI</b> base units are dimensionally independent. 050 * </p> 051 * 052 * @see <a href="http://en.wikipedia.org/wiki/SI_base_unit"> Wikipedia: SI base unit</a> 053 * 054 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 055 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 056 * @version 1.2, August 06, 2017 057 * @since 1.0 058 */ 059public final class BaseUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> { 060 061 /** 062 * 063 */ 064 private static final long serialVersionUID = 1721629233768215930L; 065 066 /** 067 * Holds the symbol. 068 */ 069 private final String symbol; 070 071 /** 072 * Holds the base unit dimension. 073 */ 074 private final Dimension dimension; 075 076 private Q quantityType; 077 078 protected Q getQuantityType() { 079 return quantityType; 080 } 081 082 /** 083 * Creates a base unit having the specified symbol and dimension. 084 * 085 * @param symbol 086 * the symbol of this base unit. 087 */ 088 public BaseUnit(String symbol, Dimension dimension, Q quant) { 089 this.symbol = symbol; 090 this.dimension = dimension; 091 quantityType = quant; 092 } 093 094 /** 095 * Creates a base unit having the specified symbol and dimension. 096 * 097 * @param symbol 098 * the symbol of this base unit. 099 */ 100 public BaseUnit(String symbol, Dimension dimension) { 101 this.symbol = symbol; 102 this.dimension = dimension; 103 } 104 105 /** 106 * Creates a base unit having the specified symbol. 107 * 108 * @param symbol 109 * the symbol of this base unit. 110 */ 111 public BaseUnit(String symbol) { 112 this.symbol = symbol; 113 this.dimension = QuantityDimension.NONE; 114 } 115 116 /** 117 * Creates a base unit having the specified symbol and name. 118 * 119 * @param symbol 120 * the symbol of this base unit. 121 * @param name 122 * the name of this base unit. 123 * @throws IllegalArgumentException 124 * if the specified symbol is associated to a different unit. 125 */ 126 public BaseUnit(String symbol, String name) { 127 this(symbol); 128 this.name = name; 129 } 130 131 @Override 132 public String getSymbol() { 133 return symbol; 134 } 135 136 @Override 137 public Unit<Q> toSystemUnit() { 138 return this; 139 } 140 141 @Override 142 public UnitConverter getSystemConverter() throws UnsupportedOperationException { 143 return AbstractConverter.IDENTITY; 144 } 145 146 @Override 147 public Dimension getDimension() { 148 return dimension; 149 } 150 151 @Override 152 public final boolean equals(Object obj) { 153 if (this == obj) 154 return true; 155 if (obj instanceof BaseUnit) { 156 BaseUnit<?> thatUnit = (BaseUnit<?>) obj; 157 return this.symbol.equals(thatUnit.symbol) && this.dimension.equals(thatUnit.dimension); 158 } 159 if (obj instanceof AbstractUnit) { 160 return AbstractUnit.Equalizer.areEqual(this, (AbstractUnit) obj); 161 } else { 162 return false; 163 } 164 } 165 166 @Override 167 public final int hashCode() { 168 return symbol.hashCode(); 169 } 170 171 @Override 172 public Map<? extends AbstractUnit<Q>, Integer> getBaseUnits() { 173 // TODO Shall we return null, empty list or what (e.g. Optional in SE 174 // 8)? 175 return null; 176 } 177 178 179}