Documentation of 'javolution37.javolution.lang.Reusable' Java class
Reusable
javolution37.javolution.lang

Interface Reusable

  • All Known Implementing Classes:
    FastList, FastMap, FastSet, FastTable, ObjectReader, ObjectWriter, TextBuilder, Utf8ByteBufferReader, Utf8ByteBufferWriter, Utf8StreamReader, Utf8StreamWriter, WriterHandler, XmlInputStream, XmlOutputStream, XmlPullParserImpl, XmlSaxParserImpl


    public interface Reusable

    This interfaces identifies mutable objects capable of being used again or repeatedly; once reset, reusable objects behave as if they were brand-new.

    Reusable instances should not allocate new internal objects after creation except for the purpose of increasing their internal capacities. In such case the new allocations have to be performed in the same memory areas as the reusable objects themselves (necessary to avoid memory leaks or memory clashes when running on RTSJ VMs). For example:

         import javax.realtime.MemoryArea;
         public class Targets implements Reusable {
             private Target[] _targets = new Target[32];
             private int _count;
             private void capacityOverflow() {
                  MemoryArea.getMemoryArea(this).executeInArea(new Runnable() {
                      public void run() {
                           Target[] tmp = new Target[_targets.length * 2];
                           System.arraycopy(_targets, 0, tmp, 0, _count);
                           _targets = tmp;
                      }
                  });
             }
             ...
         }

    Instances of this class can safely reside in permanent memory (e.g. static members) or be an integral part of a higher level component. For example:

         public class XmlFormat {
              // RTSJ Unsafe! Memory leaks (when entries removed) or IllegalAssignmentError (when new entries while in ScopedArea).   
              static HashMap<Class, XmlFormat> ClassToFormat = HashMap<Class, XmlFormat>();
                 
              // RTSJ safe! FastMap is Reusable. Removed entries are internally recycled, new entries are in ImmortalMemory.
              static FastMap<Class, XmlFormat> ClassToFormat = FastMap<Class, XmlFormat>();
         }

    Reusable objects can also be allocated on the stack providing that their factory cleanup method calls the reset method. For example:

         public class Foo extends RealtimeObject implements Reusable {
             private static final Factory<Foo> FACTORY = new Factory<Foo>() {
                 public Foo create() {
                     return new Foo();
                 }
                 public void cleanup(Foo obj) {
                     obj.reset();
                 }
             };
             public static Foo newInstance() {
                 return FACTORY.object();
             } 
             ...
         }

    • Method Detail

      • reset

        void reset()
        Resets the internal state of this object to its default values.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.