< prev index next >

make/src/classes/build/tools/generatecurrencydata/GenerateCurrencyData.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 104     // mask for the numeric code of the currency
 105     private static final int NUMERIC_CODE_MASK = 0x000FFC00;
 106     // shift count for the numeric code of the currency
 107     private static final int NUMERIC_CODE_SHIFT = 10;
 108 
 109     // generated data
 110     private static int[] mainTable = new int[A_TO_Z * A_TO_Z];
 111 
 112     private static final int maxSpecialCases = 30;
 113     private static int specialCaseCount = 0;
 114     private static long[] specialCaseCutOverTimes = new long[maxSpecialCases];
 115     private static String[] specialCaseOldCurrencies = new String[maxSpecialCases];
 116     private static String[] specialCaseNewCurrencies = new String[maxSpecialCases];
 117     private static int[] specialCaseOldCurrenciesDefaultFractionDigits = new int[maxSpecialCases];
 118     private static int[] specialCaseNewCurrenciesDefaultFractionDigits = new int[maxSpecialCases];
 119     private static int[] specialCaseOldCurrenciesNumericCode = new int[maxSpecialCases];
 120     private static int[] specialCaseNewCurrenciesNumericCode = new int[maxSpecialCases];
 121 
 122     private static final int maxOtherCurrencies = 128;
 123     private static int otherCurrenciesCount = 0;
 124     private static StringBuffer otherCurrencies = new StringBuffer();
 125     private static int[] otherCurrenciesDefaultFractionDigits = new int[maxOtherCurrencies];
 126     private static int[] otherCurrenciesNumericCode= new int[maxOtherCurrencies];
 127 
 128     // date format for parsing cut-over times
 129     private static SimpleDateFormat format;
 130 
 131     // Minor Units
 132     private static String[] currenciesWithDefinedMinorUnitDecimals =
 133         new String[SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS + 1];
 134     private static String currenciesWithMinorUnitsUndefined;
 135 
 136     public static void main(String[] args) {
 137 
 138         // Look for "-o outputfilename" option
 139         if ( args.length == 2 && args[0].equals("-o") ) {
 140             try {
 141                 out = new DataOutputStream(new FileOutputStream(args[1]));
 142             } catch ( FileNotFoundException e ) {
 143                 System.err.println("Error: " + e.getMessage());
 144                 e.printStackTrace(System.err);


 301 
 302     private static void buildOtherTables() {
 303         if (validCurrencyCodes.length() % 7 != 6) {
 304             throw new RuntimeException("\"all\" entry has incorrect size");
 305         }
 306         for (int i = 0; i < (validCurrencyCodes.length() + 1) / 7; i++) {
 307             if (i > 0 && validCurrencyCodes.charAt(i * 7 - 1) != '-') {
 308                 throw new RuntimeException("incorrect separator in \"all\" entry");
 309             }
 310             String currencyCode = validCurrencyCodes.substring(i * 7, i * 7 + 3);
 311             int numericCode = Integer.parseInt(
 312                 validCurrencyCodes.substring(i * 7 + 3, i * 7 + 6));
 313             checkCurrencyCode(currencyCode);
 314             int tableEntry = mainTable[(currencyCode.charAt(0) - 'A') * A_TO_Z + (currencyCode.charAt(1) - 'A')];
 315             if (tableEntry == INVALID_COUNTRY_ENTRY ||
 316                     (tableEntry & SPECIAL_CASE_COUNTRY_MASK) != 0 ||
 317                     (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) != (currencyCode.charAt(2) - 'A')) {
 318                 if (otherCurrenciesCount == maxOtherCurrencies) {
 319                     throw new RuntimeException("too many other currencies");
 320                 }
 321                 if (otherCurrencies.length() > 0) {
 322                     otherCurrencies.append('-');
 323                 }
 324                 otherCurrencies.append(currencyCode);
 325                 otherCurrenciesDefaultFractionDigits[otherCurrenciesCount] = getDefaultFractionDigits(currencyCode);
 326                 otherCurrenciesNumericCode[otherCurrenciesCount] = getNumericCode(currencyCode);
 327                 otherCurrenciesCount++;
 328             }
 329         }
 330     }
 331 
 332     private static void checkCurrencyCode(String currencyCode) {
 333         if (currencyCode.length() != 3) {
 334             throw new RuntimeException("illegal length for currency code: " + currencyCode);
 335         }
 336         for (int i = 0; i < 3; i++) {
 337             char aChar = currencyCode.charAt(i);
 338             if ((aChar < 'A' || aChar > 'Z') && !currencyCode.equals("XB5")) {
 339                 throw new RuntimeException("currency code contains illegal character: " + currencyCode);
 340             }
 341         }
 342         if (validCurrencyCodes.indexOf(currencyCode) == -1) {
 343             throw new RuntimeException("currency code not listed as valid: " + currencyCode);
 344         }
 345     }
 346 
 347     private static void writeOutput() throws IOException {
 348         out.writeInt(MAGIC_NUMBER);
 349         out.writeInt(Integer.parseInt(formatVersion));
 350         out.writeInt(Integer.parseInt(dataVersion));
 351         writeIntArray(mainTable, mainTable.length);
 352         out.writeInt(specialCaseCount);
 353         writeLongArray(specialCaseCutOverTimes, specialCaseCount);
 354         writeStringArray(specialCaseOldCurrencies, specialCaseCount);
 355         writeStringArray(specialCaseNewCurrencies, specialCaseCount);
 356         writeIntArray(specialCaseOldCurrenciesDefaultFractionDigits, specialCaseCount);
 357         writeIntArray(specialCaseNewCurrenciesDefaultFractionDigits, specialCaseCount);
 358         writeIntArray(specialCaseOldCurrenciesNumericCode, specialCaseCount);
 359         writeIntArray(specialCaseNewCurrenciesNumericCode, specialCaseCount);
 360         out.writeInt(otherCurrenciesCount);
 361         out.writeUTF(otherCurrencies.toString());
 362         writeIntArray(otherCurrenciesDefaultFractionDigits, otherCurrenciesCount);
 363         writeIntArray(otherCurrenciesNumericCode, otherCurrenciesCount);
 364     }
 365 
 366     private static void writeIntArray(int[] ia, int count) throws IOException {
 367         for (int i = 0; i < count; i ++) {
 368             out.writeInt(ia[i]);
 369         }
 370     }
 371 
 372     private static void writeLongArray(long[] la, int count) throws IOException  {
 373         for (int i = 0; i < count; i ++) {
 374             out.writeLong(la[i]);










 375         }
 376     }
 377 
 378     private static void writeStringArray(String[] sa, int count) throws IOException  {
 379         for (int i = 0; i < count; i ++) {
 380             String str = (sa[i] != null) ? sa[i] : "";

 381             out.writeUTF(str);


 382         }
 383     }

 384 }
   1 /*
   2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 104     // mask for the numeric code of the currency
 105     private static final int NUMERIC_CODE_MASK = 0x000FFC00;
 106     // shift count for the numeric code of the currency
 107     private static final int NUMERIC_CODE_SHIFT = 10;
 108 
 109     // generated data
 110     private static int[] mainTable = new int[A_TO_Z * A_TO_Z];
 111 
 112     private static final int maxSpecialCases = 30;
 113     private static int specialCaseCount = 0;
 114     private static long[] specialCaseCutOverTimes = new long[maxSpecialCases];
 115     private static String[] specialCaseOldCurrencies = new String[maxSpecialCases];
 116     private static String[] specialCaseNewCurrencies = new String[maxSpecialCases];
 117     private static int[] specialCaseOldCurrenciesDefaultFractionDigits = new int[maxSpecialCases];
 118     private static int[] specialCaseNewCurrenciesDefaultFractionDigits = new int[maxSpecialCases];
 119     private static int[] specialCaseOldCurrenciesNumericCode = new int[maxSpecialCases];
 120     private static int[] specialCaseNewCurrenciesNumericCode = new int[maxSpecialCases];
 121 
 122     private static final int maxOtherCurrencies = 128;
 123     private static int otherCurrenciesCount = 0;
 124     private static String[] otherCurrencies = new String[maxOtherCurrencies];
 125     private static int[] otherCurrenciesDefaultFractionDigits = new int[maxOtherCurrencies];
 126     private static int[] otherCurrenciesNumericCode= new int[maxOtherCurrencies];
 127 
 128     // date format for parsing cut-over times
 129     private static SimpleDateFormat format;
 130 
 131     // Minor Units
 132     private static String[] currenciesWithDefinedMinorUnitDecimals =
 133         new String[SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS + 1];
 134     private static String currenciesWithMinorUnitsUndefined;
 135 
 136     public static void main(String[] args) {
 137 
 138         // Look for "-o outputfilename" option
 139         if ( args.length == 2 && args[0].equals("-o") ) {
 140             try {
 141                 out = new DataOutputStream(new FileOutputStream(args[1]));
 142             } catch ( FileNotFoundException e ) {
 143                 System.err.println("Error: " + e.getMessage());
 144                 e.printStackTrace(System.err);


 301 
 302     private static void buildOtherTables() {
 303         if (validCurrencyCodes.length() % 7 != 6) {
 304             throw new RuntimeException("\"all\" entry has incorrect size");
 305         }
 306         for (int i = 0; i < (validCurrencyCodes.length() + 1) / 7; i++) {
 307             if (i > 0 && validCurrencyCodes.charAt(i * 7 - 1) != '-') {
 308                 throw new RuntimeException("incorrect separator in \"all\" entry");
 309             }
 310             String currencyCode = validCurrencyCodes.substring(i * 7, i * 7 + 3);
 311             int numericCode = Integer.parseInt(
 312                 validCurrencyCodes.substring(i * 7 + 3, i * 7 + 6));
 313             checkCurrencyCode(currencyCode);
 314             int tableEntry = mainTable[(currencyCode.charAt(0) - 'A') * A_TO_Z + (currencyCode.charAt(1) - 'A')];
 315             if (tableEntry == INVALID_COUNTRY_ENTRY ||
 316                     (tableEntry & SPECIAL_CASE_COUNTRY_MASK) != 0 ||
 317                     (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) != (currencyCode.charAt(2) - 'A')) {
 318                 if (otherCurrenciesCount == maxOtherCurrencies) {
 319                     throw new RuntimeException("too many other currencies");
 320                 }
 321                 otherCurrencies[otherCurrenciesCount] = currencyCode;



 322                 otherCurrenciesDefaultFractionDigits[otherCurrenciesCount] = getDefaultFractionDigits(currencyCode);
 323                 otherCurrenciesNumericCode[otherCurrenciesCount] = getNumericCode(currencyCode);
 324                 otherCurrenciesCount++;
 325             }
 326         }
 327     }
 328 
 329     private static void checkCurrencyCode(String currencyCode) {
 330         if (currencyCode.length() != 3) {
 331             throw new RuntimeException("illegal length for currency code: " + currencyCode);
 332         }
 333         for (int i = 0; i < 3; i++) {
 334             char aChar = currencyCode.charAt(i);
 335             if ((aChar < 'A' || aChar > 'Z') && !currencyCode.equals("XB5")) {
 336                 throw new RuntimeException("currency code contains illegal character: " + currencyCode);
 337             }
 338         }
 339         if (validCurrencyCodes.indexOf(currencyCode) == -1) {
 340             throw new RuntimeException("currency code not listed as valid: " + currencyCode);
 341         }
 342     }
 343 
 344     private static void writeOutput() throws IOException {
 345         out.writeInt(MAGIC_NUMBER);
 346         out.writeInt(Integer.parseInt(formatVersion));
 347         out.writeInt(Integer.parseInt(dataVersion));
 348         writeIntArray(mainTable, mainTable.length);
 349         out.writeInt(specialCaseCount);
 350         writeSpecialCaseEntries();






 351         out.writeInt(otherCurrenciesCount);
 352         writeOtherCurrencies();


 353     }
 354 
 355     private static void writeIntArray(int[] ia, int count) throws IOException {
 356         for (int i = 0; i < count; i++) {
 357             out.writeInt(ia[i]);
 358         }
 359     }
 360 
 361     private static void writeSpecialCaseEntries() throws IOException {
 362         for (int index = 0; index < specialCaseCount; index++) {
 363             out.writeLong(specialCaseCutOverTimes[index]);
 364             String str = (specialCaseOldCurrencies[index] != null)
 365                     ? specialCaseOldCurrencies[index] : "";
 366             out.writeUTF(str);
 367             str = (specialCaseNewCurrencies[index] != null)
 368                     ? specialCaseNewCurrencies[index] : "";
 369             out.writeUTF(str);
 370             out.writeInt(specialCaseOldCurrenciesDefaultFractionDigits[index]);
 371             out.writeInt(specialCaseNewCurrenciesDefaultFractionDigits[index]);
 372             out.writeInt(specialCaseOldCurrenciesNumericCode[index]);
 373             out.writeInt(specialCaseNewCurrenciesNumericCode[index]);
 374         }
 375     }
 376 
 377     private static void writeOtherCurrencies() throws IOException {
 378         for (int index = 0; index < otherCurrenciesCount; index++) {
 379             String str = (otherCurrencies[index] != null)
 380                     ? otherCurrencies[index] : "";
 381             out.writeUTF(str);
 382             out.writeInt(otherCurrenciesDefaultFractionDigits[index]);
 383             out.writeInt(otherCurrenciesNumericCode[index]);
 384         }
 385     }
 386 
 387 }
< prev index next >