Debian Codegraph @

tools/misc/src/main/java/com/ibm/icu/dev/tool/timescale/CalculateLimits.java

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1996-2008, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
8 *
9 */
10
11 package com.ibm.icu.dev.tool.timescale;
12
13 import com.ibm.icu.math.BigDecimal;
14 import com.ibm.icu.text.MessageFormat;
15 import com.ibm.icu.util.UniversalTimeScale;
16
17 /**
18 * This class calculates the minimum and maximum values which can be
19 * used as arguments to <code>toLong</code> and <code>from</code>.
20 *
21 * NOTE: If you change the way in which these values are calculated, it
22 * may be necessary to disable to <code>toRangeCheck()</code> and
23 * <code>fromRangeCheck()</code> methods in the <code>UniversalTimeScale</code>
24 * for all of the calculations to run without throwing an error.
25 *
26 * @see com.ibm.icu.util.UniversalTimeScale
27 */
28 public class CalculateLimits {
29
30 /**
31 * The default constructor.
32 */
33 public CalculateLimits()
34 {
35 }
36
37 /**
38 * This method first calculates the <code>from</code> limits by
39 * passing <code>Long.MIN_VALUE</code> and <code>Long.MAX_VALUE</code> to
40 * the (internal) <code>toBigDecimalTrunc()</code> method. Any values outside
41 * of the range of a <code>long</code> are pinned.
42 *
43 * The mimimum and maximum values for <code>toLong</code> are calculated by passing
44 * the min and max values calculated above to <code>BigDecimalFrom()</code>. Because
45 * this method will round, the returned values are adjusted to take this into account.
46 *
47 * @see com.ibm.icu.util.UniversalTimeScale
48 *
49 * @param args - the command line arguments
50 */
51 public static void main(String[] args)
52 {
53 MessageFormat fmt = new MessageFormat("{0}L, {1}L, {2}L, {3}L");
54 BigDecimal universalMin = new BigDecimal(Long.MIN_VALUE);
55 BigDecimal universalMax = new BigDecimal(Long.MAX_VALUE);
56 Object limitArgs[] = {null, null, null, null};
57
58 System.out.println("\nTo, From limits:");
59
60 // from limits
61 for(int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
62 BigDecimal min = UniversalTimeScale.toBigDecimalTrunc(universalMin, scale).max(universalMin);
63 BigDecimal max = UniversalTimeScale.toBigDecimalTrunc(universalMax, scale).min(universalMax);
64 long minLong = min.longValue();
65 long maxLong = max.longValue();
66
67 limitArgs[2] = min.toString();
68 limitArgs[3] = max.toString();
69
70 // to limits
71 BigDecimal minTrunc = UniversalTimeScale.bigDecimalFrom(min, scale);
72 BigDecimal maxTrunc = UniversalTimeScale.bigDecimalFrom(max, scale);
73 BigDecimal minResidue = minTrunc.subtract(universalMin);
74 BigDecimal maxResidue = universalMax.subtract(maxTrunc);
75 long units = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.UNITS_VALUE);
76 BigDecimal half = new BigDecimal(units == 1? 0: units / 2 - 1);
77
78 min = minTrunc.subtract(minResidue.min(half));
79 max = maxTrunc.add(maxResidue.min(half));
80 limitArgs[0] = min.toString();
81 limitArgs[1] = max.toString();
82
83 System.out.println(fmt.format(limitArgs));
84
85 // round-trip test the from limits
86 if(UniversalTimeScale.toLong(UniversalTimeScale.from(minLong, scale), scale) != minLong) {
87 System.out.println("OOPS: min didn't round trip!");
88 }
89
90 if(UniversalTimeScale.toLong(UniversalTimeScale.from(maxLong, scale), scale) != maxLong) {
91 System.out.println("OOPS: max didn't round trip!");
92 }
93
94 // make sure that the to limits convert to the from limits
95 if(UniversalTimeScale.toLong(min.longValue(), scale) != minLong) {
96 System.out.println("OOPS: toLong(toMin) != fromMin");
97 }
98
99 if(UniversalTimeScale.toLong(max.longValue(), scale) != maxLong) {
100 System.out.println("OOPS: toLong(toMax) != fromMax");
101 }
102 }
103 }
104 }