View Javadoc
1   /*
2    * #%L
3    * NamedReference.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.client;
21  
22  import java.lang.ref.ReferenceQueue;
23  import java.lang.ref.WeakReference;
24  
25  /**
26   * NamedReference provides a Reference that tracks a name. We use a weak
27   * reference as the base class as we want the garbage collector to reclaim the
28   * instance as soon as possible.
29   * 
30   * @param <T>
31   *            The type of the referent.
32   * 
33   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
34   *         mutated in incompatible ways between any two releases of the driver.
35   * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
36   */
37  /* package */class NamedReference<T> extends WeakReference<T> {
38  
39      /** The name for the referent. */
40      private final String myName;
41  
42      /**
43       * Creates a new NamedReference.
44       * 
45       * @param name
46       *            The name for the referent.
47       * @param referent
48       *            The named referent.
49       * @param q
50       *            The queue to add the reference to when reclaimed.
51       */
52      protected NamedReference(final String name, final T referent,
53              final ReferenceQueue<? super T> q) {
54          super(referent, q);
55          myName = name;
56      }
57  
58      /**
59       * Returns the name for the referent.
60       * 
61       * @return The name for the referent.
62       */
63      public String getName() {
64          return myName;
65      }
66  }