001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2020, Units of Measurement project. 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.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 AnnotatedUnit#of(Unit, 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:werner@units.tech">Werner Keil</a> 056 * @version 1.5, March 30, 2019 057 * @since 1.0 058 */ 059public final class AnnotatedUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> { 060 061 /** 062 * 063 */ 064 private static final long serialVersionUID = -5676462045106887728L; 065 066 /** 067 * Holds the actual unit. 068 */ 069 private final Unit<Q> actualUnit; 070 071 /** 072 * Holds the annotation. 073 */ 074 private final String annotation; 075 076 /** 077 * Creates an annotated unit equivalent to the specified unit. 078 * 079 * @param actualUnit 080 * the unit to be annotated. 081 * @param annotation 082 * the annotation. 083 * @return the annotated unit. 084 */ 085 public AnnotatedUnit(Unit<Q> actualUnit, String annotation) { 086 this.actualUnit = actualUnit instanceof AnnotatedUnit ? ((AnnotatedUnit<Q>) actualUnit).actualUnit : actualUnit; 087 this.annotation = annotation; 088 } 089 090 /** 091 * Returns the actual unit of this annotated unit (never an annotated unit itself). 092 * 093 * @return the actual unit. 094 */ 095 public Unit<Q> getActualUnit() { 096 return actualUnit; 097 } 098 099 /** 100 * Returns the annotation of this annotated unit. 101 * 102 * @return the annotation. 103 */ 104 public String getAnnotation() { 105 return annotation; 106 } 107 108 @Override 109 public String getSymbol() { 110 return actualUnit.getSymbol(); 111 } 112 113 @Override 114 public Map<? extends Unit<?>, Integer> getBaseUnits() { 115 return actualUnit.getBaseUnits(); 116 } 117 118 @Override 119 public Unit<Q> toSystemUnit() { 120 return actualUnit.getSystemUnit(); 121 } 122 123 @Override 124 public Dimension getDimension() { 125 return actualUnit.getDimension(); 126 } 127 128 @Override 129 public UnitConverter getSystemConverter() { 130 return ((AbstractUnit<Q>)actualUnit).getSystemConverter(); 131 } 132 133 @Override 134 public int hashCode() { 135 return Objects.hash(actualUnit, annotation); 136 } 137 138 @Override 139 public boolean equals(Object obj) { 140 if (this == obj) { 141 return true; 142 } 143 if (obj instanceof AnnotatedUnit<?>) { 144 AnnotatedUnit<?> other = (AnnotatedUnit<?>) obj; 145 return Objects.equals(actualUnit, other.actualUnit) && Objects.equals(annotation, other.annotation); 146 } 147 return false; 148 } 149 150 /** 151 * Creates an annotated unit equivalent to the specified unit. 152 * 153 * @param actualUnit 154 * the unit to be annotated. 155 * @param annotation 156 * the annotation. 157 * @return the annotated unit. 158 */ 159 public static <Q extends Quantity<Q>> AnnotatedUnit<Q> of(Unit<Q> actualUnit, String annotation) { 160 return new AnnotatedUnit<>(actualUnit, annotation); 161 } 162}