2 *******************************************************************************
3 * Copyright (C) 1996-2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 package com.ibm.icu.dev.tool.timescale;
11 import com.ibm.icu.math.BigDecimal;
12 import com.ibm.icu.text.MessageFormat;
13 import com.ibm.icu.util.UniversalTimeScale;
16 * This class calculates the minimum and maximum values which can be
17 * used as arguments to <code>toLong</code> and <code>from</code>.
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.
24 * @see com.ibm.icu.util.UniversalTimeScale
26 public class CalculateLimits {
29 * The default constructor.
31 public CalculateLimits()
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.
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.
45 * @see com.ibm.icu.util.UniversalTimeScale
47 * @param args - the command line arugments
49 public static void main(String[] args)
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};
56 System.out.println("\nTo, 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();
65 limitArgs[2] = min.toString();
66 limitArgs[3] = max.toString();
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);
76 min = minTrunc.subtract(minResidue.min(half));
77 max = maxTrunc.add(maxResidue.min(half));
78 limitArgs[0] = min.toString();
79 limitArgs[1] = max.toString();
81 System.out.println(fmt.format(limitArgs));
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!");
88 if(UniversalTimeScale.toLong(UniversalTimeScale.from(maxLong, scale), scale) != maxLong) {
89 System.out.println("OOPS: max didn't round trip!");
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");
97 if(UniversalTimeScale.toLong(max.longValue(), scale) != maxLong) {
98 System.out.println("OOPS: toLong(toMax) != fromMax");