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 javax.measure.Dimension; 033import javax.measure.Quantity; 034import javax.measure.Unit; 035import javax.measure.UnitConverter; 036 037import tec.uom.se.AbstractUnit; 038 039import java.util.Map; 040import java.util.Objects; 041 042/** 043 * <p> 044 * This class represents an annotated unit. 045 * </p> 046 * 047 * <p> 048 * Instances of this class are created through the {@link AbstractUnit#annotate(String)} method. 049 * </p> 050 * 051 * @param <Q> 052 * The type of the quantity measured by this unit. 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 0.3, June 09, 2014 057 */ 058public final class AnnotatedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> { 059 060 /** 061 * 062 */ 063 private static final long serialVersionUID = -5676462045106887728L; 064 065 /** 066 * Holds the actual unit. 067 */ 068 private final AbstractUnit<Q> actualUnit; 069 070 /** 071 * Holds the annotation. 072 */ 073 private final String annotation; 074 075 /** 076 * Creates an annotated unit equivalent to the specified unit. 077 * 078 * @param actualUnit 079 * the unit to be annotated. 080 * @param annotation 081 * the annotation. 082 * @return the annotated unit. 083 */ 084 public AnnotatedUnit(AbstractUnit<Q> actualUnit, String annotation) { 085 this.actualUnit = (actualUnit instanceof AnnotatedUnit) ? ((AnnotatedUnit<Q>) actualUnit).actualUnit : actualUnit; 086 this.annotation = annotation; 087 } 088 089 /** 090 * Returns the actual unit of this annotated unit (never an annotated unit itself). 091 * 092 * @return the actual unit. 093 */ 094 public AbstractUnit<Q> getActualUnit() { 095 return actualUnit; 096 } 097 098 /** 099 * Returns the annotqtion of this annotated unit. 100 * 101 * @return the annotation. 102 */ 103 public String getAnnotation() { 104 return annotation; 105 } 106 107 @Override 108 public String getSymbol() { 109 return actualUnit.getSymbol(); 110 } 111 112 @Override 113 public Map<? extends Unit<?>, Integer> getBaseUnits() { 114 return actualUnit.getBaseUnits(); 115 } 116 117 @Override 118 public AbstractUnit<Q> toSystemUnit() { 119 return actualUnit.getSystemUnit(); 120 } 121 122 @Override 123 public Dimension getDimension() { 124 return actualUnit.getDimension(); 125 } 126 127 @Override 128 public UnitConverter getSystemConverter() { 129 return actualUnit.getSystemConverter(); 130 } 131 132 @Override 133 public int hashCode() { 134 return Objects.hash(actualUnit, annotation); 135 } 136 137 @Override 138 public boolean equals(Object obj) { 139 if (this == obj) { 140 return true; 141 } 142 if (obj instanceof AnnotatedUnit<?>) { 143 AnnotatedUnit<?> other = (AnnotatedUnit<?>) obj; 144 return Objects.equals(actualUnit, other.actualUnit) && Objects.equals(annotation, other.annotation); 145 } 146 return false; 147 } 148}