/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.solr.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yonik
 * @version $Id$
 */

public abstract class RefCounted<Type> {
  protected final Type resource;
  protected final AtomicInteger refcount= new AtomicInteger();
  public RefCounted(Type resource) { this.resource = resource; }
  public final RefCounted<Type> incref() { refcount.incrementAndGet(); return this; }
  public final Type get() { return resource; }
  public void decref() { if (refcount.decrementAndGet()==0) close(); }
  protected abstract void close();
}

@@ -882,16 +882,415 @@
 er;%0A%0A/**
+ Keep track of a reference count on a resource and close it when%0A * the count hits zero.%0A *%0A * By itself, this class could have some race conditions%0A * since there is no synchronization between the refcount%0A * check and the close.  Solr's use in reference counting searchers%0A * is safe since the count can only hit zero if it's unregistered (and%0A * hence incref() will not be called again on it).%0A *
 %0A * @aut
@@ -1435,16 +1435,17 @@
 refcount
+ 
 = new At
@@ -1459,16 +1459,17 @@
 eger();%0A
+%0A
   public
@@ -1496,16 +1496,20 @@
 ource) %7B
+%0A   
  this.re
@@ -1518,35 +1518,38 @@
 urce = resource;
+%0A 
  %7D%0A
+%0A
   public final R
@@ -1574,16 +1574,20 @@
 cref() %7B
+%0A   
  refcoun
@@ -1606,16 +1606,20 @@
 ndGet();
+%0A   
  return 
@@ -1623,19 +1623,22 @@
 rn this;
+%0A 
  %7D%0A
+%0A
   public
@@ -1656,16 +1656,20 @@
  get() %7B
+%0A   
  return 
@@ -1677,19 +1677,22 @@
 esource;
+%0A 
  %7D%0A
+%0A
   public
@@ -1707,16 +1707,20 @@
 cref() %7B
+%0A   
  if (ref
@@ -1746,23 +1746,42 @@
 et()
+ 
 ==
+ 
 0) 
-close(); %7D
+%7B%0A      close();%0A    %7D%0A  %7D%0A
 %0A  p

add usage comments, reformat