Class to represent 16-bit unsigned integers. : Number « Data Type « Java






Class to represent 16-bit unsigned integers.

      
/*
 * D-Bus Java Implementation Copyright (c) 2005-2006 Matthew Johnson This
 * program is free software; you can redistribute it and/or modify it under the
 * terms of either the GNU Lesser General Public License Version 2 or the
 * Academic Free Licence Version 2.1. Full licence texts are included in the
 * COPYING file with this program.
 */
//package org.freedesktop.dbus;

import java.text.MessageFormat;

/**
 * Class to represent 16-bit unsigned integers.
 */
@SuppressWarnings("serial")
public class UInt16 extends Number implements Comparable<UInt16> {
  /** Maximum possible value. */
  public static final int MAX_VALUE = 65535;
  /** Minimum possible value. */
  public static final int MIN_VALUE = 0;
  private int             value;

  /**
   * Create a UInt16 from an int.
   * 
   * @param value
   *          Must be within MIN_VALUE&ndash;MAX_VALUE
   * @throws NumberFormatException
   *           if value is not between MIN_VALUE and MAX_VALUE
   */
  public UInt16(int value) {
    
    this.value = value;
  }

  /**
   * Create a UInt16 from a String.
   * 
   * @param value
   *          Must parse to a valid integer within MIN_VALUE&ndash;MAX_VALUE
   * @throws NumberFormatException
   *           if value is not an integer between MIN_VALUE and MAX_VALUE
   */
  public UInt16(String value) {
    this(Integer.parseInt(value));
  }

  /** The value of this as a byte. */
  @Override
  public byte byteValue() {
    return (byte) value;
  }

  /**
   * Compare two UInt16s.
   * 
   * @return 0 if equal, -ve or +ve if they are different.
   */
  public int compareTo(UInt16 other) {
    return /* (int) */(value - other.value);
  }

  /** The value of this as a double. */
  @Override
  public double doubleValue() {
    return value;
  }

  /** Test two UInt16s for equality. */
  @Override
  public boolean equals(Object o) {
    return (o instanceof UInt16) && (((UInt16) o).value == value);
  }

  /** The value of this as a float. */
  @Override
  public float floatValue() {
    return value;
  }

  @Override
  public int hashCode() {
    return /* (int) */value;
  }

  /** The value of this as a int. */
  @Override
  public int intValue() {
    return /* (int) */value;
  }

  /** The value of this as a long. */
  @Override
  public long longValue() {
    return value;
  }

  /** The value of this as a short. */
  @Override
  public short shortValue() {
    return (short) value;
  }

  /** The value of this as a string. */
  @Override
  public String toString() {
    return "" + value;
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Methods for number conversion and parsing
2.Get Digit Number String
3.Convert to numbers
4.Check Number properties and convert from Number
5.Check if number are in a given range
6.Various number-related routines and classes that are frequently used
7.Turns a string value into a java.lang.Number.
8.Provides IEEE-754r variants of NumberUtils methods.
9.Checks whether the String a valid Java number.
10.Specifies sizes of various types in bytes.
11.Implements an integer object wrapper which allows changing the integer value.Implements an integer object wrapper which allows changing the integer value.
12.Mixed Radix Number
13.This class allows a number to be converted to it's unsigned value.
14.Numbers will be equals if enough decimals are equals, without thinking about approximations.
15.Ternary Numnber
16.Implements a long object wrapper which allows changing the long value.
17.Convert the given number into an instance of the given target class.