/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil -*- * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Grendel mail/news client. * * The Initial Developer of the Original Code is Netscape Communications * Corporation. Portions created by Netscape are * Copyright (C) 1997 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ package calypso.util; /** A class for counting in a variety of bases. */ public class Abacus { /************************************************ * Formatting Strings ************************************************/ public final static String ones[]= {"zero","one ","two ","three ","four ","five ", "six ","seven ","eight ","nine ","ten "}; public final static String teens[]= {"ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ", "sixteen ","seventeen ","eighteen ","nineteen "}; public final static String tens[]= {"","ten ","twenty ","thirty ","forty ","fifty ", "sixty ","seventy ","eighty ","ninety ","hundred "}; public final static String bases[] = {"","hundred ","thousand ","million ","billion ","trillion ","quadrillion "}; protected final static String gAlphaChars = "abcdefghijklmnopqrstuvwxyz"; protected final static String gRomanCharsA = "ixcm"; protected final static String gRomanCharsB = "vld?"; protected final static String gHexChars = "0123456789abcdef"; protected final static String gBinaryChars = "01"; /************************************************ * Data members... ************************************************/ //notice that we don't have any? /** Formats the value as an alpha string * * * @return * @author gess 05-05-97 12:22pm * @notes */ public static String getAlphaString(int aValue) { return getSeriesString(Math.abs(aValue)-1,gAlphaChars,-1,gAlphaChars.length()); } /** * Convert the given integer value into a roman numeral string * * @param aValue - int to be converted to roman * @return new string * @author gess 05-05-97 12:53pm * @notes Those pesky romans used dashed above a value to multiply * that number by 1000. This has the unfortunate side effect * that without special font treatment, we cant represent * numbers in roman above 3999. * THIS METHOD HANDLES VALUES IN THE RANGE [1..3999]. * (any other value is converted to simple numeric format.) */ public static String getRomanString(int aValue) { StringBuffer addOn = new StringBuffer(); StringBuffer result = new StringBuffer(); StringBuffer decStr = new StringBuffer(); decStr.append(aValue); int len=decStr.length(); int romanPos=len; int n,digitPos; boolean negative=(aValue<0); aValue=Math.abs(aValue); for(digitPos=0;digitPos1) result.append(bases[expn]); } expn--; root/=1000; } if (0==result.length()) result.append("zero"); return result.toString(); } /** * Convert the given integer value into a series string. These are any * arbitrary but repeating pattern of characters. * * @param aValue - int to be converted to series string * @return new string * @author gess 05-05-97 12:53pm * @notes this method gets used for binary and hex conversion */ public static String getSeriesString(int aValue,String aCharSet,int anOffset,int aBase) { int ndex=0; int root=1; int next=aBase; int expn=1; StringBuffer result=new StringBuffer(); aValue=Math.abs(aValue); // must be positive here... while(next<=aValue) // scale up in baseN; exceed current value. { root=next; next*=aBase; expn++; } while(0!=(expn--)) { ndex = ((root<=aValue) && (0!=root)) ? (aValue/root): 0; aValue%=root; if(root>1) result.append(aCharSet.charAt(ndex+(1*anOffset))); else result.append(aCharSet.charAt(ndex)); root/=aBase; }; return result.toString(); } public static void PadPrint(int aValue,int width) { StringBuffer temp=new StringBuffer(); temp.append(aValue); PadPrint(temp.toString(),width); } public static void PadPrint(String aString, int aWidth) { System.out.print(aString); int padCount=aWidth-aString.length(); if(padCount>0) for(int i=0;i