1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.core.BasicClass; 18 19 /** 20 * {@code Bit} is represents a bit value. 21 * 22 * <p>{@code Bit} can be used for a different kind of formatting of a bit value. 23 * It allows to access the value as bit, kilo-bit, mega-bit, giga-bit, tera-bit, 24 * byte, kilo-byte, mega-byte, giga-byte and tera-byte. 25 * 26 * @author Martin Heidegger 27 * @version 1.1 28 */ 29 class org.as2lib.data.type.Bit extends BasicClass { 30 31 /** Default floating points used. */ 32 public static var DEFAULT_FLOATING_POINTS:Number = 2; 33 34 /** Size of a kilo. */ 35 private static var KILO:Number = 1024; 36 37 /** Size of a kilobit. */ 38 private static var KILO_BIT:Number = KILO; 39 40 /** Size of a megabit. */ 41 private static var MEGA_BIT:Number = KILO_BIT*KILO; 42 43 /** Size of a gigabit. */ 44 private static var GIGA_BIT:Number = MEGA_BIT*KILO; 45 46 /** Size of a terabit. */ 47 private static var TERA_BIT:Number = GIGA_BIT*KILO; 48 49 /** Size of a byte. */ 50 private static var BYTE:Number = 8; 51 52 /** Size of a kilobyte. */ 53 private static var KILO_BYTE:Number = KILO*BYTE; 54 55 /** Size of a megabyte. */ 56 private static var MEGA_BYTE:Number = KILO_BYTE*KILO; 57 58 /** Size of a gigabyte. */ 59 private static var GIGA_BYTE:Number = MEGA_BYTE*KILO; 60 61 /** Size of a terabyte. */ 62 private static var TERA_BYTE:Number = GIGA_BYTE*KILO; 63 64 /** Shortname of bit. */ 65 private static var SHORT_BIT:String = "b"; 66 67 /** Shortname of kilobit. */ 68 private static var SHORT_KILO_BIT:String = "Kb"; 69 70 /** Shortname of megabit. */ 71 private static var SHORT_MEGA_BIT:String = "Mb"; 72 73 /** Shortname of gigabit. */ 74 private static var SHORT_GIGA_BIT:String = "Gb"; 75 76 /** Shortname of terabit. */ 77 private static var SHORT_TERA_BIT:String = "Tb"; 78 79 /** Holder for the amount of bits. */ 80 private var bit:Number; 81 82 /** Holder for the comma seperation. */ 83 private var comma:Number; 84 85 /** 86 * Constructs a new {@code Bit}. 87 * 88 * @param bit value in bit 89 */ 90 public function Bit(bit:Number) { 91 this.bit = bit; 92 comma = DEFAULT_FLOATING_POINTS; 93 } 94 95 /** 96 * Sets the used amount of values after the comma. 97 * 98 * <p>This method does not change anything if {@code fp} is smaller than 0 99 * or not passed-in. 100 * 101 * @param fp amount of characters after the floating point 102 * @return the current instance 103 */ 104 public function setFloatingPoints(fp:Number):Bit { 105 if(fp >= 0 && fp != null) { 106 this.comma = fp; 107 } 108 return this; 109 } 110 111 /** 112 * Rounds a number by a count of floating points. 113 * 114 * @param num {@code Number} to be rounded 115 * @param fp amount of characters after the floating point 116 */ 117 private function round(num:Number, fp:Number):Number { 118 var result:Number = 1; 119 for(var i:Number = 0; i<fp; i++) { 120 result *= 10; 121 } 122 return (Math.round(num*result)/result); 123 } 124 125 /** 126 * Returns the value in bit. 127 * 128 * @return value in bit 129 */ 130 public function getBit(Void):Number { 131 return bit; 132 } 133 134 /** 135 * Returns the value in bytes. 136 * 137 * @return value in bytes 138 */ 139 public function getBytes(Void):Number { 140 return round(bit/BYTE, comma); 141 } 142 143 /** 144 * Returns the value in kilobit. 145 * 146 * @return value in kilobit 147 */ 148 public function getKiloBit(Void):Number { 149 return round(bit/KILO_BIT, comma); 150 } 151 152 /** 153 * Returns the value in kilobytes. 154 * 155 * @return value in kilobytes 156 */ 157 public function getKiloBytes(Void):Number { 158 return round(bit/KILO_BYTE, comma); 159 } 160 161 /** 162 * Returns the value in megabit. 163 * 164 * @return value in megabit 165 */ 166 public function getMegaBit(Void):Number { 167 return round(bit/MEGA_BIT, comma); 168 } 169 170 /** 171 * Returns the value in megabytes. 172 * 173 * @return value in megabytes 174 */ 175 public function getMegaBytes(Void):Number { 176 return round(bit/MEGA_BYTE, comma); 177 } 178 179 /** 180 * Returns the value in gigabit. 181 * 182 * @return value in gigabit 183 */ 184 public function getGigaBit(Void):Number { 185 return round(bit/GIGA_BIT, comma); 186 } 187 188 /** 189 * Returns the value in gigabytes. 190 * 191 * @return value in gigabytes 192 */ 193 public function getGigaBytes(Void):Number { 194 return round(bit/GIGA_BYTE, comma); 195 } 196 197 /** 198 * Returns the value in terabit. 199 * 200 * @return value in terabit 201 */ 202 public function getTeraBit(Void):Number { 203 return round(bit/TERA_BIT, comma); 204 } 205 206 /** 207 * Returns the value in terabytes. 208 * 209 * @return value in terabytes 210 */ 211 public function getTeraBytes(Void):Number { 212 return round(bit/TERA_BYTE, comma); 213 } 214 215 /** 216 * Extended toString method for a well formatted bit value. 217 * 218 * <p>This method uses the next matching size and adds the matching Shortname for it. 219 * 220 * <p>Examples: 221 * <code> 222 * new BitFormat(1).toString(); // 1b 223 * new BitFormat(1234).toString(); // 1.21Kb 224 * new BitFormat(15002344).toString(); // 14.31Mb 225 * </code> 226 * 227 * @return bits in the next matching size with the matchin unit 228 * @see #DEFAULT_FLOATING_POINTS 229 */ 230 public function toString():String { 231 if(bit < KILO_BIT) { 232 return getBit()+SHORT_BIT; 233 } else if(bit < MEGA_BIT) { 234 return getKiloBit()+SHORT_KILO_BIT; 235 } else if(bit < GIGA_BIT) { 236 return getMegaBit()+SHORT_MEGA_BIT; 237 } else if(bit < TERA_BIT) { 238 return getGigaBit()+SHORT_GIGA_BIT; 239 } else { 240 return getTeraBit()+SHORT_TERA_BIT; 241 } 242 } 243 244 /** 245 * 246 */ 247 public function valueOf():Number { 248 return getBytes(); 249 } 250 }