Completed
Push — master ( cdb5aa...909635 )
by Bernhard
10s
created

Flt::valueOf()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.0416
1
<?php
2
3
/**
4
 * \AppserverIo\Lang\Flt
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/lang
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Lang;
22
23
/**
24
 * This class implements functionality to handle
25
 * a float value as object.
26
 *
27
 * @author    Tim Wagner <[email protected]>
28
 * @copyright 2015 TechDivision GmbH <[email protected]>
29
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
30
 * @link      https://github.com/appserver-io/lang
31
 * @link      http://www.appserver.io
32
 */
33
class Flt extends Number implements \Serializable
34
{
35
36
    /**
37
     * The value of the Float.
38
     *
39
     * @var float
40
     */
41
    protected $value;
42
43
    /**
44
     * Constructs a newly allocated <code>Float</code> object that
45
     * represents the primitive <code>float</code> argument.
46
     *
47
     * @param float $value The value to be represented by the <code>Float</code>
48
     */
49 12
    public function __construct($value)
50
    {
51
        // initialize property default values here, as declarative default values may break thread safety,
52
        // when utilizing static and non-static access on class methods within same thread context!
53 12
        $this->value = null;
54
55 12
        if (!is_float($value)) {
0 ignored issues
show
introduced by
The condition is_float($value) is always true.
Loading history...
56
            NumberFormatException::forInputString($value);
57
        }
58 12
        $this->value = $value;
59 12
    }
60
61
    /**
62
     * This method returns the class name as
63
     * a string.
64
     *
65
     * @return string
66
     */
67 1
    public static function __getClass()
68
    {
69 1
        return __CLASS__;
70
    }
71
72
    /**
73
     * Returns a <code>Float</code> object holding the
74
     * <code>float</code> value represented by the argument string
75
     * <code>s</code>.
76
     * <p>
77
     * If <code>s</code> is <code>null</code>, then a
78
     * <code>NullPointerException</code> is thrown.
79
     * <p>
80
     * Leading and trailing whitespace characters in <code>s</code>
81
     * are ignored. The rest of <code>s</code> should constitute a
82
     * <i>Float</i> as described by the lexical syntax rules:
83
     * <blockquote><i>
84
     * <dl>
85
     * <dt>Float:
86
     * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
87
     * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
88
     * <dd>Sign<sub>opt</sub> FloatingPointLiteral
89
     * </dl>
90
     * </i></blockquote>
91
     * where <i>Sign</i> and <i>FloatingPointLiteral</i> are as
92
     * defined in <a href="http://java.sun.com/docs/books/jls/second_edition/
93
     * html/lexical.doc.html#230798">&sect;3.10.2</a>
94
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
95
     * Language Specification</a>. If <code>s</code> does not have the
96
     * form of a <i>Float</i>, then a
97
     * <code>NumberFormatException</code> is thrown. Otherwise,
98
     * <code>s</code> is regarded as representing an exact decimal
99
     * value in the usual "computerized scientific notation"; this
100
     * exact decimal value is then conceptually converted to an
101
     * "infinitely precise" binary value that is then rounded to type
102
     * <code>float</code> by the usual round-to-nearest rule of IEEE
103
     * 754 floating-point arithmetic, which includes preserving the
104
     * sign of a zero value. Finally, a <code>Float</code> object
105
     * representing this <code>float</code> value is returned.
106
     * <p>
107
     * To interpret localized string representations of a
108
     * floating-point value, use subclasses of {@link
109
     * java.text.NumberFormat}.
110
     *
111
     * <p>Note that trailing format specifiers, specifiers that
112
     * determine the type of a floating-point literal
113
     * (<code>1.0f</code> is a <code>float</code> value;
114
     * <code>1.0d</code> is a <code>double</code> value), do
115
     * <em>not</em> influence the results of this method. In other
116
     * words, the numerical value of the input string is converted
117
     * directly to the target floating-point type. In general, the
118
     * two-step sequence of conversions, string to <code>double</code>
119
     * followed by <code>double</code> to <code>float</code>, is
120
     * <em>not</em> equivalent to converting a string directly to
121
     * <code>float</code>. For example, if first converted to an
122
     * intermediate <code>double</code> and then to
123
     * <code>float</code>, the string<br>
124
     * <code>"1.00000017881393421514957253748434595763683319091796875001d"
125
     * </code><br>results in the <code>float</code> value
126
     * <code>1.0000002f</code>; if the string is converted directly to
127
     * <code>float</code>, <code>1.000000<b>1</b>f</code> results.
128
     *
129
     * @param \AppserverIo\Lang\Strng $string The string to be parsed
130
     *
131
     * @return \AppserverIo\Lang\Flt A <code>Float</code> object holding the value represented by the <code>Strng</code> argument
132
     * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable number
133
     */
134 4
    public static function valueOf(Strng $string)
135
    {
136 4
        if (! preg_match("/([0-9\.-]+)/", $string->stringValue())) {
137
            NumberFormatException::forInputString($string->stringValue());
138
        }
139 4
        if (! is_numeric($string->stringValue())) {
140 2
            NumberFormatException::forInputString($string->stringValue());
141
        }
142 2
        return new Flt((float) $string->stringValue());
143
    }
144
145
    /**
146
     * Returns a new <code>float</code> initialized to the value
147
     * represented by the specified <code>Strng</code>, as performed
148
     * by the <code>valueOf</code> method of class <code>Float</code>.
149
     *
150
     * @param \AppserverIo\Lang\Strng $string She string to be parsed
151
     *
152
     * @return float The <code>float</code> value represented by the string argument
153
     * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable <code>float</code>.
154
     * @see \AppserverIo\Lang\Flt::valueOf($string)
155
     */
156 2
    public static function parseFloat(Strng $string)
157
    {
158 2
        return Flt::valueOf($string)->floatValue();
159
    }
160
161
    /**
162
     * Returns the value of the specified number as a <code>float</code>.
163
     * This may involve rounding.
164
     *
165
     * @return float The numeric value represented by this object after conversion to type <code>float</code>
166
     * @see \AppserverIo\Lang\Number::floatValue()
167
     */
168 9
    public function floatValue()
169
    {
170 9
        return $this->value;
171
    }
172
173
    /**
174
     * Returns the value of the specified number as an <code>int</code>.
175
     * This may involve rounding or truncation.
176
     *
177
     * @return integer The numeric value represented by this object after conversion to type <code>int</code>
178
     * @see \AppserverIo\Lang\Number::intValue()
179
     */
180 3
    public function intValue()
181
    {
182 3
        return (int) $this->value;
183
    }
184
185
    /**
186
     * Returns the value of the specified number as a <code>double</code>.
187
     * This may involve rounding.
188
     *
189
     * @return double The numeric value represented by this object after conversion to type <code>double</code>
190
     * @see \AppserverIo\Lang\Number::doubleValue()
191
     */
192 1
    public function doubleValue()
193
    {
194 1
        return (double) $this->value;
195
    }
196
197
    /**
198
     * This method has to be called to serialize the Float.
199
     *
200
     * @return string Returns a serialized version of the Float
201
     * @see \Serializable::serialize()
202
     */
203 1
    public function serialize()
204
    {
205 1
        return serialize($this->value);
206
    }
207
208
    /**
209
     * This method unserializes the passed string and initializes the Float
210
     * itself with the data.
211
     *
212
     * @param string $data Holds the data of the instance as serialized string
213
     *
214
     * @return void
215
     * @see \Serializable::unserialize($data)
216
     */
217 1
    public function unserialize($data)
218
    {
219 1
        $this->value = unserialize($data);
220 1
    }
221
222
    /**
223
     * This object as String returned.
224
     *
225
     * @return \AppserverIo\Lang\Strng The value as String.
226
     */
227
    public function toString()
228
    {
229
        return new Strng($this->value);
230
    }
231
232
    /**
233
     * This method returns the class as
234
     * a string representation.
235
     *
236
     * @return string The objects string representation
237
     * @see \AppserverIo\Lang\Objct::__toString()
238
     */
239
    public function __toString()
240
    {
241
        $string = new Strng($this->value);
242
        return $string->stringValue();
243
    }
244
245
    /**
246
     * Returns true if the passed value is equal.
247
     *
248
     * @param \AppserverIo\Lang\Objct $val The value to check
249
     *
250
     * @return boolean
251
     */
252 2
    public function equals(Objct $val)
253
    {
254 2
        if ($val instanceof Flt) {
255 2
            return $this->floatValue() == $val->floatValue();
256
        }
257
        return false;
258
    }
259
260
    /**
261
     * Adds the value of the passed Float.
262
     *
263
     * @param \AppserverIo\Lang\Flt $toAdd The Float to add
264
     *
265
     * @return \AppserverIo\Lang\Flt The instance
266
     */
267 1
    public function add(Flt $toAdd)
268
    {
269 1
        $this->value += $toAdd->floatValue();
270 1
        return $this;
271
    }
272
273
    /**
274
     * Subtracts the value of the passed Float.
275
     *
276
     * @param \AppserverIo\Lang\Flt $toSubtract The Float to subtract
277
     *
278
     * @return \AppserverIo\Lang\Flt The instance
279
     */
280 1
    public function subtract(Flt $toSubtract)
281
    {
282 1
        $this->value -= $toSubtract->floatValue();
283 1
        return $this;
284
    }
285
286
    /**
287
     * Multiplies the Float with the passed one.
288
     *
289
     * @param \AppserverIo\Lang\Flt $toMultiply The Float to multiply
290
     *
291
     * @return \AppserverIo\Lang\Flt The instance
292
     */
293 1
    public function multiply(Flt $toMultiply)
294
    {
295 1
        $this->value *= $toMultiply->intValue();
296 1
        return $this;
297
    }
298
299
    /**
300
     * Divides the Float by the passed one.
301
     *
302
     * @param \AppserverIo\Lang\Flt $dividyBy The Float to dividy by
303
     *
304
     * @return \AppserverIo\Lang\Flt The instance
305
     */
306 2
    public function divide(Flt $dividyBy)
307
    {
308 2
        $this->value = $this->value / $dividyBy->floatValue();
309 2
        return $this;
310
    }
311
}
312