Fold yang-model-util-ut
[yangtools.git] / yang / yang-repo-spi / src / main / java / org / opendaylight / yangtools / yang / model / repo / spi / 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.spi;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 final class RefcountedRegistration {
14     private final SchemaSourceRegistration<?> reg;
15     private int refcount = 1;
16
17     RefcountedRegistration(final SchemaSourceRegistration<?> reg) {
18         this.reg = requireNonNull(reg);
19     }
20
21     public void incRef() {
22         refcount++;
23     }
24
25     public boolean decRef() {
26         checkState(refcount > 0, "Refcount underflow: %s", refcount);
27
28         if (0 == --refcount) {
29             reg.close();
30             return true;
31         }
32
33         return false;
34     }
35 }