Access keysTop, Summary, Constructors,
Class properties,
Instance properties,
Instance methodsBit
| Kind of class: |
class |
| Inherits from: |
BasicClass
|
| Known subclasses: |
|
| Version: |
1.1 |
| Author: |
Martin Heidegger |
| Classpath: |
org.as2lib.data.type.Bit |
| File last modified: |
Thursday, 13 October 2005, 15:21:00 |
/*
* Copyright the original author or authors.
*
* Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html
*
* 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.
*/
import org.as2lib.core.BasicClass;
/**
* {@code Bit} is represents a bit value.
*
* <p>{@code Bit} can be used for a different kind of formatting of a bit value.
* It allows to access the value as bit, kilo-bit, mega-bit, giga-bit, tera-bit,
* byte, kilo-byte, mega-byte, giga-byte and tera-byte.
*
* @author Martin Heidegger
* @version 1.1
*/
class org.as2lib.data.type.Bit extends BasicClass {
/** Default floating points used. */
public static var DEFAULT_FLOATING_POINTS:Number = 2;
/** Size of a kilo. */
private static var KILO:Number = 1024;
/** Size of a kilobit. */
private static var KILO_BIT:Number = KILO;
/** Size of a megabit. */
private static var MEGA_BIT:Number = KILO_BIT*KILO;
/** Size of a gigabit. */
private static var GIGA_BIT:Number = MEGA_BIT*KILO;
/** Size of a terabit. */
private static var TERA_BIT:Number = GIGA_BIT*KILO;
/** Size of a byte. */
private static var BYTE:Number = 8;
/** Size of a kilobyte. */
private static var KILO_BYTE:Number = KILO*BYTE;
/** Size of a megabyte. */
private static var MEGA_BYTE:Number = KILO_BYTE*KILO;
/** Size of a gigabyte. */
private static var GIGA_BYTE:Number = MEGA_BYTE*KILO;
/** Size of a terabyte. */
private static var TERA_BYTE:Number = GIGA_BYTE*KILO;
/** Shortname of bit. */
private static var SHORT_BIT:String = "b";
/** Shortname of kilobit. */
private static var SHORT_KILO_BIT:String = "Kb";
/** Shortname of megabit. */
private static var SHORT_MEGA_BIT:String = "Mb";
/** Shortname of gigabit. */
private static var SHORT_GIGA_BIT:String = "Gb";
/** Shortname of terabit. */
private static var SHORT_TERA_BIT:String = "Tb";
/** Holder for the amount of bits. */
private var bit:Number;
/** Holder for the comma seperation. */
private var comma:Number;
/**
* Constructs a new {@code Bit}.
*
* @param bit value in bit
*/
public function Bit(bit:Number) {
this.bit = bit;
comma = DEFAULT_FLOATING_POINTS;
}
/**
* Sets the used amount of values after the comma.
*
* <p>This method does not change anything if {@code fp} is smaller than 0
* or not passed-in.
*
* @param fp amount of characters after the floating point
* @return the current instance
*/
public function setFloatingPoints(fp:Number):Bit {
if(fp >= 0 && fp != null) {
this.comma = fp;
}
return this;
}
/**
* Rounds a number by a count of floating points.
*
* @param num {@code Number} to be rounded
* @param fp amount of characters after the floating point
*/
private function round(num:Number, fp:Number):Number {
var result:Number = 1;
for(var i:Number = 0; i<fp; i++) {
result *= 10;
}
return (Math.round(num*result)/result);
}
/**
* Returns the value in bit.
*
* @return value in bit
*/
public function getBit(Void):Number {
return bit;
}
/**
* Returns the value in bytes.
*
* @return value in bytes
*/
public function getBytes(Void):Number {
return round(bit/BYTE, comma);
}
/**
* Returns the value in kilobit.
*
* @return value in kilobit
*/
public function getKiloBit(Void):Number {
return round(bit/KILO_BIT, comma);
}
/**
* Returns the value in kilobytes.
*
* @return value in kilobytes
*/
public function getKiloBytes(Void):Number {
return round(bit/KILO_BYTE, comma);
}
/**
* Returns the value in megabit.
*
* @return value in megabit
*/
public function getMegaBit(Void):Number {
return round(bit/MEGA_BIT, comma);
}
/**
* Returns the value in megabytes.
*
* @return value in megabytes
*/
public function getMegaBytes(Void):Number {
return round(bit/MEGA_BYTE, comma);
}
/**
* Returns the value in gigabit.
*
* @return value in gigabit
*/
public function getGigaBit(Void):Number {
return round(bit/GIGA_BIT, comma);
}
/**
* Returns the value in gigabytes.
*
* @return value in gigabytes
*/
public function getGigaBytes(Void):Number {
return round(bit/GIGA_BYTE, comma);
}
/**
* Returns the value in terabit.
*
* @return value in terabit
*/
public function getTeraBit(Void):Number {
return round(bit/TERA_BIT, comma);
}
/**
* Returns the value in terabytes.
*
* @return value in terabytes
*/
public function getTeraBytes(Void):Number {
return round(bit/TERA_BYTE, comma);
}
/**
* Extended toString method for a well formatted bit value.
*
* <p>This method uses the next matching size and adds the matching Shortname for it.
*
* <p>Examples:
* <code>
* new BitFormat(1).toString(); // 1b
* new BitFormat(1234).toString(); // 1.21Kb
* new BitFormat(15002344).toString(); // 14.31Mb
* </code>
*
* @return bits in the next matching size with the matchin unit
* @see #DEFAULT_FLOATING_POINTS
*/
public function toString():String {
if(bit < KILO_BIT) {
return getBit()+SHORT_BIT;
} else if(bit < MEGA_BIT) {
return getKiloBit()+SHORT_KILO_BIT;
} else if(bit < GIGA_BIT) {
return getMegaBit()+SHORT_MEGA_BIT;
} else if(bit < TERA_BIT) {
return getGigaBit()+SHORT_GIGA_BIT;
} else {
return getTeraBit()+SHORT_TERA_BIT;
}
}
/**
*
*/
public function valueOf():Number {
return getBytes();
}
}
Bit is represents a bit value.
Bit can be used for a different kind of formatting of a bit value.
It allows to access the value as bit, kilo-bit, mega-bit, giga-bit, tera-bit,
byte, kilo-byte, mega-byte, giga-byte and tera-byte.
Constructor
Bit
function Bit (
bit:Number)
Constructs a new Bit.
Class properties
BYTE
static private BYTE:Number = 8
(read,write)
Size of a byte.
DEFAULT_FLOATING_POINTS
static DEFAULT_FLOATING_POINTS:Number = 2
(read,write)
Default floating points used.
GIGA_BIT
static private GIGA_BIT:Number = MEGA_BIT*KILO
(read,write)
Size of a gigabit.
GIGA_BYTE
static private GIGA_BYTE:Number = MEGA_BYTE*KILO
(read,write)
Size of a gigabyte.
KILO
static private KILO:Number = 1024
(read,write)
Size of a kilo.
KILO_BIT
static private KILO_BIT:Number = KILO
(read,write)
Size of a kilobit.
KILO_BYTE
static private KILO_BYTE:Number = KILO*BYTE
(read,write)
Size of a kilobyte.
MEGA_BIT
static private MEGA_BIT:Number = KILO_BIT*KILO
(read,write)
Size of a megabit.
MEGA_BYTE
static private MEGA_BYTE:Number = KILO_BYTE*KILO
(read,write)
Size of a megabyte.
SHORT_BIT
static private SHORT_BIT:String = "b"
(read,write)
Shortname of bit.
SHORT_GIGA_BIT
static private SHORT_GIGA_BIT:String = "Gb"
(read,write)
Shortname of gigabit.
SHORT_KILO_BIT
static private SHORT_KILO_BIT:String = "Kb"
(read,write)
Shortname of kilobit.
SHORT_MEGA_BIT
static private SHORT_MEGA_BIT:String = "Mb"
(read,write)
Shortname of megabit.
SHORT_TERA_BIT
static private SHORT_TERA_BIT:String = "Tb"
(read,write)
Shortname of terabit.
TERA_BIT
static private TERA_BIT:Number = GIGA_BIT*KILO
(read,write)
Size of a terabit.
TERA_BYTE
static private TERA_BYTE:Number = GIGA_BYTE*KILO
(read,write)
Size of a terabyte.
Instance properties
bit
private bit:Number
(read,write)
Holder for the amount of bits.
comma
private comma:Number
(read,write)
Holder for the comma seperation.
Instance methods
getBit
function getBit (
Void) : Number
Returns the value in bit.
getBytes
function getBytes (
Void) : Number
Returns the value in bytes.
getGigaBit
function getGigaBit (
Void) : Number
Returns the value in gigabit.
getGigaBytes
function getGigaBytes (
Void) : Number
Returns the value in gigabytes.
Returns:value in gigabytes
getKiloBit
function getKiloBit (
Void) : Number
Returns the value in kilobit.
getKiloBytes
function getKiloBytes (
Void) : Number
Returns the value in kilobytes.
Returns:value in kilobytes
getMegaBit
function getMegaBit (
Void) : Number
Returns the value in megabit.
getMegaBytes
function getMegaBytes (
Void) : Number
Returns the value in megabytes.
Returns:value in megabytes
getTeraBit
function getTeraBit (
Void) : Number
Returns the value in terabit.
getTeraBytes
function getTeraBytes (
Void) : Number
Returns the value in terabytes.
Returns:value in terabytes
round
private function round (
num:Number,
fp:Number) : Number
Rounds a number by a count of floating points.
Parameters:
num:
Number to be rounded
fp :
amount of characters after the floating point
setFloatingPoints
function setFloatingPoints (
Sets the used amount of values after the comma.
This method does not change anything if fp is smaller than 0
or not passed-in.
Parameters:
fp:
amount of characters after the floating point
Returns:the current instance
toString
function toString (
) : String
Extended toString method for a well formatted bit value.
This method uses the next matching size and adds the matching Shortname for it.
Examples:
new BitFormat(1).toString();
new BitFormat(1234).toString();
new BitFormat(15002344).toString();
Returns:bits in the next matching size with the matchin unit
valueOf
function valueOf (
) : Number