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

Ian Grigg iang@cypherpunks.ai
Tue, 3 Oct 2000 20:38:59 -0400 (AST)


iang        00/10/03 20:38:59

  Modified:    webfunds/ricardian Contract.java Support.java
  Log:
  added some more support for fractions - methods to return units

Revision  Changes    Path
1.46      +45 -14    java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- Contract.java	2000/09/30 18:21:11	1.45
+++ Contract.java	2000/10/04 00:38:58	1.46
@@ -1,4 +1,4 @@
-/* $Id: Contract.java,v 1.45 2000/09/30 18:21:11 iang Exp $
+/* $Id: Contract.java,v 1.46 2000/10/04 00:38:58 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -75,13 +75,19 @@
                                SECT_ISSUE         = "issue",
                                SECT_KEYS          = "keys",
                                SECT_LOCAL         = "local",
-                               SECT_APPL          = "appl";
+                               SECT_APPL          = "appl",
 
-    protected static final int CURRENCY_TYPE   = 0;
-    protected static final int BOND_TYPE       = 1;
-    protected static final int SHARE_TYPE      = 2;
-    protected static final int SERVER_TYPE     = 3;
-    protected static final int UNKNOWN_TYPE    = -1;
+                               CURRENCY_WORD      = "currency",
+                               BOND_WORD          = "bond",
+                               SHARE_WORD         = "share",
+                               SERVER_WORD        = "server";
+
+    protected static final int
+                               CURRENCY_TYPE   = 0,
+                               BOND_TYPE       = 1,
+                               SHARE_TYPE      = 2,
+                               SERVER_TYPE     = 3,
+                               UNKNOWN_TYPE    = -1;
 
     protected   byte[] contractData;
     protected   byte[] localData;
@@ -188,9 +194,9 @@
         s = getField("issue", "power");
         if (s != null && s.length() > 0)
         {
-System.err.println("got new power: " + s);
+// System.err.println("got new power: " + s);
             int p = Support.powerInt(s);
-System.err.println("set new power: " + p);
+// System.err.println("set new power: " + p);
             setPower(p);
             this.factor = Support.power2factorDouble(p);
             return ;
@@ -227,8 +233,34 @@
         return units * factor;
     }
 
+    /**
+     *  Needs more support from the contract to do properly.
+     *  @return a printable name for the unit of contract (cents),
+     *  null if not available.
+     */
+    public String getNameOfUnitOfContract()
+    {
+        if (getType() == CURRENCY_TYPE)
+            return getField(CURRENCY_WORD,  "currency_fraction");
 
+        return null;
+    }
 
+    /**
+     *  Needs more support from the contract to do properly.
+     *  @return a printable name for the unit of account (dollars),
+     *  null if not available.
+     */
+    public String getNameOfUnitOfAccount()
+    {
+        if (getType() == CURRENCY_TYPE)
+            return getField(CURRENCY_WORD,  "currency_symbol");
+
+        return getName();
+    }
+
+
+
     public String fileNameFromDigest(byte[] hash)
     {
         String s = Base64.encode(hash);
@@ -683,7 +715,6 @@
     }
 
 
-
     /**
      *  Work out what type of contract this is.
      *  An unknown one is not an exception, just something
@@ -694,13 +725,13 @@
     {
         IniFileReader ini = new IniFileReader(contractData);
         String type = ini.getSectionItemValue("issue", "type");
-        if("currency".equalsIgnoreCase(type))
+        if(CURRENCY_WORD.equalsIgnoreCase(type))
             return CURRENCY_TYPE;
-        else if("bond".equalsIgnoreCase(type))
+        else if(BOND_WORD.equalsIgnoreCase(type))
             return BOND_TYPE;
-        else if("share".equalsIgnoreCase(type))
+        else if(BOND_WORD.equalsIgnoreCase(type))
             return SHARE_TYPE;
-        else if("server".equalsIgnoreCase(type))
+        else if(SERVER_WORD.equalsIgnoreCase(type))
             return SERVER_TYPE;
 
         if (errors.length() > 0)



1.6       +52 -1     java/webfunds/ricardian/Support.java

Index: Support.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Support.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Support.java	2000/09/24 23:12:08	1.5
+++ Support.java	2000/10/04 00:38:59	1.6
@@ -1,5 +1,5 @@
 /*
- * $Id: Support.java,v 1.5 2000/09/24 23:12:08 iang Exp $
+ * $Id: Support.java,v 1.6 2000/10/04 00:38:59 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -21,6 +21,10 @@
 public class Support
 {
 
+
+
+/////////  File Manipulation  ////////////////////////////
+
     /**
      * Read the url into a byte array
      * @return an array of the contents found at url
@@ -69,6 +73,7 @@
     }
 
 
+
 /////////  Units of Account and Contract  ////////////////////////////
 
     /**
@@ -224,8 +229,46 @@
         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];
+    }
+
+
+
+
     public static void main(String[] args)
         throws ContractException
     {
@@ -247,5 +290,13 @@
             if (p != test)
                 throw new RuntimeException("test == " + test + "   p == " + p);
         }
+
+        int j = 1;
+        while (true)
+        {
+            System.err.println(j + ": " + getWords(j));
+            j--;
+        }
+        // will Panic
     }
 }