View Javadoc

1   package org.freehep.graphicsio.emf;
2   
3   import java.util.BitSet;
4   
5   /**
6    * Allocates and frees handles for EMF files
7    * 
8    * @author Tony Johnson
9    * @version $Id: EMFHandleManager.java 8584 2006-08-10 23:06:37Z duns $
10   */
11  public class EMFHandleManager {
12      private BitSet handles = new BitSet();
13  
14      private int maxHandle;
15  
16      public int getHandle() {
17          int handle = nextClearBit();
18          handles.set(handle);
19          if (handle > maxHandle)
20              maxHandle = handle;
21          return handle;
22      }
23  
24      public int freeHandle(int handle) {
25          handles.clear(handle);
26          return handle;
27      }
28  
29      private int nextClearBit() {
30          // return handles.nextClearBit(1); // JDK 1.4
31          for (int i = 1;; i++)
32              if (!handles.get(i))
33                  return i;
34      }
35  
36      public int highestHandleInUse() {
37          return handles.length() - 1;
38      }
39  
40      public int maxHandlesUsed() {
41          return maxHandle + 1;
42      }
43  }