File Source: WeakList.java

         /* 
    P/P   *  Method: com.dmdirc.util.WeakList__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.util;
    24  
    25  import java.lang.ref.WeakReference;
    26  import java.util.ArrayList;
    27  import java.util.Collection;
    28  import java.util.Iterator;
    29  import java.util.List;
    30  import java.util.ListIterator;
    31  
    32  /**
    33   * Implements a list of weak references. The weak references (and subsequent
    34   * garbage collection) are handled transparently.
    35   * 
    36   * @param <T> The type of object that this list will contain.
    37   * @author chris
    38   */
    39  public class WeakList<T> implements List<T> {
    40  
    41      /** The items in this list. */
    42      private final List<WeakReference<T>> list = new ArrayList<WeakReference<T>>();
    43  
    44      /**
    45       * Creates a new instance of WeakList.
    46       */
    47      public WeakList() {
                 /* 
    P/P           *  Method: void com.dmdirc.util.WeakList()
                  * 
                  *  Postconditions:
                  *    this.list == &amp;new ArrayList(WeakList#1)
                  *    new ArrayList(WeakList#1) num objects == 1
                  */
    48          super();
    49      }
    50  
    51      /**
    52       * Removes any entries from the list that have been GC'd.
    53       */
    54      private void cleanUp() {
                 /* 
    P/P           *  Method: void cleanUp()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@56 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.ref.WeakReference:get(...)@56: Inverse{null}, Addr_Set{null}
                  */
    55          for (int i = 0; i < list.size(); i++) {
    56              if (list.get(i).get() == null) {
    57                  list.remove(i--);
    58              }
    59          }
    60      }
    61  
    62      /**
    63       * Dereferences the specified list of WeakReferences to get a plain List.
    64       * 
    65       * @param list The list to be dereferenced
    66       * @return A list containing the items referenced by the specified list
    67       */
    68      private List<T> dereferenceList(final List<WeakReference<T>> list) {
                 /* 
    P/P           *  Method: List dereferenceList(List)
                  * 
                  *  Preconditions:
                  *    list != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@71 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(dereferenceList#1)
                  *    new ArrayList(dereferenceList#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.ref.WeakReference:get(...)@72: Addr_Set{null}, Inverse{null}
                  *    java.util.Iterator:hasNext(...)@71: {0}, {1}
                  */
    69          final List<T> res = new ArrayList<T>();
    70  
    71          for (WeakReference<T> item : list) {
    72              if (item.get() != null) {
    73                  res.add(item.get());
    74              }
    75          }
    76  
    77          return res;
    78      }
    79  
    80      /**
    81       * Creates a new collection of weak references to elements in the specified
    82       * collection.
    83       * 
    84       * @param c The collection whose elements should be referenced
    85       * @return A copy of the specified collection, with each item wrapped in
    86       * a weak reference.
    87       */
    88      @SuppressWarnings(value = "unchecked")
    89      private Collection<WeakReference<T>> referenceCollection(final Collection<?> c) {
                 /* 
    P/P           *  Method: Collection referenceCollection(Collection)
                  * 
                  *  Preconditions:
                  *    c != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(referenceCollection#1)
                  *    new ArrayList(referenceCollection#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@92: {0}, {1}
                  */
    90          final Collection<WeakReference<T>> res = new ArrayList<WeakReference<T>>();
    91  
    92          for (Object item : c) {
    93              res.add(new EquatableWeakReference(item));
    94          }
    95  
    96          return res;
    97      }
    98  
    99      /** {@inheritDoc} */
   100      @Override
   101      public int size() {
                 /* 
    P/P           *  Method: int size()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   102          cleanUp();
   103  
   104          return list.size();
   105      }
   106  
   107      /** {@inheritDoc} */
   108      @Override
   109      public boolean isEmpty() {
                 /* 
    P/P           *  Method: bool isEmpty()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   110          cleanUp();
   111  
   112          return list.isEmpty();
   113      }
   114  
   115      /** {@inheritDoc} */
   116      @Override @SuppressWarnings("unchecked")
   117      public boolean contains(final Object o) {
                 /* 
    P/P           *  Method: bool contains(Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   118          return list.contains(new EquatableWeakReference(o));
   119      }
   120  
   121      /** {@inheritDoc} */
   122      @Override
   123      public Iterator<T> iterator() {
                 /* 
    P/P           *  Method: Iterator iterator()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   124          return dereferenceList(list).iterator();
   125      }
   126  
   127      /** {@inheritDoc} */
   128      @Override
   129      public Object[] toArray() {
                 /* 
    P/P           *  Method: Object[] toArray()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   130          return dereferenceList(list).toArray();
   131      }
   132  
   133      /** {@inheritDoc} */
   134      @Override
   135      public <T> T[] toArray(final T[] a) {
                 /* 
    P/P           *  Method: Object[] toArray(Object[])
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   136          return dereferenceList(list).toArray(a);
   137      }
   138  
   139      /** {@inheritDoc} */
   140      @Override
   141      public boolean add(final T e) {
                 /* 
    P/P           *  Method: bool add(Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   142          return list.add(new EquatableWeakReference<T>(e));
   143      }
   144  
   145      /** {@inheritDoc} */
   146      @Override @SuppressWarnings(value = "unchecked")
   147      public boolean remove(final Object o) {
                 /* 
    P/P           *  Method: bool remove(Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   148          return list.remove(new EquatableWeakReference(o));
   149      }
   150  
   151      /** {@inheritDoc} */
   152      @Override
   153      public boolean containsAll(final Collection<?> c) {
                 /* 
    P/P           *  Method: bool containsAll(Collection)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   154          return dereferenceList(list).containsAll(c);
   155      }
   156  
   157      /** {@inheritDoc} */
   158      @Override
   159      public boolean addAll(final Collection<? extends T> c) {
                 /* 
    P/P           *  Method: bool addAll(Collection)
                  * 
                  *  Preconditions:
                  *    c != null
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   160          return list.addAll(referenceCollection(c));
   161      }
   162  
   163      /** {@inheritDoc} */
   164      @Override
   165      public boolean addAll(final int index, final Collection<? extends T> c) {
                 /* 
    P/P           *  Method: bool addAll(int, Collection)
                  * 
                  *  Preconditions:
                  *    c != null
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   166          return list.addAll(index, referenceCollection(c));
   167      }
   168  
   169      /** {@inheritDoc} */
   170      @Override
   171      public boolean removeAll(final Collection<?> c) {
                 /* 
    P/P           *  Method: bool removeAll(Collection)
                  * 
                  *  Preconditions:
                  *    c != null
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   172          return list.removeAll(referenceCollection(c));
   173      }
   174  
   175      /** {@inheritDoc} */
   176      @Override
   177      public boolean retainAll(final Collection<?> c) {
                 /* 
    P/P           *  Method: bool retainAll(Collection)
                  * 
                  *  Preconditions:
                  *    c != null
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   178          return list.retainAll(referenceCollection(c));
   179      }
   180  
   181      /** {@inheritDoc} */
   182      @Override
   183      public void clear() {
                 /* 
    P/P           *  Method: void clear()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  */
   184          list.clear();
   185      }
   186  
   187      /** {@inheritDoc} */
   188      @Override
   189      public T get(final int index) {
                 /* 
    P/P           *  Method: Object get(int)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@192 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   190          cleanUp();
   191  
   192          return list.get(index).get();
   193      }
   194  
   195      /** {@inheritDoc} */
   196      @Override
   197      public T set(final int index, final T element) {
                 /* 
    P/P           *  Method: Object set(int, Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    return_value == element
                  *    init'ed(return_value)
                  */
   198          list.set(index, new EquatableWeakReference<T>(element));
   199  
   200          return element;
   201      }
   202  
   203      /** {@inheritDoc} */
   204      @Override
   205      public void add(final int index, final T element) {
                 /* 
    P/P           *  Method: void add(int, Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  */
   206          list.add(index, new EquatableWeakReference<T>(element));
   207      }
   208  
   209      /** {@inheritDoc} */
   210      @Override
   211      public T remove(final int index) {
                 /* 
    P/P           *  Method: Object remove(int)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Presumptions:
                  *    java.util.List:remove(...)@212 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   212          return list.remove(index).get();
   213      }
   214  
   215      /** {@inheritDoc} */
   216      @Override @SuppressWarnings(value = "unchecked")
   217      public int indexOf(final Object o) {
                 /* 
    P/P           *  Method: int indexOf(Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   218          cleanUp();
   219  
   220          return list.indexOf(o);
   221      }
   222  
   223      /** {@inheritDoc} */
   224      @Override @SuppressWarnings(value = "unchecked")
   225      public int lastIndexOf(final Object o) {
                 /* 
    P/P           *  Method: int lastIndexOf(Object)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   226          cleanUp();
   227  
   228          return list.lastIndexOf(o);
   229      }
   230  
   231      /** {@inheritDoc} */
   232      @Override
   233      public ListIterator<T> listIterator() {
                 /* 
    P/P           *  Method: ListIterator listIterator()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   234          cleanUp();
   235  
   236          return dereferenceList(list).listIterator();
   237      }
   238  
   239      /** {@inheritDoc} */
   240      @Override
   241      public ListIterator<T> listIterator(final int index) {
                 /* 
    P/P           *  Method: ListIterator listIterator(int)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   242          cleanUp();
   243  
   244          return dereferenceList(list).listIterator(index);
   245      }
   246  
   247      /** {@inheritDoc} */
   248      @Override
   249      public List<T> subList(final int fromIndex, final int toIndex) {
                 /* 
    P/P           *  Method: List subList(int, int)
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Presumptions:
                  *    java.util.List:subList(...)@250 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(dereferenceList#1)
                  *    new ArrayList(dereferenceList#1) num objects == 1
                  */
   250          return dereferenceList(list.subList(fromIndex, toIndex));
   251      }
   252  }








SofCheck Inspector Build Version : 2.17854
WeakList.java 2009-Jun-25 01:54:24
WeakList.class 2009-Sep-02 17:04:17