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 */ 43 // class Number(T) : Nullable!(T) if(isNumeric!T) { 44 interface Number { 45 46 /** 47 * Returns the value of the specified number as an {@code int}. 48 * 49 * @return the numeric value represented by this object after conversion 50 * to type {@code int}. 51 */ 52 int intValue(); 53 54 /** 55 * Returns the value of the specified number as a {@code long}. 56 * 57 * @return the numeric value represented by this object after conversion 58 * to type {@code long}. 59 */ 60 long longValue(); 61 62 /** 63 * Returns the value of the specified number as a {@code float}. 64 * 65 * @return the numeric value represented by this object after conversion 66 * to type {@code float}. 67 */ 68 float floatValue(); 69 70 /** 71 * Returns the value of the specified number as a {@code double}. 72 * 73 * @return the numeric value represented by this object after conversion 74 * to type {@code double}. 75 */ 76 double doubleValue(); 77 78 /** 79 * Returns the value of the specified number as a {@code byte}. 80 * 81 * <p>This implementation returns the result of {@link #intValue} cast 82 * to a {@code byte}. 83 * 84 * @return the numeric value represented by this object after conversion 85 * to type {@code byte}. 86 */ 87 byte byteValue(); 88 89 /** 90 * Returns the value of the specified number as a {@code short}. 91 * 92 * <p>This implementation returns the result of {@link #intValue} cast 93 * to a {@code short}. 94 * 95 * @return the numeric value represented by this object after conversion 96 * to type {@code short}. 97 */ 98 short shortValue(); 99 100 string toString(); 101 } 102 103 104 /** 105 * 106 */ 107 abstract class AbstractNumber(T) : Nullable!T, Number { 108 109 this(T value) { 110 super(value); 111 } 112 113 /** 114 * Returns the value of this {@code T} as an 115 * {@code int}. 116 */ 117 int intValue() { 118 return cast(int)value; 119 } 120 121 /** 122 * Returns the value of this {@code T} as a {@code long} 123 * after a widening primitive conversion. 124 * @jls 5.1.2 Widening Primitive Conversions 125 * @see T#toUnsignedLong(int) 126 */ 127 long longValue() { 128 return cast(long)value; 129 } 130 131 /** 132 * Returns the value of this {@code T} as a {@code float} 133 * after a widening primitive conversion. 134 * @jls 5.1.2 Widening Primitive Conversions 135 */ 136 float floatValue() { 137 return cast(float)value; 138 } 139 140 /** 141 * Returns the value of this {@code T} as a {@code double} 142 * after a widening primitive conversion. 143 * @jls 5.1.2 Widening Primitive Conversions 144 */ 145 double doubleValue() { 146 return cast(double)value; 147 } 148 149 150 /** 151 * Returns the value of this {@code T} as a {@code byte} after 152 * a narrowing primitive conversion. 153 * @jls 5.1.3 Narrowing Primitive Conversions 154 */ 155 byte byteValue() { 156 return cast(byte)value; 157 } 158 159 /** 160 * Returns the value of this {@code T} as a {@code short} after 161 * a narrowing primitive conversion. 162 * @jls 5.1.3 Narrowing Primitive Conversions 163 */ 164 short shortValue() { 165 return cast(short)value; 166 } 167 168 override string toString() { 169 return super.toString(); 170 } 171 }