001/* The base unit of a unit category. 002 003 Copyright (c) 2001-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.data.unit; 028 029import java.util.Iterator; 030 031import ptolemy.data.ScalarToken; 032import ptolemy.data.Token; 033import ptolemy.data.expr.Parameter; 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.NameDuplicationException; 036import ptolemy.kernel.util.NamedObj; 037 038/////////////////////////////////////////////////////////////////// 039//// BaseUnit 040 041/** 042 The base unit of a unit category. The category of a base unit is specified 043 by a unit category property. 044 045 @author Xiaojun Liu 046 @version $Id$ 047 @since Ptolemy II 2.0 048 @Pt.ProposedRating Red (liuxj) 049 @Pt.AcceptedRating Red (liuxj) 050 */ 051public class BaseUnit extends Parameter { 052 /** Construct a base unit with the given name contained by 053 * the specified entity. The container argument must not be null, or a 054 * NullPointerException will be thrown. This attribute will use the 055 * workspace of the container for synchronization and version counts. 056 * If the name argument is null, then the name is set to the empty string. 057 * Increment the version of the workspace. 058 * @param container The container. 059 * @param name The name of this attribute. 060 * @exception IllegalActionException If the attribute is not of an 061 * acceptable class for the container, or if the name contains a period. 062 * @exception NameDuplicationException If the name coincides with 063 * an attribute already in the container. 064 */ 065 public BaseUnit(NamedObj container, String name) 066 throws IllegalActionException, NameDuplicationException { 067 super(container, name); 068 } 069 070 /////////////////////////////////////////////////////////////////// 071 //// public methods //// 072 073 /** Get the token contained by this base unit. The token contains 074 * the unit information specified by the unit category property. 075 * Calling this method 076 * will trigger evaluation of the expression, if the value has been 077 * given by setExpression(). Notice the evaluation of the expression 078 * can trigger an exception if the expression is not valid, or if the 079 * result of the expression violates type constraints specified by 080 * setTypeEquals() or setTypeAtMost(), or if the result of the expression 081 * is null and there are other variables that depend on this one. 082 * The returned value will be null if neither an expression nor a 083 * token has been set, or either has been set to null. 084 * @return The token contained by this variable converted to the 085 * type of this variable, or null if there is none. 086 * @exception IllegalActionException If the expression cannot 087 * be parsed or cannot be evaluated, or if the result of evaluation 088 * violates type constraints, or if the result of evaluation is null 089 * and there are variables that depend on this one. 090 */ 091 @Override 092 public Token getToken() throws IllegalActionException { 093 Token token = super.getToken(); 094 095 if (token != _token) { 096 Iterator attributes = attributeList(UnitCategory.class).iterator(); 097 098 if (attributes.hasNext()) { 099 //UnitCategory category = (UnitCategory) attributes.next(); 100 String name = getName(); 101 UnitUtilities.registerUnitCategory(name); 102 103 int index = UnitUtilities.getUnitCategoryIndex(name); 104 105 if (index >= 0) { 106 ((ScalarToken) token).setUnitCategory(index); 107 } 108 } 109 110 _token = token; 111 } 112 113 return _token; 114 } 115 116 /////////////////////////////////////////////////////////////////// 117 //// private variables //// 118 // The token that has unit exponents set correctly. 119 private Token _token = null; 120}