[Webfunds-commits] java/webfunds/sox/value ValueManager.java

Ian Grigg iang@cypherpunks.ai
Tue, 10 Apr 2001 16:14:21 -0400 (AST)


iang        01/04/10 16:14:21

  Modified:    webfunds/sox/value ValueManager.java
  Log:
  added policy flags for payments - whether to write rubber cheques

Revision  Changes    Path
1.5       +38 -13    java/webfunds/sox/value/ValueManager.java

Index: ValueManager.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/value/ValueManager.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ValueManager.java	2001/04/06 22:52:14	1.4
+++ ValueManager.java	2001/04/10 20:14:20	1.5
@@ -1,4 +1,4 @@
-/* $Id: ValueManager.java,v 1.4 2001/04/06 22:52:14 iang Exp $
+/* $Id: ValueManager.java,v 1.5 2001/04/10 20:14:20 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -60,7 +60,7 @@
  *
  *  (Was the SOXWallet -- attempt to separate and make simple!)
  *
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
  */
 public class ValueManager
     extends Debug
@@ -276,7 +276,7 @@
         Account acct = getAccount(accountid);
 
         ItemId[] items = acct.getItemIds();
-        logmsg("Ac " + acct + " : " + items.length + " items!");
+        logmsg(acct + " " + items.length + " items:");
         for (int i = 0; i < items.length; i++)
             logmsg("      " + items[i]);
         return items;
@@ -363,21 +363,33 @@
 
     private boolean gotEnough(AccountId src,
                               ItemId item,
-                              long amount)
+                              long amount,
+                              int flags)
     {
-        long pend = getValue(src, item, true);
+        if (flags >= PAY_RUBBER_ANY)
+            return true ;
+
         long confirmed = getValue(src, item, false);
+        long pend = getValue(src, item, true);      // generally negative
  
-        logmsg("amount: " + amount + ", pend: " + pend +
-                      ", confirmed: " + confirmed);
+        long val = confirmed;
+        if (flags == PAY_NO_RUBBER)
+            val += pend;
+
+        logmsg("amount: " + amount + ":  val = " + val +
+                      "(" + flags + ": " + confirmed + ", " + pend + ")");
 
-        if ( (pend + confirmed) < amount )
+        if ( val < amount )
             return false;
         else
             return true;
     }
 
 
+    public static final int   PAY_NO_RUBBER             = 0,
+                              PAY_RUBBER_WITH_PENDING   = 1,
+                              PAY_RUBBER_ANY            = 2,
+                              PAY_INSANE                = 3; // use at peril
 
     /**
      *  Make a payment.
@@ -396,24 +408,30 @@
      * @param from the time from which the payment is valid
      * @param till the time at which the payment will expire
      * @param pid a payment identifier, ignored if empty
+     * @param flags whether the payment can be written if funds are low
      */
     public synchronized AbstractPayment makePayment(
                               AccountId src, AccountId tgt,
                               ItemId item, long amount,
                               byte[] desc,
                               long from, long till,
-                              String pid)
+                              String pid,
+                              int flags)
         throws PaymentException, AccountException
     {
 
-        if (amount < 0)
+        boolean sanity = true;
+        if (flags == PAY_INSANE)
+            sanity = false;
+
+        if (sanity && amount < 0)
             throw new IllegalArgumentException("amount " + amount + " < 0 !");
 
         ValueAccount val = getKnownValueAccount(src, item);
 
         // it is an error (?) if the user asks for pid when
         // that one is already in play.
-        if (pid != null && (pid.length() > 0))
+        if (sanity && pid != null && (pid.length() > 0))
         {
             StateReceipt sr;
             try {
@@ -434,7 +452,7 @@
          *  or if the amount is zero...  This is a judgement call of course.)
          */
         if (!src.equals(tgt) && (amount > 0) &&
-            !gotEnough(src, item, amount))
+            !gotEnough(src, item, amount, flags))
         {
             throw new PaymentException(PaymentException.NOT_ENUF_FUNDS,
                                        "amount " + amount + " not available");
@@ -452,6 +470,7 @@
                                        "Payment no good: " + ex);
         }
 
+logmsg("Payment Created::::: " + payment);
         savePaymentAsPending(src, payment);
 
         return payment ;
@@ -562,18 +581,24 @@
      * @param from the time from which the payment is valid
      * @param till the time at which the payment will expire
      * @param type the type of token method from PaymentFactory
+     * @param flags PAY_* values
      */
     public synchronized TokenPayment makeTokenPayment(
                               AccountId src, AccountId tgt,
                               ItemId item, long amount,
                               byte[] desc,
                               long from, long till,
-                              int type)
+                              int type,
+                              int flags)
         throws PaymentException, AccountException
     {
+        boolean sanity = true;
+        if (flags == PAY_INSANE)
+            sanity = false;
+
         ValueAccount val = getKnownValueAccount(src, item);
 
-        if (amount < 0)
+        if (sanity && amount < 0)
             throw new IllegalArgumentException("amount " + amount + " < 0 !");
 
         /*
@@ -582,7 +607,7 @@
          *  or if the amount is zero...  This is a judgement call of course.)
          */
         if ((amount > 0) &&
-            !gotEnough(src, item, amount))
+            !gotEnough(src, item, amount, flags))
         {
             throw new PaymentException(PaymentException.NOT_ENUF_FUNDS,
                                        "amount " + amount + " not available");