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 abstract class AbstractNumber(T) : Nullable!T, Number { 107 108 this(T value) { 109 super(value); 110 } 111 112 /** 113 * Returns the value of this {@code T} as an 114 * {@code int}. 115 */ 116 int intValue() { 117 return cast(int)value; 118 } 119 120 /** 121 * Returns the value of this {@code T} as a {@code long} 122 * after a widening primitive conversion. 123 * @jls 5.1.2 Widening Primitive Conversions 124 * @see T#toUnsignedLong(int) 125 */ 126 long longValue() { 127 return cast(long)value; 128 } 129 130 /** 131 * Returns the value of this {@code T} as a {@code float} 132 * after a widening primitive conversion. 133 * @jls 5.1.2 Widening Primitive Conversions 134 */ 135 float floatValue() { 136 return cast(float)value; 137 } 138 139 /** 140 * Returns the value of this {@code T} as a {@code double} 141 * after a widening primitive conversion. 142 * @jls 5.1.2 Widening Primitive Conversions 143 */ 144 double doubleValue() { 145 return cast(double)value; 146 } 147 148 149 /** 150 * Returns the value of this {@code T} as a {@code byte} after 151 * a narrowing primitive conversion. 152 * @jls 5.1.3 Narrowing Primitive Conversions 153 */ 154 byte byteValue() { 155 return cast(byte)value; 156 } 157 158 /** 159 * Returns the value of this {@code T} as a {@code short} after 160 * a narrowing primitive conversion. 161 * @jls 5.1.3 Narrowing Primitive Conversions 162 */ 163 short shortValue() { 164 return cast(short)value; 165 } 166 167 override string toString() { 168 return super.toString(); 169 } 170 }