1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.Number; 13 14 import hunt.Nullable; 15 import std.traits; 16 17 /** 18 * The class {@code Number} is the superclass of platform 19 * classes representing numeric values that are convertible to the 20 * primitive types {@code byte}, {@code double}, {@code float}, {@code 21 * int}, {@code long}, and {@code short}. 22 * 23 * The specific semantics of the conversion from the numeric value of 24 * a particular {@code Number} implementation to a given primitive 25 * type is defined by the {@code Number} implementation in question. 26 * 27 * For platform classes, the conversion is often analogous to a 28 * narrowing primitive conversion or a widening primitive conversion 29 * as defined in <cite>The Java™ Language Specification</cite> 30 * for converting between primitive types. Therefore, conversions may 31 * lose information about the overall magnitude of a numeric value, may 32 * lose precision, and may even return a result of a different sign 33 * than the input. 34 * 35 * See the documentation of a given {@code Number} implementation for 36 * conversion details. 37 * 38 * @author Lee Boynton 39 * @author Arthur van Hoff 40 * @jls 5.1.2 Widening Primitive Conversions 41 * @jls 5.1.3 Narrowing Primitive Conversions 42 * @since 1.0 43 */ 44 // class Number(T) : Nullable!(T) if(isNumeric!T) { 45 interface Number { 46 47 /** 48 * Returns the value of the specified number as an {@code int}. 49 * 50 * @return the numeric value represented by this object after conversion 51 * to type {@code int}. 52 */ 53 int intValue(); 54 55 /** 56 * Returns the value of the specified number as a {@code long}. 57 * 58 * @return the numeric value represented by this object after conversion 59 * to type {@code long}. 60 */ 61 long longValue(); 62 63 /** 64 * Returns the value of the specified number as a {@code float}. 65 * 66 * @return the numeric value represented by this object after conversion 67 * to type {@code float}. 68 */ 69 float floatValue(); 70 71 /** 72 * Returns the value of the specified number as a {@code double}. 73 * 74 * @return the numeric value represented by this object after conversion 75 * to type {@code double}. 76 */ 77 double doubleValue(); 78 79 /** 80 * Returns the value of the specified number as a {@code byte}. 81 * 82 * <p>This implementation returns the result of {@link #intValue} cast 83 * to a {@code byte}. 84 * 85 * @return the numeric value represented by this object after conversion 86 * to type {@code byte}. 87 * @since 1.1 88 */ 89 byte byteValue(); 90 91 /** 92 * Returns the value of the specified number as a {@code short}. 93 * 94 * <p>This implementation returns the result of {@link #intValue} cast 95 * to a {@code short}. 96 * 97 * @return the numeric value represented by this object after conversion 98 * to type {@code short}. 99 * @since 1.1 100 */ 101 short shortValue(); 102 103 string toString(); 104 } 105 106 107 /** 108 */ 109 abstract class AbstractNumber(T) : Nullable!T, Number { 110 111 this(T value) { 112 super(value); 113 } 114 115 /** 116 * Returns the value of this {@code T} as an 117 * {@code int}. 118 */ 119 int intValue() { 120 return cast(int)value; 121 } 122 123 /** 124 * Returns the value of this {@code T} as a {@code long} 125 * after a widening primitive conversion. 126 * @jls 5.1.2 Widening Primitive Conversions 127 * @see T#toUnsignedLong(int) 128 */ 129 long longValue() { 130 return cast(long)value; 131 } 132 133 /** 134 * Returns the value of this {@code T} as a {@code float} 135 * after a widening primitive conversion. 136 * @jls 5.1.2 Widening Primitive Conversions 137 */ 138 float floatValue() { 139 return cast(float)value; 140 } 141 142 /** 143 * Returns the value of this {@code T} as a {@code double} 144 * after a widening primitive conversion. 145 * @jls 5.1.2 Widening Primitive Conversions 146 */ 147 double doubleValue() { 148 return cast(double)value; 149 } 150 151 152 /** 153 * Returns the value of this {@code T} as a {@code byte} after 154 * a narrowing primitive conversion. 155 * @jls 5.1.3 Narrowing Primitive Conversions 156 */ 157 byte byteValue() { 158 return cast(byte)value; 159 } 160 161 /** 162 * Returns the value of this {@code T} as a {@code short} after 163 * a narrowing primitive conversion. 164 * @jls 5.1.3 Narrowing Primitive Conversions 165 */ 166 short shortValue() { 167 return cast(short)value; 168 } 169 170 override string toString() { 171 return super.toString(); 172 } 173 }