Commons Lang
  1. Commons Lang
  2. LANG-577

Add ObjectReference interface and two implementations

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Review Patch
    • Component/s: lang.*
    • Labels:
      None

      Description

      In some situations it would be helpful to use a reference to an object, e.g. for parameters by reference

      void doSomething(ObjectReference<String> ref) {
          ref.set("Hello");
      }
      

      or for anonymous methods

      final ObjectReference<String> ref = new MemoryReference<String>();
      final Runnable r = new Runnable() {
          void run() {
              ref.set("Hello");
          }
      }
      r.run();
      

      Additionally it is sometimes useful to keep the reference in other places than in shared memory, e.g. in a ThreadLocal or in case of a web application in a scoped reference or even in combination with some other persistence mechanism. Basically I am proposing the interface ObjectReference:

      /**
       * Interface to reference an object.
       * 
       * @param <T> the type of the referenced object
       * @author Apache Software Foundation
       * @since 3.0
       */
      public interface ObjectReference<T> {
          /**
           * Getter for the referenced object.
           * 
           * @return the object or <code>null</code>
           */
          T get();
      
          /**
           * Setter for the reference.
           * 
           * @param object the object to reference (may be <code>null</code>)
           */
          void set(T object);
      }
      

      and the two implementations MemoryReference and ThreadLocalReference in the new package org.apache.commons.lang3.reference. I've seen such or similar types in various libraries.

      Comments?

      Unit test will be provided also.

      1. reference.diff
        8 kB
        Joerg Schaible

        Activity

        Joerg Schaible created issue -
        Joerg Schaible made changes -
        Field Original Value New Value
        Attachment reference.diff [ 12428864 ]
        Henri Yandell made changes -
        Fix Version/s 3.1 [ 12313371 ]
        Fix Version/s 3.0 [ 12311714 ]
        Mark Thomas made changes -
        Workflow jira [ 12485485 ] Default workflow, editable Closed status [ 12602230 ]
        Henri Yandell made changes -
        Fix Version/s Review Patch [ 12325281 ]
        Fix Version/s 3.x [ 12313371 ]

          People

          • Assignee:
            Joerg Schaible
            Reporter:
            Joerg Schaible
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development