3cd59b2cce7cb2e960f78005d34f0912b5f4b370
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / repo / util / RefcountedRegistration.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.repo.util;
9
10 import com.google.common.base.Preconditions;
11
12 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
13
14 final class RefcountedRegistration {
15     private final SchemaSourceRegistration<?> reg;
16     private int refcount = 1;
17
18     RefcountedRegistration(final SchemaSourceRegistration<?> reg) {
19         this.reg = Preconditions.checkNotNull(reg);
20     }
21
22     public void incRef() {
23         refcount++;
24     }
25
26     public boolean decRef() {
27         Preconditions.checkState(refcount > 0, "Refcount underflow: %s", refcount);
28
29         if (0 == --refcount) {
30             reg.close();
31             return true;
32         } else {
33             return false;
34         }
35     }
36 }