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