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