1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.database.sqlite; 18 19 import android.annotation.UnsupportedAppUsage; 20 import java.io.Closeable; 21 22 /** 23 * An object created from a SQLiteDatabase that can be closed. 24 * 25 * This class implements a primitive reference counting scheme for database objects. 26 */ 27 public abstract class SQLiteClosable implements Closeable { 28 @UnsupportedAppUsage 29 private int mReferenceCount = 1; 30 31 /** 32 * Called when the last reference to the object was released by 33 * a call to {@link #releaseReference()} or {@link #close()}. 34 */ onAllReferencesReleased()35 protected abstract void onAllReferencesReleased(); 36 37 /** 38 * Called when the last reference to the object was released by 39 * a call to {@link #releaseReferenceFromContainer()}. 40 * 41 * @deprecated Do not use. 42 */ 43 @Deprecated onAllReferencesReleasedFromContainer()44 protected void onAllReferencesReleasedFromContainer() { 45 onAllReferencesReleased(); 46 } 47 48 /** 49 * Acquires a reference to the object. 50 * 51 * @throws IllegalStateException if the last reference to the object has already 52 * been released. 53 */ acquireReference()54 public void acquireReference() { 55 synchronized(this) { 56 if (mReferenceCount <= 0) { 57 throw new IllegalStateException( 58 "attempt to re-open an already-closed object: " + this); 59 } 60 mReferenceCount++; 61 } 62 } 63 64 /** 65 * Releases a reference to the object, closing the object if the last reference 66 * was released. 67 * 68 * @see #onAllReferencesReleased() 69 */ releaseReference()70 public void releaseReference() { 71 boolean refCountIsZero = false; 72 synchronized(this) { 73 refCountIsZero = --mReferenceCount == 0; 74 } 75 if (refCountIsZero) { 76 onAllReferencesReleased(); 77 } 78 } 79 80 /** 81 * Releases a reference to the object that was owned by the container of the object, 82 * closing the object if the last reference was released. 83 * 84 * @see #onAllReferencesReleasedFromContainer() 85 * @deprecated Do not use. 86 */ 87 @Deprecated releaseReferenceFromContainer()88 public void releaseReferenceFromContainer() { 89 boolean refCountIsZero = false; 90 synchronized(this) { 91 refCountIsZero = --mReferenceCount == 0; 92 } 93 if (refCountIsZero) { 94 onAllReferencesReleasedFromContainer(); 95 } 96 } 97 98 /** 99 * Releases a reference to the object, closing the object if the last reference 100 * was released. 101 * 102 * Calling this method is equivalent to calling {@link #releaseReference}. 103 * 104 * @see #releaseReference() 105 * @see #onAllReferencesReleased() 106 */ close()107 public void close() { 108 releaseReference(); 109 } 110 } 111