001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util; 018 019import java.util.concurrent.atomic.AtomicLong; 020 021public final class ReferenceCount { 022 private final AtomicLong count; 023 private final Runnable onFirst; 024 private final Runnable onRelease; 025 026 private ReferenceCount(Runnable onFirst, Runnable onRelease) { 027 this.count = new AtomicLong(0); 028 this.onFirst = ObjectHelper.notNull(onFirst, "onFirst"); 029 this.onRelease = ObjectHelper.notNull(onRelease, "onRelease"); 030 } 031 032 /** 033 * Returns the reference count. 034 */ 035 public long get() { 036 return count.get(); 037 } 038 039 /** 040 * Increases the reference count invoke onFirst on the first increment; 041 */ 042 public void retain() throws IllegalStateException { 043 while (true) { 044 long v = count.get(); 045 if (v < 0) { 046 throw new IllegalStateException("Released"); 047 } 048 049 if (count.compareAndSet(v, v + 1)) { 050 if (v == 0) { 051 this.onFirst.run(); 052 } 053 054 break; 055 } 056 } 057 } 058 059 /** 060 * Decreases the reference count and invoke onRelease if the reference count reaches {@code 0}. 061 */ 062 public void release() throws IllegalStateException { 063 while (true) { 064 long v = count.get(); 065 if (v <= 0) { 066 throw new IllegalStateException("ReferenceCount already released"); 067 } 068 069 if (count.compareAndSet(v, v - 1)) { 070 if (v == 1) { 071 onRelease.run(); 072 } 073 074 break; 075 } 076 } 077 } 078 079 // ******************************* 080 // Helpers 081 // ******************************* 082 083 public static ReferenceCount on(Runnable onFirst, Runnable onRelease) { 084 return new ReferenceCount(onFirst, onRelease); 085 } 086 087 public static ReferenceCount onRelease(Runnable onRelease) { 088 return new ReferenceCount(() -> { }, onRelease); 089 } 090}