|
| 1 | +package Parts; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import NumberParser.*; |
| 6 | +import NumberParser.Number; |
| 7 | +import UnitParser.*; |
| 8 | +import InternalNumberParser.CSharpAdaptation.*; |
| 9 | + |
| 10 | +@SuppressWarnings("serial") |
| 11 | +public class NumberParser |
| 12 | +{ |
| 13 | + public static void main(String[] args) |
| 14 | + { |
| 15 | + StartTest(); |
| 16 | + } |
| 17 | + |
| 18 | + public static void StartTest() |
| 19 | + { |
| 20 | + System.out.println("-------------- NumberParser --------------"); |
| 21 | + System.out.println(); |
| 22 | + |
| 23 | + //------ There are 4 main classes (NumberX) which take care of different actions. |
| 24 | + |
| 25 | + //--- Number is the simplest and lightest one. |
| 26 | + PrintSampleItem("Ini1", new Number(123.45678945699)); |
| 27 | + PrintSampleItem("Ini2", new Number(Double.MAX_VALUE, Integer.MAX_VALUE)); |
| 28 | + |
| 29 | + //--- NumberD is the simplest version supporting any numeric type. |
| 30 | + PrintSampleItem("Ini3", new NumberD(1.5634456f)); |
| 31 | + PrintSampleItem("Ini4", new NumberD(555555555555555555l, NumericTypes.Long)); |
| 32 | + |
| 33 | + //--- NumberO can deal with different numeric types at the same time. |
| 34 | + PrintSampleItem("Ini5", new NumberO(1567894563321.5634456, OtherTypes.IntegerTypes)); |
| 35 | + ArrayList<NumericTypes> others = new ArrayList<NumericTypes>() |
| 36 | + {{ |
| 37 | + add(NumericTypes.Short); |
| 38 | + add(NumericTypes.Double); |
| 39 | + }}; |
| 40 | + |
| 41 | + PrintSampleItem |
| 42 | + ( |
| 43 | + "Ini6", new NumberO |
| 44 | + ( |
| 45 | + new NumberO(new NumberD(53264485)), others |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + //--- NumberP is the only one extracting numeric information from strings. |
| 50 | + PrintSampleItem("Ini7", new NumberP("12555555.2", new ParseConfig(NumericTypes.Byte))); |
| 51 | + PrintSampleItem |
| 52 | + ( |
| 53 | + "Ini8", new NumberP |
| 54 | + ( |
| 55 | + "1 00 00 000", new ParseConfig() |
| 56 | + {{ |
| 57 | + setParseType(ParseTypes.ParseThousandsStrict); |
| 58 | + }} |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + //------ All the NumberX classes support the most common operations. |
| 63 | + |
| 64 | + //--- Arithmetic operators. |
| 65 | + PrintSampleItem("Op1", Number.Addition(new Number(1.233333658789), new Number(0.0000000012))); |
| 66 | + PrintSampleItem |
| 67 | + ( |
| 68 | + "Op2", Number.Division |
| 69 | + ( |
| 70 | + Number.Multiplication |
| 71 | + ( |
| 72 | + new Number(1000.0), new Number(new NumberD(55555555555555555555.55555)) |
| 73 | + ), |
| 74 | + new Number(new NumberP("1e-350")) |
| 75 | + ) |
| 76 | + ); |
| 77 | + |
| 78 | + //--- Comparison operators. |
| 79 | + PrintSampleItem |
| 80 | + ( |
| 81 | + "Op3", |
| 82 | + ( |
| 83 | + new Number(555.0, -3).equals |
| 84 | + ( |
| 85 | + new Number(new NumberP("0.555")) |
| 86 | + ) |
| 87 | + ? new Number(1.0) : new Number() |
| 88 | + ) |
| 89 | + ); |
| 90 | + PrintSampleItem |
| 91 | + ( |
| 92 | + "Op4", |
| 93 | + ( |
| 94 | + new Number(123.5).lessOrEqualThan |
| 95 | + ( |
| 96 | + new Number(new NumberD(123)) |
| 97 | + ) |
| 98 | + ? new Number(1.0) : new Number() |
| 99 | + ) |
| 100 | + ); |
| 101 | + |
| 102 | + //------ Math2 contains NumberX-adapted versions of all the .NET System.Math methods. |
| 103 | + |
| 104 | + //--- The expectations of the corresponding native method have to be met, otherwise an error would be triggered. |
| 105 | + PrintSampleItem("Math1", Math2.Max(new NumberD(999), new NumberD(1.234))); //Valid scenario for Math.Max (int implicitly convertible to double). |
| 106 | + PrintSampleItem("Math2", Math2.Pow(new NumberD(5555555555l, 500), new NumberD(5.3))); //Error. 5555555555*10^500 is outside the Math.Pow supported range. |
| 107 | + |
| 108 | + //--- If the target range is met, using the expected format isn't always required. |
| 109 | + PrintSampleItem("Math3", Math2.Log(new NumberD(20, 3))); //No error despite calling Math.Log with decimal when it expects double. |
| 110 | + PrintSampleItem("Math4", Math2.Sin(new NumberD('e'))); //No error despite calling Math.Sin with char when it expects double. |
| 111 | + |
| 112 | + |
| 113 | + //------ Math2 also includes other mathematical methods which I developed completely from scratch. |
| 114 | + |
| 115 | + //--- The PowDecimal/SqrtDecimal algorithms only rely on the decimal type and are more precise than the native versions. |
| 116 | + //--- You can find more information about these algorithms in https://varocarbas.com/fractional_exponentiation/. |
| 117 | + PrintSampleItem("Math5", Math2.PowDecimal(new Number(0.0000000000000001), 1.234567895)); |
| 118 | + PrintSampleItem("Math6", Math2.SqrtDecimal(new Number(9999999999999999999.0, 500))); |
| 119 | + |
| 120 | + //-- RoundExact/TruncateExact can deal with multiple rounding/truncating scenarios which aren't supported by the native methods. |
| 121 | + PrintSampleItem |
| 122 | + ( |
| 123 | + "Math7", Math2.RoundExact |
| 124 | + ( |
| 125 | + new Number(124555897.5500008), 4, RoundType.AlwaysToEven, |
| 126 | + RoundSeparator.BeforeDecimalSeparator |
| 127 | + ) |
| 128 | + ); |
| 129 | + PrintSampleItem("Math8", Math2.TruncateExact(new Number(5.123999), 3)); |
| 130 | + |
| 131 | + //-- GetPolynomialFit/ApplyPolynomialFit allow to perform regression analysis (2nd degree polynomial fits created via least squares). |
| 132 | + PrintSampleItem |
| 133 | + ( |
| 134 | + "Math9", Math2.ApplyPolynomialFit |
| 135 | + ( |
| 136 | + Math2.GetPolynomialFit |
| 137 | + ( |
| 138 | + new NumberD[] { new NumberD(1), new NumberD(2), new NumberD(4) }, |
| 139 | + new NumberD[] { new NumberD(1), new NumberD(4), new NumberD(16) } |
| 140 | + ), |
| 141 | + new NumberD(3) |
| 142 | + ) |
| 143 | + ); |
| 144 | + |
| 145 | + //-- Factorial calculates the factorial of any integer number up to 100000. |
| 146 | + PrintSampleItem("Math10", Math2.Factorial(new NumberD(25))); |
| 147 | + |
| 148 | + |
| 149 | + //------ Other FlexibleParser parts. |
| 150 | + //All the FlexibleParser parts are independent among each other and only the corresponding DLL file needs to be referred. |
| 151 | + //On the other, codes relying on various parts can take advantage of certain compatibility among their main classes. |
| 152 | + |
| 153 | + //--- UnitParser. |
| 154 | + PrintSampleItem("UP1", new Number(new UnitP("12.3 MabA"))); |
| 155 | + PrintSampleItem("UP2", new NumberD(new UnitP(0.01, SIPrefixes.Micro.toString() + Units.Second.toString()))); |
| 156 | + PrintSampleItem("UP3", new NumberP(new UnitP("Error"))); |
| 157 | + |
| 158 | + |
| 159 | + System.out.println(); |
| 160 | + System.out.println("------------------------------------------"); |
| 161 | + System.out.println(); |
| 162 | + System.out.println(); |
| 163 | + } |
| 164 | + |
| 165 | + static void PrintSampleItem(String sampleId, Object numberX) |
| 166 | + { |
| 167 | + System.out.println |
| 168 | + ( |
| 169 | + sampleId + " -- " + |
| 170 | + ( |
| 171 | + numberX == null ? " " : |
| 172 | + numberX.getClass().getName().replace |
| 173 | + ( |
| 174 | + "NumberParser.", "" |
| 175 | + ) |
| 176 | + ) |
| 177 | + //Each ToString() method outputs what the given NumberX needs. |
| 178 | + + " - " + CSharpOtherNP.SpecificNumberXToString(numberX) |
| 179 | + + System.lineSeparator() |
| 180 | + ); |
| 181 | + } |
| 182 | +} |
0 commit comments