[Webfunds-commits] java/webfunds/util Support.java

Ian Grigg iang@cypherpunks.ai
Fri, 6 Apr 2001 11:28:33 -0400 (AST)


iang        01/04/06 11:28:32

  Modified:    webfunds/ricardian Support.java
               webfunds/util Support.java
  Added:       webfunds/ricardian Units.java
  Log:
  1. moved all the units of account/contract code into webfunds.ricardian.Units
     was in catchall module webfunds.ricardian.Support
  2. added getFile to webfunds.util.Support (getURL is to go, doesn't use CM)
  3. deprecated all contents of webfunds.ricardian.Support

Revision  Changes    Path
1.9       +8 -1      java/webfunds/ricardian/Support.java

Index: Support.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Support.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Support.java	2001/03/28 00:13:45	1.8
+++ Support.java	2001/04/06 15:28:31	1.9
@@ -1,5 +1,5 @@
 /*
- * $Id: Support.java,v 1.8 2001/03/28 00:13:45 gelderen Exp $
+ * $Id: Support.java,v 1.9 2001/04/06 15:28:31 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -28,6 +28,8 @@
     /**
      * Read the url into a byte array
      * @return an array of the contents found at url
+     *
+     * XXX: DEPRECATE!  does not use CommsManager
      */
     static byte[] getURL(URL url)
         throws IOException
@@ -42,6 +44,8 @@
     /**
      * Read the url into a byte array
      * @return an array of the contents found at url
+     *
+     * XXX: DEPRECATE!  now in webfunds.util.support
      */
     static byte[] getFile(File file)
         throws IOException
@@ -55,6 +59,8 @@
 
     /**
      * @return true if the byte arrays are equal, false if not
+     *
+     * XXX: DEPRECATE!  now in webfunds.util.support
      */
     static boolean equals(byte[] b1, byte[] b2)
     {
@@ -75,6 +81,7 @@
 
 
 /////////  Units of Account and Contract  ////////////////////////////
+/////////  XXX: DEPRECATE!  now in Units  ////////////////////////////
 
     /**
      *  @return double value found in String



1.1                  java/webfunds/ricardian/Units.java

Index: Units.java
===================================================================
/*
 * $Id: Units.java,v 1.1 2001/04/06 15:28:32 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.ricardian;

import java.util.Hashtable;
import java.net.URL;
import webfunds.sox.*;

/**
 *  Units of Account and and Units of Contract.
 *
 *  This code was in Support.java
 */
public class Units
{
    private Units() {}

    /**
     *  @return double value found in String
     *  @except ContractException interpreted as factor
     */
    static double factorDouble(String s)
        throws ContractException
    {
        double factor;
        try {
             factor = new Double(s).doubleValue();
        } catch (Exception ex) {
            throw new ContractException(ContractException.FRACTION_FAIL,    
                                        "factor <<" + s + ">> is not numeric");
        }
        return factor;
    }

    /**
     *  @return int value found in String
     *  @except ContractException interpreted as power
     */
    static int powerInt(String s)
        throws ContractException
    {
        int p;
        try {
             p = new Integer(s).intValue();
        } catch (Exception ex) {
            throw new ContractException(ContractException.FRACTION_FAIL,    
                                        "power <<" + s + ">> is not numeric");
        }
        return p;
    }

    /**
     *  @return factor value represented by power
     *  @except ContractException if the power is too large
     */
    static double power2factorDouble(int p)
        throws ContractException
    {
        String fr = power2factorString(p);
        return factorDouble(fr);
    }

    /**
     *  @return factor value represented by power, as String
     *  @except ContractException if the power is too large
     */
    static String power2factorString(int p)
        throws ContractException
    {

        if (! (-10 <= p && p <= 10) )
             throw new ContractException(ContractException.FRACTION_FAIL,
                                        "power is too big! " + p);

        String fr;
        if (p == 0)
            fr = "1";
        else if (p < 0)
        {
            fr = "1";
            while (p != 0)
            {
                 p++;
                 fr += "0";
            }
        }
        else // if (p > 0)
        {
            fr = "0.";
            while (--p > 0)
            {
                 fr += "0";
            }
            fr += "1";
        }

        return fr;
    }


    /**
     *  @return true if the double is close enough to one
     */
    static boolean equalsOne(double d)
    {
        return ((0.99999 <= d) && (d <= 1.00001));
    }


    /**
     *  Turn the factor into a power.
     *  Some factors (e.g. 0.5) are not powers, so you have to
     *  catch the exception and deal with it.
     *
     *  @return power represented by this factor
     *  @except ContractException if not a power (not multiple of 1)
     */
    static int factor2powerInt(double d)
        throws ContractException
    {

        String e = "double " + d + " is not a power!";

        if (equalsOne(d))
            return 0;

        int power;
        if (d < 1)
        {
            power = 0;
            while (! equalsOne(d))
            {
                 d *= 10;
                 if (d > 1 && !equalsOne(d))
                     throw new ContractException(
                                        ContractException.FRACTION_FAIL,
                                        e);
                 power += 1;
            }
        }
        else // if (d > 1)
        {
            power = 0;
            while (! equalsOne(d))
            {
                 d /= 10;
                 if (d < 1 && !equalsOne(d))
                     throw new ContractException(
                                        ContractException.FRACTION_FAIL,
                                        e);
                 power -= 1;
            }
        }
        return power;
    }

    /**
     *  Some factors (e.g. 0.5) are not powers, so you have to
     *  catch the exception and deal with it.
     *
     *  @return power represented by this factor, as String
     *  @except ContractException if not a power (not multiple of 1)
     */
    static String factor2powerString(double d)
        throws ContractException
    {
        int p = factor2powerInt(d);
        return p + "";
    }

    public static String getMinorWords(int power)
    {
        int minors = power % 3;
        String s = "";
        if (minors == 1)
            s += "tens";
        else if (minors == 2)
            s += "hundreds";
        return s;
    }

    public static final String bigStrings[] =
                { "thousands", "millions", "billions", "trillions" };

    /**
     * return words like "tens of thousands" ...
     * only works for powers less than 0.
     */
    public static String getWords(int power)
    {
        power = -power;
        if (power <= 0)
            return "";

        String s = getMinorWords(power);
        int bigs = power / 3;
        if (bigs == 0)
            return s;

        if (s.length() > 0)
            s += " of ";

        if (bigs-- > bigStrings.length)
            throw new IllegalArgumentException("power is too big: " + power);
        return s + bigStrings[bigs];
    }

    /**
     *  Handy call to return a string with a number of repeats.
     */
    public static String repeats(int i, String s)
    {
        int z = (i < 0) ? -i : i;
        String zeroes = "";
        while (z-- > 0)
            zeroes += s;
        return zeroes;
    }


    /**
     *  @return a suitable DecimalFactor pattern for the power
     */
    public static String power2DecimalFormatPattern(int p)
    {
        if (p == 0)
            return "0";

        if (p < 0)
            return "0" + repeats(p, "#");
        else // if (p > 0)
            return "0." + repeats(p, "0");
    }



    public static void main(String[] args)
        throws ContractException
    {
        int ints[] = { 0, 1, -1, 2, -2, 10, -10 };
        for (int i = 0; i < ints.length; i++)
        {
            int test = ints[i];
            System.err.println(i + ": " + test);
            String s = power2factorString(test);
            System.err.println("   factor string " + s);
            double d = power2factorDouble(test);
            System.err.println("   factor double " + d);

            int p = factor2powerInt(d);
            System.err.println("   power int     " + p);
            s = factor2powerString(d);
            System.err.println("   power string  " + s);
            s = power2DecimalFormatPattern(p);
            System.err.println("   DF pattern    " + s);

            if (p != test)
                throw new RuntimeException("test == " + test + "   p == " + p);
        }

        int j = 1;
        while (true)
        {
            System.err.println(j + ": " + getWords(j));
            j--;
        }
        // will Panic
    }
}



1.2       +22 -1     java/webfunds/util/Support.java

Index: Support.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/util/Support.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Support.java	2001/04/02 02:22:43	1.1
+++ Support.java	2001/04/06 15:28:32	1.2
@@ -1,11 +1,17 @@
 /*
- * $Id: Support.java,v 1.1 2001/04/02 02:22:43 iang Exp $
+ * $Id: Support.java,v 1.2 2001/04/06 15:28:32 iang Exp $
  *
  * Copyright (c) 2000-2001 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
 package webfunds.util;
 
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+
 /**
  *  General static support for Stuff.
  */
@@ -32,4 +38,19 @@
 
         return s1.equals(s2) ;
     }
+
+    /**
+     * Read a file into a byte array
+     * @return an array of the contents found at File
+     */
+    static byte[] getFile(File file)
+        throws IOException
+    {
+        FileInputStream fis = new FileInputStream(file);
+        byte[] buf = new byte[fis.available()];
+        fis.read(buf);
+        fis.close();
+        return buf ;
+    }
+
 }