⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actorpool.java

📁 J2ME Game Programming的隋书源代码
💻 JAVA
字号:
/**
 * A pool of objects for use in situations where you want to minimize object
 * lifecycling (and subsequently garbage collection). It also serves as a very
 * high speed, minimal overhead collection for small numbers of objects.
 * <p>
 * Keep in mind that Actors that are pooled are not constructed; they are
 * "reset" when handed out. You need to "grab" one and then call init to setup
 * the actor for its new use.
 * <p>
 * This class maintains two lists in order to differientiate between actors
 * in use and those that are unallocated from the pool. This allows for much
 * faster cycling of only the in-use actors.
 */

public class ActorPool
{
   private Actor firstFree;		// ref to the head of the free actor list
   private Actor lastFree;
   private Actor firstUsed;		// ref to the head of the used list (oldest)
   private Actor oldestUsed;
   private Actor lastUsed;

   /**
    * Constructs an actor pool using a base of objects passed in as an array.
    * @param initObjects The starting objects for the pool.
    */
   public ActorPool(Actor[] initObjects)
   {
      if (initObjects == null || initObjects.length < 2)
         return;

      // init the linked list of free objects
      firstFree = initObjects[0];
      lastFree = initObjects[initObjects.length - 1];

      for (int i = 0; i < initObjects.length; i++)
      {
         if (i < initObjects.length - 1)
            initObjects[i].setNextLinked(initObjects[i + 1]);
         else
            initObjects[i].setNextLinked(null);

         if (i > 0)
            initObjects[i].setPrevLinked(initObjects[i - 1]);
         else
            initObjects[i].setPrevLinked(null);
      }
   }

   /**
    * Returns the next free object by moving it from the free pool to the used
    * one. If no free objects are available it returns the oldest from the used
    * pool.
    */
   public Actor getNextFree()
   {
      // give out the next free one
      if (firstFree != null)
      {
         // return the next actor off the start of the free list
         if (firstFree.getNextLinked() != null)
            firstFree.getNextLinked().setPrevLinked(null);

         Actor newFirstFree = firstFree.getNextLinked();

         // now link this actor onto the end of the used list
         firstFree.setNextLinked(null);
         if (lastUsed != null)
         {
            firstFree.setPrevLinked(lastUsed);
            lastUsed.setNextLinked(firstFree);
         }
         lastUsed = firstFree;

         if (oldestUsed == null)
            oldestUsed = firstFree;
         if (firstUsed == null)
            firstUsed = firstFree;

         // if we're giving out the last one then clear lastFree
         if (lastFree == firstFree)
            lastFree = null;

         Actor actorToReturn = firstFree;
         firstFree = newFirstFree;

         return actorToReturn;
      }

      // none free, hand out the oldest from the used list and make it's
      // next the oldestUsed (so we don't just keep reusing this one)
      Actor actorToReturn = oldestUsed;
      if (oldestUsed == null)
      {
         // we've been through the whole list, restart from the top
         oldestUsed = firstUsed;
         actorToReturn = oldestUsed;
      }
      // since we've handed this guy out, make the next one the oldest
      // (if that's null we'll catch it next time around anyway).
      oldestUsed = oldestUsed.getNextLinked();
      return actorToReturn;
   }

   /**
    * Outputs the contents of the current lists. Usually for debugging.
    */
   public void dump()
   {
      System.out.print("FREE LIST: ");
      Actor a = firstFree;
      while (a != null)
      {
         System.out.print("{" + a + "} ");
         a = a.getNextLinked();
      }
      System.out.println("");

      System.out.print("USED LIST: ");
      a = firstUsed;
      while (a != null)
      {
         System.out.print("{" + a + "} ");
         a = a.getNextLinked();
      }
      System.out.println("");
      System.out.println("LastUsed: {" + lastUsed + "} OldestUsed: {" + oldestUsed +
                         "} firstUsed: {" + firstUsed +
                         "} LastFree: {" + lastFree + "} firstFree: {" + firstFree + "}");

   }

   /**
    * Releases an actor by moving it from the used list back to the free list.
    * @param actorToRelease
    */
   public void release(Actor actorToRelease)
   {
      // add this actor back onto the end of the free list

      if (actorToRelease.getPrevLinked() != null)
      // join the break in the used list
         actorToRelease.getPrevLinked().setNextLinked(actorToRelease.getNextLinked());
      if (actorToRelease.getNextLinked() != null)
         actorToRelease.getNextLinked().setPrevLinked(actorToRelease.getPrevLinked());

      // if this actor was the oldestUsed then make it's next take that place
      if (oldestUsed == actorToRelease)
         oldestUsed = actorToRelease.getNextLinked();
      if (firstUsed == actorToRelease)
         firstUsed = actorToRelease.getNextLinked();
      if (lastUsed == actorToRelease)
         lastUsed = actorToRelease.getPrevLinked();

      // now move this actor onto the end of the free list
      actorToRelease.setNextLinked(null);
      if (firstFree == null)
      {
         firstFree = actorToRelease;
         actorToRelease.setPrevLinked(null);
      }

      if (lastFree != null)
      {
         actorToRelease.setPrevLinked(lastFree);
         lastFree.setNextLinked(actorToRelease);
      }
      else
         actorToRelease.setPrevLinked(null);

      lastFree = actorToRelease;
   }

   public Actor getFirstUsed()
   {
      return firstUsed;
   }


}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -