Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2002, 2013). All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.util.measurement;
 
A unit system for measurements. This class contains definitions of the most common SI units.

This class only support exponents for the base SI units in the range -64 to +63. Any operation which produces an exponent outside of this range will result in a Unit object with undefined exponents.

Author(s):
$Id: e97563d26244bd44b59b81febc33cdfb418729a8 $
Immutable:
 
 /*
  * This local class maintains the information about units. It can calculate new
  * units when two values are multiplied, divided, added or subtracted. <p> The
  * unit works with the 7 basic SI types + rad + up to 2^6 custom types. For each
  * type, the unit keeps a bit mask with the exponents of the basic types. Eg.
  * m/s is m = 1, s = -1. Multiplying one unit with another means that the bit
  * masks are added, dividing means that the bit masks are subtracted. <p> This
  * class can handle any reasonable combination of SI units. However, it will
  * always try to coerce results back into the basic set. E.g. when you do V*A
  * you should get W and not m2.kg/s3 . Only when the existing types do not match
  * does the unit fallback to the expanded form. <p> This class uses offset
  * arithmetic. This means that the exponents are stored in an long. The special
  * field is used for units that should not be arithmetically divided or
  * multiplied, like longitude and lattitude. These special units can however, be
  * divided and multiplied by the basic 7 constants of the SI, e.g. deg/s.
  */
 public class Unit {
 	private final static long	UNITYcreateType(0, 0, 0, 0, 0, 0, 0, 0, 0);
 	private final static long	ZERO		= 0x40L;
 	private final static long	MASK		= 0x7fL;
 	private final static int	m_SHIFT		= 0;
 	private final static int	s_SHIFT		= 7;
 	private final static int	kg_SHIFT	= 14;
 	private final static int	K_SHIFT		= 21;
 	private final static int	A_SHIFT		= 28;
 	private final static int	mol_SHIFT	= 35;
 	private final static int	cd_SHIFT	= 42;
 	private final static int	rad_SHIFT	= 49;
 	private final static int	x_SHIFT		= 56;
 	private final static long	x_MASK << ;
No Unit (Unity)
 
 	public final static Unit	unitynew Unit("");																											// Unity
 	/* SI Base Units */
The length unit meter (m)
 
 	public final static Unit	mnew Unit("m"createType(0, 0, 0, 0, 0, 0, 0, 0, 1));																		// Distance
 	// meter
 	
The time unit second (s)
 
 	public final static Unit	snew Unit("s"createType(0, 0, 0, 0, 0, 0, 0, 1, 0));																		// Time
 	// Seconds
 	// s
 	
The mass unit kilogram (kg)
 
 	public final static Unit	kgnew Unit("kg"createType(0, 0, 0, 0, 0, 0, 1, 0, 0));																		// Mass
 	// kilogram
 	// kg
 	
The temperature unit kelvin (K)
 
 	public final static Unit	Knew Unit("K"createType(0, 0, 0, 0, 0, 1, 0, 0, 0));																		// Temperature
 	// kelvin
 	// K
 	
The electric current unit ampere (A)
 
 	public final static Unit	Anew Unit("A"createType(0, 0, 0, 0, 1, 0, 0, 0, 0));																		// Current
 	// ampere
 	// A
 	
The amount of substance unit mole (mol)
 
 	public final static Unit	molnew Unit("mol"createType(0, 0, 0, 1, 0, 0, 0, 0, 0));																		// Substance
 	// mole
 	// mol
 	
The luminous intensity unit candela (cd)
 
 	public final static Unit	cdnew Unit("cd"createType(0, 0, 1, 0, 0, 0, 0, 0, 0));																		// Light
 	// candela
 	// cd
 	/* SI Derived Units */
The speed unit meter per second (m/s)
 
 	public final static Unit	m_snew Unit("m/s"createType(0, 0, 0, 0, 0, 0, 0, -1, 1));																		// Speed
 	// m/s
 	
The acceleration unit meter per second squared (m/s2)
 
	public final static Unit	m_s2new Unit("m/s2"createType(0, 0, 0, 0, 0, 0, 0, -2, 1));																	// Acceleration
	// m/s^2
The area unit square meter (m2)
	public final static Unit	m2new Unit("m2"createType(0, 0, 0, 0, 0, 0, 0, 0, 2));																		// Surface
	// m^2
The volume unit cubic meter (m3)
	public final static Unit	m3new Unit("m3"createType(0, 0, 0, 0, 0, 0, 0, 0, 3));																		// Volume
	// m^3
The frequency unit hertz (Hz).

hertz is expressed in SI units as 1/s

	public final static Unit	Hznew Unit("Hz"createType(0, 0, 0, 0, 0, 0, 0, -1, 0));																		// Frequency
	// 1/s
The force unit newton (N).

N is expressed in SI units as m·kg/s2

	public final static Unit	Nnew Unit("N"createType(0, 0, 0, 0, 0, 0, 1, -2, 1));																		// Force
	// newton
	// (m*kg)/s^2
The pressure unit pascal (Pa).

Pa is equal to N/m2 or is expressed in SI units as kg/m·s2

	public final static Unit	Panew Unit("Pa"createType(0, 0, 0, 0, 0, 0, 1, -2, -1));																		// Pressure
	// pascal
	// kg/(m*s^2)
The energy unit joule (J).

joule is equal to N·m or is expressed in SI units as m2·kg/s2

	public final static Unit	Jnew Unit("J"createType(0, 0, 0, 0, 0, 0, 1, -2, 2));																		// Energy
	// joule
	// (m^2*kg)/s^2
The power unit watt (W).

watt is equal to J/s or is expressed in SI units as m2·kg/s3

	public final static Unit	Wnew Unit("W"createType(0, 0, 0, 0, 0, 0, 1, -3, 2));																		// Power
	// watt
	// (m^2*kg)/s^3
The electric charge unit coulomb (C).

coulomb is expressed in SI units as s·A

	public final static Unit	Cnew Unit("C"createType(0, 0, 0, 0, 1, 0, 0, 1, 0));																		// Charge
	// coulumb
	// s*A
The electric potential difference unit volt (V).

volt is equal to W/A or is expressed in SI units as m2·kg/s3·A

	public final static Unit	Vnew Unit("V"createType(0, 0, 0, 0, -1, 0, 1, -3, 2));																		// El.
	// Potent.
	// volt
	// (m^2*kg)/(s^3*A)
The capacitance unit farad (F).

farad is equal to C/V or is expressed in SI units as s4·A2/m2·kg

	public final static Unit	Fnew Unit("F"createType(0, 0, 0, 0, 2, 0, -1, 4, -2));																		// Capacitance
	// farad
	// (s^4*A^2)/(m^2*kg)
The electric resistance unit ohm.

ohm is equal to V/A or is expressed in SI units as m2·kg/s3·A2

	public final static Unit	Ohmnew Unit("Ohm"createType(0, 0, 0, 0, -2, 0, 1, -3, 2));																	// Resistance
	// ohm
	// (m^2*kg)/(s^3*A^2)
The electric conductance unit siemens (S).

siemens is equal to A/V or is expressed in SI units as s3·A2/m2·kg

	public final static Unit	Snew Unit("S"createType(0, 0, 0, 0, 2, 0, -1, 3, -2));																		// Conductance
	// siemens
	// (s^3*A^2)/(m^2*kg)
The magnetic flux unit weber (Wb).

weber is equal to V·s or is expressed in SI units as m2·kg/s2·A

	public final static Unit	Wbnew Unit("Wb"createType(0, 0, 0, 0, -1, 0, 1, -2, 2));																		// Magn.
	// Flux
	// weber
	// (m^2*kg)/(s^2*A)
The magnetic flux density unit tesla (T).

tesla is equal to Wb/m2 or is expressed in SI units as kg/s2·A

	public final static Unit	Tnew Unit("T"createType(0, 0, 0, 0, -1, 0, 1, -2, 0));																		// Magn.
	// Flux
	// Dens.
	// tesla
	// kg/(s^2*A)
The illuminance unit lux (lx).

lux is expressed in SI units as cd/m2

	public final static Unit	lxnew Unit("lx"createType(0, 0, 1, 0, 0, 0, 0, 0, -2));																		// Illuminace
	// lux
	// cd/m^2
The absorbed dose unit gray (Gy).

Gy is equal to J/kg or is expressed in SI units as m2/s2

	public final static Unit	Gynew Unit("Gy"createType(0, 0, 0, 0, 0, 0, 0, -2, 2));																		// Absorbed
	// dose
	// gray
	// m^2/s^2
The catalytic activity unit katal (kat).

katal is expressed in SI units as mol/s

	public final static Unit	katnew Unit("kat"createType(0, 0, 0, 1, 0, 0, 0, -1, 0));																		// Catalytic
	// Act.
	// katal
	// mol/s
The angle unit radians (rad)
	public final static Unit	radnew Unit("rad"createType(0, 1, 0, 0, 0, 0, 0, 0, 0));																		// Angle
	// radians
	// rad
An array containing all units defined. The first seven items must be m, s, kg, K, A, mol, cd, rad in this order!
	private final static Unit[]	allUnitsnew Unit[] {};
	/* @GuardedBy("this") */
	private static Hashtable	base;
	private final String		name;
	private final long			type;

Creates a new Unit instance.

Parameters:
name the name of the Unit
type the type of the Unit
	private Unit(String namelong type) {
		if (name == null) {
			name = computeName(type);
		}
		this. = name;
		this. = type;
		// System.out.println( name + " " + Long.toHexString( type ) );
	}

Create a type field from the base SI unit exponent values.
	private static long createType(int _xint _radint _cdint _molint _Aint _Kint _kgint _sint _m) {
		return ((( + _m) & ) << ) | ((( + _s) & ) << ) | ((( + _kg) & ) << ) | ((( + _K) & ) << ) | ((( + _A) & ) << )
				| ((( + _mol) & ) << ) | ((( + _cd) & ) << ) | ((( + _rad) & ) << ) | (((long_x) << );
	}

Checks whether this Unit object is equal to the specified Unit object. The Unit objects are considered equal if their exponents are equal.

Parameters:
obj the Unit object that should be checked for equality
Returns:
true if the specified Unit object is equal to this Unit object.
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Unit)) {
			return false;
		}
		return ((Unitobj). == ;
	}

Returns the hash code for this object.

Returns:
This object's hash code.
	public int hashCode() {
		return 31 * 17 + (int) ( ^ ( >>> 32));
	}

Returns a new Unit that is the multiplication of this Unit and the Unit specified

Parameters:
that the Unit that will be multiplied with this Unit
Returns:
a new Unit that is the multiplication of this Unit and the Unit specified
Throws:
java.lang.RuntimeException if both Unit s are special
See also:
isSpecial()
	Unit mul(Unit that) {
		if (this.isSpecial() && that.isSpecial()) {
			throw new ArithmeticException("Cannot multiply " + this + " with " + that);
		}
		return find(this. -  + that.type);
	}

Returns a new Unit that is the division of this Unit and the Unit specified

Parameters:
that the Unit that this Unit will be divided with
Returns:
a new Unit that is the division of this Unit and the Unit specified
Throws:
java.lang.RuntimeException if both Unit s are special
See also:
isSpecial()
	Unit div(Unit that) {
		if (this.isSpecial() && that.isSpecial()) {
			if (this. == that.type) {
				return .;
			}
			throw new ArithmeticException("Cannot divide " + this + " by " + that);
		}
		return find(this. - that.type + );
	}

Returns a new Unit that is the addition of this Unit and the Unit specified.

Parameters:
that the Unit that should be added to this Unit
Returns:
a new Unit that is the addition of this Unit and the Unit specified.
Throws:
java.lang.RuntimeException if the two Unit s are not the same
	Unit add(Unit that) {
		if (!this.equals(that)) {
			throw new ArithmeticException("Cannot add " + this + " to " + that);
		}
		return this;
	}

Returns a new Unit that is the subtraction between this Unit and the Unit specified.

Parameters:
that the Unit that will be subtracted from this Unit
Returns:
a new Unit that is the subtraction between this Unit and the Unit specified.
Throws:
java.lang.RuntimeException if the Unit specified is not the same as this Unit
	Unit sub(Unit that) {
		if (!this.equals(that)) {
			throw new ArithmeticException("Cannot subtract " + that + " from " + this);
		}
		return this;
	}

Finds a Unit based on a type. If the Unit is not found, it will be created and added to the list of all units under a null name.

Parameters:
type the type of the Unit to find
Returns:
the Unit
	static synchronized Unit find(long type) {
		if ( == null) {
			int size = .;
			 = new Hashtable(size << 1);
			for (int i = 0; i < sizei++) {
			}
		}
		Unit unit = new Unit(nulltype);
		Unit out = (Unit.get(unit);
		if (out == null) {
			.put(unitunit);
			out = unit;
		}
		return out;
	}

Returns a String object representing the Unit

Returns:
A String object representing the Unit
	public String toString() {
		return ;
	}
	private static String computeName(long type) {
		int _m = (int) (((type >> ) & ) - );
		int _s = (int) (((type >> ) & ) - );
		int _kg = (int) (((type >> ) & ) - );
		int _K = (int) (((type >> ) & ) - );
		int _A = (int) (((type >> ) & ) - );
		int _mol = (int) (((type >> ) & ) - );
		int _cd = (int) (((type >> ) & ) - );
		int _rad = (int) (((type >> ) & ) - );
		StringBuffer numerator = new StringBuffer();
		StringBuffer denominator = new StringBuffer();
		addSIname(_m"m"numeratordenominator);
		addSIname(_s"s"numeratordenominator);
		addSIname(_kg"kg"numeratordenominator);
		addSIname(_K"K"numeratordenominator);
		addSIname(_A"A"numeratordenominator);
		addSIname(_mol"mol"numeratordenominator);
		addSIname(_cd"cd"numeratordenominator);
		addSIname(_rad"rad"numeratordenominator);
		if (denominator.length() > 0) {
			if (numerator.length() == 0) {
				numerator.append("1");
			}
			numerator.append("/");
			numerator.append(denominator.toString());
		}
		return numerator.toString();
	}
	private static void addSIname(int siString nameStringBuffer numeratorStringBuffer denominator) {
		if (si != 0) {
			StringBuffer sb = (si > 0) ? numerator : denominator;
			if (sb.length() > 0) {
				sb.append("*");
			}
			sb.append(name);
			int power = Math.abs(si);
			if (power > 1) {
				sb.append("^");
				sb.append(power);
			}
		}
	}

Checks whether the unit has a special type, i.e. not a SI unit.

Returns:
true if the type is special, otherwise false.
	private boolean isSpecial() {
		return ( & ) != 0;
	}
New to GrepCode? Check out our FAQ X