Changeset 1732

Show
Ignore:
Timestamp:
07/22/08 22:37:47 (4 months ago)
Author:
mikey
Message:

refactor net::stubbles::lang::types::stubByte to allow more different number implementations

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • labs/incubator/src/main/php/net/stubbles/lang/types/stubByte.php

    r1723 r1732  
    11<?php 
    22/** 
    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)
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    88 * @version     $Id$ 
    99 */ 
    10 stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalArgumentException'); 
     10stubClassLoader::load('net::stubbles::lang::types::stubNumber', 
     11                      'net::stubbles::lang::exceptions::stubIllegalArgumentException' 
     12); 
    1113/** 
    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)
    1315 * 
    1416 * @package     stubbles 
    1517 * @subpackage  lang_types 
    1618 */ 
    17 class stubByte extends stubBaseObject 
     19class stubByte extends stubNumber 
    1820{ 
    1921    /** 
    20      * minimum value of a byte 
     22     * minimum value of a byte: -2^7 
    2123     */ 
    2224    const MIN_VALUE = -128; 
    2325    /** 
    24      * maximum value of a byte 
     26     * maximum value of a byte: (2^7) - 1 
    2527     */ 
    2628    const MAX_VALUE = 127; 
    27     /** 
    28      * value of the byte 
    29      * 
    30      * @var  int 
    31      */ 
    32     protected $value; 
    3329 
    3430    /** 
    35      * constructor 
    36  
    37      * @param   int  $value 
    38      * @throws  stubIllegalArgumentException 
     31     * returns minimum allowed value 
     32     * 
     33     * @return  int 
    3934     */ 
    40     public function __construct($value
     35    public function minValue(
    4136    { 
    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; 
    4838    } 
    4939 
    5040    /** 
    51      * returns integer representation 
     41     * returns maximum allowed value 
    5242     * 
    5343     * @return  int 
    5444     */ 
    55     public function asInt() 
     45    public function maxValue() 
    5646    { 
    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; 
    8348    } 
    8449} 
  • labs/incubator/src/test/php/net/stubbles/lang/types/stubByteTestCase.php

    r1723 r1732  
    1919class stubByteTestCase extends PHPUnit_Framework_TestCase 
    2020{ 
     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 
    2143    /** 
    2244     * a value smaller than -128 is not allowed