[Webfunds-commits] java/webfunds/ricardian Share.java Contract.java Currency.java Support.java

Ian Grigg iang@cypherpunks.ai
Sun, 24 Sep 2000 19:12:09 -0400 (AST)


iang        00/09/24 19:12:09

  Modified:    webfunds/ricardian Contract.java Currency.java Support.java
  Added:       webfunds/ricardian Share.java
  Log:
  1. moved all units stuff into Contract from Currency.  All new contracts
     should use [issue] power=2 to achieve a standard decimal unit.  If
     an older contract, Contract and Currency both analyse and fall back
     to the factor and other versions...  Factor could still be used for
     non decimal contracts, but the applicability of that is not apparent.
  2. added Share as a new form of Contract.

Revision  Changes    Path
1.44      +69 -10    java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- Contract.java	2000/09/17 19:41:27	1.43
+++ Contract.java	2000/09/24 23:12:08	1.44
@@ -1,4 +1,4 @@
-/* $Id: Contract.java,v 1.43 2000/09/17 19:41:27 iang Exp $
+/* $Id: Contract.java,v 1.44 2000/09/24 23:12:08 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -62,8 +62,6 @@
  * <a href="http://www.systemics.com/docs/ricardo/issuer/contract.html">
  * http://www.systemics.com/docs/ricardo/issuer/contract.html</a>
  * for the specification of a contract.
- *
- * NOTE that this was erroneously part of SOX hierarchy.
  */
 
 public class Contract
@@ -96,12 +94,15 @@
     protected   String  name;
     protected   String  fileName;
     protected   String  type = null;
+
+    protected   int           power;
+    protected   double        factor;
+    private     boolean       isPower = false;
+    public      int           getPower()        { return power; }
+    public      boolean       isPower()         { return isPower; }
+    protected   void          setPower(int p)   { power = p; isPower = true; }
+    public      double        getFactor()       { return factor; }
 
-    private     int     power = 0;
-    private     boolean isPower = false;
-    public      int     getPower()        { return power; }
-    public      boolean isPower()         { return isPower; }
-    protected   void    setPower(int p)   { power = p; isPower = true; }
 
 
     protected String logfix = "     C ";
@@ -168,8 +169,66 @@
             throw new ContractException(ContractException.NOT_A_CONTRACT,
                                         "no [issue] type");
 
+        readAndSetPower();
+    }
+
+    /**
+     *  Units of account / contract used to be part of the
+     *  Currency contract, but have been since widened to
+     *  be a basic function of all contracts (at least,
+     *  as far as decimal goes).
+     *
+     *  Here, if we have an [issue] power, this is the
+     *  number of digits after the decimal point.
+     */
+    public void readAndSetPower()
+        throws ContractException
+    {
+        String s;
+        s = getField("issue", "power");
+        if (s != null && s.length() > 0)
+        {
+System.err.println("got new power: " + s);
+            int p = Support.powerInt(s);
+System.err.println("set new power: " + p);
+            setPower(p);
+            this.factor = Support.power2factorDouble(p);
+            return ;
+        }
+
+        if (getType() == CURRENCY_TYPE)    // let Currency sort out old ones
+            return ;
+
+        setPower(0);
+        this.factor = 1;
+    }
+
+
+    /**
+     *  unitsOfAccount = dollars, unitsOfContract = cents
+     */
+    public long getUnitsOfContract(double amount)
+    {
+// System.err.println(getField("currency", "factor"));
+// System.err.println("Fraction = " + factor);
+// System.err.println("Amount   = " + amount);
+// System.err.println("Amount/Fraction = " + Math.round(amount/factor));
+        return Math.round(amount/factor);
+    }
+
+    /**
+     *  Consider casting the result to a float to clean FP errors.
+     */
+    //amount = dollars, units = cents
+    public double getUnitsOfAccount(long units)
+    {
+// System.err.println("returning " + units + " * " + factor + " == " +
+//                                                  ( units * factor));
+        return units * factor;
     }
 
+
+
     public String fileNameFromDigest(byte[] hash)
     {
         String s = Base64.encode(hash);
@@ -440,10 +499,10 @@
         {
             case CURRENCY_TYPE:
                 return new Currency(con, loc, my);
+            case SHARE_TYPE:
+                return new Share(con, loc, my);
             case BOND_TYPE:
                 //return new Bond(con, loc, my);
-            case SHARE_TYPE:
-                //return new Share(con, loc, my);
             case SERVER_TYPE:
                 return new ServerContract(con, loc, my);
         }



1.9       +28 -13    java/webfunds/ricardian/Currency.java

Index: Currency.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Currency.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Currency.java	2000/09/16 16:29:56	1.8
+++ Currency.java	2000/09/24 23:12:08	1.9
@@ -1,5 +1,5 @@
 /*
- * $Id: Currency.java,v 1.8 2000/09/16 16:29:56 iang Exp $
+ * $Id: Currency.java,v 1.9 2000/09/24 23:12:08 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -16,11 +16,20 @@
         throws ContractException
     {
         super(contractD, localD, userD);
+
+        if (isPower())
+            return ;
+
+        /*
+         *  Check for an older Factor.
+         *  Newer [issue] power overrides this.
+         */
         String s;
         s = getField("issue", "power");
-        if (s == null || s.length() == 0)
-            s = getField("currency", "power");
+        if (s != null && s.length() > 0)         // already set in parent
+            return ;
 
+        s = getField("currency", "decimal_power");
         if (s != null && s.length() > 0)
         {
 // System.err.println("got new power: " + s);
@@ -28,27 +37,32 @@
 // System.err.println("set new power: " + p);
             setPower(p);
             this.factor = Support.power2factorDouble(p);
+            return ;
         }
-        else
-        {
-            s = getField("currency", "factor");
-            this.factor = Support.factorDouble(s);
-            try {
+
+        s = getField("currency", "factor");
+        if (s == null || s.length() == 0)
+            throw new ContractException(ContractException.NOT_A_CONTRACT,
+                     "currency must have power or factor");
+
+        // found the old factor, last chance!
+        this.factor = Support.factorDouble(s);
+//        try {
                 int p = Support.factor2powerInt(this.factor);
                 setPower(p);
-            } catch (ContractException ex) {
+//        } catch (ContractException ex) {
 // System.err.println("not a power: " + ex);
-            }
-        }
+//        }
     }
 
-    protected final double factor;
+    // protected final double factor;
     /** getting the fraction will be useful if working with prices */
-    public double getFactor() { return factor; }
+    // public double getFactor() { return factor; }
 
     // amount = dollars, units = cents
     public long getUnits(double amount)
     {
+System.err.println("Deprecated: getUnits(" + amount + ")");
 // System.err.println(getField("currency", "factor"));
 // System.err.println("Fraction = " + factor);
 // System.err.println("Amount   = " + amount);
@@ -62,6 +76,7 @@
     //amount = dollars, units = cents
     public double getAmount(long units)
     {
+System.err.println("Deprecated: getAmount(" + units + ")");
 // System.err.println("returning " + units + " * " + factor + " == " +
 //                                                  ( units * factor));
         return units * factor;



1.5       +6 -1      java/webfunds/ricardian/Support.java

Index: Support.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Support.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Support.java	2000/09/05 19:39:19	1.4
+++ Support.java	2000/09/24 23:12:08	1.5
@@ -1,5 +1,5 @@
 /*
- * $Id: Support.java,v 1.4 2000/09/05 19:39:19 iang Exp $
+ * $Id: Support.java,v 1.5 2000/09/24 23:12:08 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -15,6 +15,9 @@
 import java.net.URL;
 import webfunds.sox.*;
 
+/**
+ *  General static support for Contracts.
+ */
 public class Support
 {
 
@@ -65,6 +68,8 @@
         return s1.equals(s2) ;
     }
 
+
+/////////  Units of Account and Contract  ////////////////////////////
 
     /**
      *  @return double value found in String



1.1                  java/webfunds/ricardian/Share.java

Index: Share.java
===================================================================
/*
 * $Id: Share.java,v 1.1 2000/09/24 23:12:08 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */

package webfunds.ricardian;

public class Share
    extends Contract
{
    public Share(byte[] contractD, byte[] localD, byte[] userD)
        throws ContractException
    {
        super(contractD, localD, userD);

    }

    public int getType()    { return Contract.CURRENCY_TYPE; }
}