Changeset 1732
- Timestamp:
- 07/22/08 22:37:47 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
labs/incubator/src/main/php/net/stubbles/lang/types/stubByte.php
r1723 r1732 1 1 <?php 2 2 /** 3 * Represents a byte, which is in the range of -128 to 127.3 * Represents a byte, which is in the range of -128 (-2^7) to 127 ((2^7) - 1). 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 8 8 * @version $Id$ 9 9 */ 10 stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalArgumentException'); 10 stubClassLoader::load('net::stubbles::lang::types::stubNumber', 11 'net::stubbles::lang::exceptions::stubIllegalArgumentException' 12 ); 11 13 /** 12 * Represents a byte, which is in the range of -128 to 127.14 * Represents a byte, which is in the range of -128 (-2^7) to 127 ((2^7) - 1). 13 15 * 14 16 * @package stubbles 15 17 * @subpackage lang_types 16 18 */ 17 class stubByte extends stub BaseObject19 class stubByte extends stubNumber 18 20 { 19 21 /** 20 * minimum value of a byte 22 * minimum value of a byte: -2^7 21 23 */ 22 24 const MIN_VALUE = -128; 23 25 /** 24 * maximum value of a byte 26 * maximum value of a byte: (2^7) - 1 25 27 */ 26 28 const MAX_VALUE = 127; 27 /**28 * value of the byte29 *30 * @var int31 */32 protected $value;33 29 34 30 /** 35 * constructor 36 37 * @param int $value 38 * @throws stubIllegalArgumentException 31 * returns minimum allowed value 32 * 33 * @return int 39 34 */ 40 public function __construct($value)35 public function minValue() 41 36 { 42 $value = (int) $value; 43 if (self::MIN_VALUE > $value || self::MAX_VALUE < $value) { 44 throw new stubIllegalArgumentException('Invalid byte value ' . $value . ', must be between ' . self::MIN_VALUE . ' and ' . self::MAX_VALUE); 45 } 46 47 $this->value = $value; 37 return self::MIN_VALUE; 48 38 } 49 39 50 40 /** 51 * returns integer representation41 * returns maximum allowed value 52 42 * 53 43 * @return int 54 44 */ 55 public function asInt()45 public function maxValue() 56 46 { 57 return $this->value; 58 } 59 60 /** 61 * returns float representation 62 * 63 * @return float 64 */ 65 public function asFloat() 66 { 67 return (float) $this->value; 68 } 69 70 /** 71 * checks whether a value is equal to the class 72 * 73 * @param mixed $compare 74 * @return bool 75 */ 76 public function equals($compare) 77 { 78 if ($compare instanceof self) { 79 return $this->value === $compare->value; 80 } 81 82 return false; 47 return self::MAX_VALUE; 83 48 } 84 49 } labs/incubator/src/test/php/net/stubbles/lang/types/stubByteTestCase.php
r1723 r1732 19 19 class stubByteTestCase extends PHPUnit_Framework_TestCase 20 20 { 21 /** 22 * return value of minValue() should be equal to MIN_VALUE class contant 23 * 24 * @test 25 */ 26 public function minValueEqualToMIN_VALUE() 27 { 28 $byte = new stubByte(1); 29 $this->assertEquals($byte->minValue(), stubByte::MIN_VALUE); 30 } 31 32 /** 33 * return value of maxValue() should be equal to MAX_VALUE class contant 34 * 35 * @test 36 */ 37 public function maxValueEqualToMAX_VALUE() 38 { 39 $byte = new stubByte(1); 40 $this->assertEquals($byte->maxValue(), stubByte::MAX_VALUE); 41 } 42 21 43 /** 22 44 * a value smaller than -128 is not allowed
