public synchronized short allocateId() throws IdAllocatorException
   {
      if (tc.isEntryEnabled()) SibTr.entry(tc, "allocateId");

      short nextAllocatedId = NULL_ID;
      
      // First check the last freed id
      if (lastFreeId != NULL_ID)
      {
         nextAllocatedId = lastFreeId;
         lastFreeId = NULL_ID;
      }
      // Is the next Id not null?
      else if (nextId != NULL_ID)
      {
         nextAllocatedId = nextId;
         if (nextId == maxId)
         {
            nextId = NULL_ID;
         }
         else
         {
            nextId++;
         }
      }
      // Otherwise check all the possible ids for a free one
      else
      {
         for (short x = 0; x < allocMap.length; x++)
         {
            byte oneByte = allocMap[x];
            
            // Get the first byte. If all the bits are set, then don't worry about it
            if (oneByte != (byte) 0xFF)
            {
               // Otherwise check each one to find out what id is free
               if ((oneByte & 0x80) == 0)
               {
                  // Bit 1 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 1);
              }
               else if ((oneByte & 0x40) == 0)
               {
                  // Bit 2 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 2);
               }
               else if ((oneByte & 0x20) == 0)
               {
                  // Bit 3 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 3);
               }
               else if ((oneByte & 0x10) == 0)
               {
                  // Bit 4 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 4);
               }
               else if ((oneByte & 0x8) == 0)
               {
                  // Bit 5 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 5);
               }
               else if ((oneByte & 0x4) == 0)
               {
                  // Bit 6 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 6);
               }
               else if ((oneByte & 0x2) == 0)
               {
                  // Bit 7 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 7);
               }
               else if ((oneByte & 0x1) == 0)
               {
                  // Bit 8 is not set
                  nextAllocatedId = (short) (((x + 1) * 8) - 8);
                  
                  // Are we allowed zero?
                  if (nextAllocatedId == 0 && !allowZero)
                  {
                     nextAllocatedId = NULL_ID;
                  }
               }
            }

            else if (nextAllocatedId != NULL_ID) 
            {
               break;
            }
         }
      }
      
      // If we found nothing, then we have no choice but to throw an exception
      if (nextAllocatedId == NULL_ID)
      {
         throw new IdAllocatorException();
      }
      else
      {
         allocate(nextAllocatedId);
      }

      if (tc.isEntryEnabled()) SibTr.exit(tc, "allocateId", ""+nextId);
      return nextAllocatedId;
   }
public synchronized short allocateId ( ) throws IdAllocatorException { if ( tc . isEntryEnabled ( ) ) SibTr . entry ( tc , "allocateId" ) ; short nextAllocatedId = NULL_ID ; if ( lastFreeId != NULL_ID ) { nextAllocatedId = lastFreeId ; lastFreeId = NULL_ID ; } else if ( nextId != NULL_ID ) { nextAllocatedId = nextId ; if ( nextId == maxId ) { nextId = NULL_ID ; } else { nextId ++ ; } } else { for ( short x = 0 ; x < allocMap . length ; x ++ ) { byte oneByte = allocMap [ x ] ; if ( oneByte != ( byte ) 0xFF ) { if ( ( oneByte & 0x80 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 1 ) ; } else if ( ( oneByte & 0x40 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 2 ) ; } else if ( ( oneByte & 0x20 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 3 ) ; } else if ( ( oneByte & 0x10 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 4 ) ; } else if ( ( oneByte & 0x8 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 5 ) ; } else if ( ( oneByte & 0x4 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 6 ) ; } else if ( ( oneByte & 0x2 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 7 ) ; } else if ( ( oneByte & 0x1 ) == 0 ) { nextAllocatedId = ( short ) ( ( ( x + 1 ) * 8 ) - 8 ) ; if ( nextAllocatedId == 0 && ! allowZero ) { nextAllocatedId = NULL_ID ; } } } else if ( nextAllocatedId != NULL_ID ) { break ; } } } if ( nextAllocatedId == NULL_ID ) { throw new IdAllocatorException ( ) ; } else { allocate ( nextAllocatedId ) ; } if ( tc . isEntryEnabled ( ) ) SibTr . exit ( tc , "allocateId" , "" + nextId ) ; return nextAllocatedId ; }
Allocates an ID.
<p>
At this point we need to try and allocate an id as efficiently as possible. So, first
check the last freed id counter. If that was not null, allocate that one and set the last
freed id to null.
<p>
If the next Id is not null then allocate the next id. If that id was 32,767 then set the
next id to null.
<p>
If the next Id is null, then we must do a scan of the allocated ids to determine if there
are any free ones available. If we find one available then we should allocate it.
<p>
If we cannot find any available then there is nothing we can do and we should throw an
exception.

@return The ID allocated.

@throws IdAllocatorException Thrown if there are currently no free IDs.