webfunds.ricardian
Class DirContractStore

java.lang.Object
  |
  +--webfunds.utils.Debug
        |
        +--webfunds.ricardian.DirContractStore
Direct Known Subclasses:
ChangeContractStore

public class DirContractStore
extends Debug
implements ContractStore

Read in a directory of contracts and provide access to them. Derived from client.DirContractStore and InfoBag.pm.


Field Summary
protected  java.util.Hashtable allNames
          All the names, pointing to ItemIds of contracts that answer to the name.
protected  java.util.Hashtable contracts
          All the contracts, indexed by ItemId.
protected  java.io.File dir
           
 
Fields inherited from class webfunds.utils.Debug
bug, debugAll, logfix
 
Constructor Summary
DirContractStore()
          Read in a directory of contracts and provide access to them.
DirContractStore(java.io.File storedir)
          Read in a directory of contracts and provide access to them.
DirContractStore(java.io.File storedir, java.io.PrintWriter bug)
          Read in a directory of contracts and provide access to them.
 
Method Summary
 void addContract(Contract contract)
          Add a new contract to the store.
 void addDirectory(java.io.File readDir)
          Read in a user directory of contracts.
protected  void addThisName(java.lang.String name, ItemId id)
          Add the hash as a contract that answers to name.
protected  void dropNames(Contract con)
           
protected  void dropThisName(java.lang.String name, ItemId id)
           
 Contract[] getAllContracts()
          Unique list - contracts appear once and only once.
 ItemId[] getAllIds()
          One to one with getAllContracts().
 java.lang.String[] getAllNames()
          There are many names for each contract, this list will be larger than the number of contracts.
 Contract getContract(ItemId hash)
          Return a contract named by hash.
 Contract[] getContractsByName(java.lang.String name)
          Return a list of contracts answering to the given name.
 java.io.File getDirectory()
          Get the directory of contracts.
 ItemId[] getIdsByName(java.lang.String name)
           
 java.lang.String[] getShortNamesWithClashes()
          Unique list of names - names appear once and only once, but each name might represent more than one contract.
 java.lang.String getType()
          Unknown?
 Contract getUniqueContract(java.lang.String name)
          Always returns a contract?
static void main(java.lang.String[] arg)
           
protected  void readDirectory(java.io.File readDir, java.io.File storeDir)
          Read in a directory of contracts and store them in a different place.
protected  void readNamesForContract(Contract contract)
          Index the various names by which the contract is known.
 void removeContract(Contract remcontract)
          Drop a contract from the store.
protected  void setDirectory(java.io.File storedir)
          Read in a directory of contracts.
 java.lang.String toString()
           
 
Methods inherited from class webfunds.utils.Debug
debug, debug, debug, debug, err, getDebug, logend, logmsg, logstart, logword
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

contracts

protected java.util.Hashtable contracts
All the contracts, indexed by ItemId. ItemId ==> Contract

allNames

protected java.util.Hashtable allNames
All the names, pointing to ItemIds of contracts that answer to the name. String ==> ItemId[]

dir

protected java.io.File dir
Constructor Detail

DirContractStore

public DirContractStore()
Read in a directory of contracts and provide access to them. The default of the current directory is used.

DirContractStore

public DirContractStore(java.io.File storedir)
                 throws ContractDirectoryException
Read in a directory of contracts and provide access to them.
Parameters:
storedir - a directory with contracts

DirContractStore

public DirContractStore(java.io.File storedir,
                        java.io.PrintWriter bug)
                 throws ContractDirectoryException
Read in a directory of contracts and provide access to them.
Parameters:
storedir - a directory with contracts
bug - place to put debugging during construction
Method Detail

getDirectory

public java.io.File getDirectory()
Get the directory of contracts.

getType

public java.lang.String getType()
Unknown?

readDirectory

protected void readDirectory(java.io.File readDir,
                             java.io.File storeDir)
                      throws ContractDirectoryException
Read in a directory of contracts and store them in a different place. If there is no directory, return silently. Should throw a Contract exception if this bit fails, not Runtime.

addDirectory

public void addDirectory(java.io.File readDir)
                  throws ContractDirectoryException
Read in a user directory of contracts. Is silent if the directory is not there.

setDirectory

protected void setDirectory(java.io.File storedir)
                     throws ContractDirectoryException
Read in a directory of contracts. Make the directory if needed. It is not an error if files cannot be rewritten.

readNamesForContract

protected void readNamesForContract(Contract contract)
Index the various names by which the contract is known. Needs to be idempotent.

addContract

public void addContract(Contract contract)
                 throws ContractException
Add a new contract to the store. This is the only way to get new contracts into the store. After adding, the contract should be re-got.
Specified by:
addContract in interface ContractStore

removeContract

public void removeContract(Contract remcontract)
Drop a contract from the store.
Specified by:
removeContract in interface ContractStore
Parameters:
contract - is the object that should be dropped.

addThisName

protected void addThisName(java.lang.String name,
                           ItemId id)
Add the hash as a contract that answers to name.

dropNames

protected void dropNames(Contract con)

dropThisName

protected void dropThisName(java.lang.String name,
                            ItemId id)

getAllContracts

public Contract[] getAllContracts()
Unique list - contracts appear once and only once.
Specified by:
getAllContracts in interface ContractStore
Returns:
a list of all contracts currently held in the store.

getShortNamesWithClashes

public java.lang.String[] getShortNamesWithClashes()
Unique list of names - names appear once and only once, but each name might represent more than one contract.
Returns:
a list of all short names.

getContract

public Contract getContract(ItemId hash)
Description copied from interface: ContractStore
Return a contract named by hash. Hash is unique.
Specified by:
getContract in interface ContractStore
Returns:
the contract that uniquely matches hash, else null

getUniqueContract

public Contract getUniqueContract(java.lang.String name)
                           throws ContractException
Always returns a contract?
Specified by:
getUniqueContract in interface ContractStore
Returns:
the contract that uniquely matches name

getContractsByName

public Contract[] getContractsByName(java.lang.String name)
Description copied from interface: ContractStore
Return a list of contracts answering to the given name. As a contract can answer to any name, and as the software can assign new names, name clashes can and do occur. It is up to the caller to decide what to do about it.
Specified by:
getContractsByName in interface ContractStore
Returns:
a list of contracts that answer to name

getIdsByName

public ItemId[] getIdsByName(java.lang.String name)
Specified by:
getIdsByName in interface ContractStore
Returns:
a list of ItemIds that answer to name

getAllIds

public ItemId[] getAllIds()
Description copied from interface: ContractStore
One to one with getAllContracts().
Specified by:
getAllIds in interface ContractStore
Returns:
a list of the ids of the contracts in the store.

getAllNames

public java.lang.String[] getAllNames()
Description copied from interface: ContractStore
There are many names for each contract, this list will be larger than the number of contracts.
Specified by:
getAllNames in interface ContractStore
Returns:
a list of the names that contracts answer by

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] arg)
                 throws ContractException,
                        ContractDirectoryException