📄 unit.java
字号:
/** * Checks whether this <tt>Unit</tt> object is equal to the * specified <tt>Unit</tt> object. The <tt>Unit</tt> objects * are considered equal if their exponents are equal. * * @param obj the <tt>Unit</tt> object that should be checked * for equality * * @return true if the specified <tt>Unit</tt> object is equal * to this <tt>Unit</tt> object. */ public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Unit)) { return false; } return ((Unit)obj).type == type; } /** * Returns the hash code for this object. * * @return This object's hash code. */ public int hashCode() { return (int)((type >>> 32) ^ type); } /** * Returns a new <tt>Unit</tt> that is the multiplication of this * <tt>Unit</tt> and the <tt>Unit</tt> specified * * @param that the <tt>Unit</tt> that will be multiplied with this <tt>Unit</tt> * * @return a new <tt>Unit</tt> that is the multiplication of this * <tt>Unit</tt> and the <tt>Unit</tt> specified * * @exception RuntimeException if both <tt>Unit</tt>s are special * * @see Unit#isSpecial */ Unit mul(Unit that) { if (this.isSpecial() && that.isSpecial()) { throw new ArithmeticException("Cannot multiply " + this + " with " + that); } return find(this.type - UNITY + that.type); } /** * Returns a new <tt>Unit</tt> that is the division of this * <tt>Unit</tt> and the <tt>Unit</tt> specified * * @param that the <tt>Unit</tt> that this <tt>Unit</tt> * will be divided with * @return a new <tt>Unit</tt> that is the division of this * <tt>Unit</tt> and the <tt>Unit</tt> specified * * @exception RuntimeException if both <tt>Unit</tt>s are special * * @see Unit#isSpecial */ Unit div(Unit that) { if (this.isSpecial() && that.isSpecial()) { if (this.type == that.type) { return Unit.unity; } throw new ArithmeticException("Cannot divide " + this + " by " + that); } return find(this.type - that.type + UNITY); } /** * Returns a new <tt>Unit</tt> that is the addition of this * <tt>Unit</tt> and the <tt>Unit</tt> specified. * * @param that the <tt>Unit</tt> that should be added to this <tt>Unit</tt> * * @return a new <tt>Unit</tt> that is the addition of this * <tt>Unit</tt> and the <tt>Unit</tt> specified. * * @exception RuntimeException if the two <tt>Unit</tt>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 <tt>Unit</tt> that is the subtraction between * this <tt>Unit</tt> and the <tt>Unit</tt> specified. * * @param that the <tt>Unit</tt> that will be subtracted from this * <tt>Unit</tt> * @return a new <tt>Unit</tt> that is the subtraction between * this <tt>Unit</tt> and the <tt>Unit</tt> specified. * * @return RuntimeException if the <tt>Unit</tt> specified is not the same * as this <tt>Unit</tt> */ Unit sub(Unit that) { if (!this.equals(that)) { throw new ArithmeticException("Cannot subtract " + that + " from " + this); } return this; } /** * Finds a <tt>Unit</tt> based on a type. If the <tt>Unit</tt> * is not found, it will be created and added to the list of all * units under a null name. * * @param type the type of the <tt>Unit</tt> to find * * @return the <tt>Unit</tt> */ static Unit find(long type) { if (base == null) { synchronized(Unit.class) { if (base == null) { int size = allUnits.length; base = new Hashtable(size << 1); for (int i = 0; i < size; i++) { base.put(allUnits[i], allUnits[i]); } } } } Unit unit = new Unit(null, type); Unit out = (Unit) base.get(unit); if (out == null) { base.put(unit, unit); out = unit; } return out; } /** * Returns a <tt>String</tt> object representing the <tt>Unit</tt> * * @return A <tt>String</tt> object representing the <tt>Unit</tt> */ public String toString() { if (name == null) { int m = (int) (((type >> m_SHIFT) & MASK) - ZERO); int s = (int) (((type >> s_SHIFT) & MASK) - ZERO); int kg = (int) (((type >> kg_SHIFT) & MASK) - ZERO); int K = (int) (((type >> K_SHIFT) & MASK) - ZERO); int A = (int) (((type >> A_SHIFT) & MASK) - ZERO); int mol = (int) (((type >> mol_SHIFT) & MASK) - ZERO); int cd = (int) (((type >> cd_SHIFT) & MASK) - ZERO); int rad = (int) (((type >> rad_SHIFT) & MASK) - ZERO); int x = (int) ((type >> x_SHIFT) & MASK); StringBuffer numerator = new StringBuffer(); StringBuffer denominator = new StringBuffer(); addSIname(m, "m", numerator, denominator); addSIname(s, "s", numerator, denominator); addSIname(kg, "kg", numerator, denominator); addSIname(K, "K", numerator, denominator); addSIname(A, "A", numerator, denominator); addSIname(mol, "mol", numerator, denominator); addSIname(cd, "cd", numerator, denominator); addSIname(rad, "rad", numerator, denominator); if (denominator.length() > 0) { if (numerator.length() == 0) { numerator.append("1"); } numerator.append("/"); numerator.append((Object)denominator); /* we use (Object) to avoid using new 1.4 method append(StringBuffer) */ } name = numerator.toString(); } return name; } private void addSIname(int si, String name, StringBuffer numerator, StringBuffer 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. * * @return true if the type is special, otherwise false. */ private boolean isSpecial() { return (type & x_MASK) != 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -