SQLiteClosable.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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
17package android.database.sqlite;
18
19/**
20 * An object create from a SQLiteDatabase that can be closed.
21 */
22public abstract class SQLiteClosable {
23    private int mReferenceCount = 1;
24    private Object mLock = new Object();
25    protected abstract void onAllReferencesReleased();
26    protected void onAllReferencesReleasedFromContainer(){}
27
28    public void acquireReference() {
29        synchronized(mLock) {
30            if (mReferenceCount <= 0) {
31                throw new IllegalStateException(
32                        "attempt to acquire a reference on a close SQLiteClosable");
33            }
34            mReferenceCount++;
35        }
36    }
37
38    public void releaseReference() {
39        synchronized(mLock) {
40            mReferenceCount--;
41            if (mReferenceCount == 0) {
42                onAllReferencesReleased();
43            }
44        }
45    }
46
47    public void releaseReferenceFromContainer() {
48        synchronized(mLock) {
49            mReferenceCount--;
50            if (mReferenceCount == 0) {
51                onAllReferencesReleasedFromContainer();
52            }
53        }
54    }
55}
56