Examining Enerjy's Java Toolkit

by Ryan Barr



Listing One

protected List readRecordsFromFile(String fileName) throws IOException {

   FileInputStream fis = new FileInputStream(fileName);

   List records = new ArrayList();      

   ...

   int ch = 0;

   int colNum = 0;

   while (-1 != (ch = fis.read())) {

      if (ch == '\t') {

      ...

      }

   }

   return records;

}



Listing Two

    ...        

FolderEnumerator firstItem = new FolderEnumerator(this, new File(directory));

   workQueue.add(firstItem);

   // Keep processing work items until the queue is empty

   while (!workQueue.isEmpty()) {

      WorkQueue.IWorkItem item = workQueue.remove();

      item.doWork();

   }

 ...



Listing Three

    

public IWorkItem remove() {

   if (head == tail) {

      return null;

   }

   IWorkItem item = queue[head];

   //The queue head should be cleaned up at this point.

   head++;

   if (head == queue.length) {

      head = 0;         

   }

   return item;

}



Listing Four



synchronized (from) {

        if (from.amount < amount) {

            return;

        }

        synchronized (to) {

            to.amount += amount;

            from.amount -= amount;

            try {

                //TSA-JAVA0087: Sleep okay here

                Thread.sleep(XFER_OVERHEAD);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }





Listing Five



if (from.name.compareTo(to.name) > 0)  {

   // Swap to ensure same order for locks below

   BankAccount temp = from;

   from = to;

   to = temp;

   amount = -amount; 

}









2



