Cleanup use of Guava library
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / spi / PotentialSchemaSource.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.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.Interner;
15 import com.google.common.collect.Interners;
16 import java.util.Objects;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
18 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
19
20 /**
21  * A potential schema source. Instances of this class track the various
22  * representations of a schema source and the cost attached to obtaining
23  * the source from them.
24  */
25 @Beta
26 public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
27     /**
28      * Each registered source has a cost associated with it. Since a particular
29      * representation can be acquired by various means, here are general constants
30      * for common cases.
31      */
32     public enum Costs {
33         /**
34          * The source is immediately available, via a lookup or similar.
35          */
36         IMMEDIATE(0),
37         /**
38          * The source is available via a computation. For transformation-type
39          * computation, the cost of acquiring the cost needs to be added, too.
40          */
41         COMPUTATION(1),
42         /**
43          * The source is available by performing local IO, such that reading
44          * from a disk.
45          */
46         LOCAL_IO(4),
47         /**
48          * The source is available by performing remote IO, such as fetching
49          * from an HTTP server or similar.
50          */
51         REMOTE_IO(8);
52
53         private final int value;
54
55         Costs(final int value) {
56             this.value = value;
57         }
58
59         /**
60          * The the cost value.
61          *
62          * @return Const constant.
63          */
64         public int getValue() {
65             return value;
66         }
67     }
68
69     private static final Interner<PotentialSchemaSource<?>> INTERNER = Interners.newWeakInterner();
70     private final Class<? extends T> representation;
71     private final SourceIdentifier sourceIdentifier;
72     private final int cost;
73
74     private PotentialSchemaSource(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation,
75             final int cost) {
76         this.representation = requireNonNull(representation);
77         this.sourceIdentifier = requireNonNull(sourceIdentifier);
78         checkArgument(cost >= 0, "cost has to be non-negative");
79         this.cost = cost;
80     }
81
82     public static <T extends SchemaSourceRepresentation> PotentialSchemaSource<T> create(
83             final SourceIdentifier sourceIdentifier, final Class<? extends T> representation, final int cost) {
84         return new PotentialSchemaSource<>(sourceIdentifier, representation, cost);
85     }
86
87     /**
88      * Return a cached reference to an object equal to this object.
89      *
90      * @return A potentially shared reference, not guaranteed to be unique.
91      */
92     @SuppressWarnings("unchecked")
93     public PotentialSchemaSource<T> cachedReference() {
94         return (PotentialSchemaSource<T>) INTERNER.intern(this);
95     }
96
97     public SourceIdentifier getSourceIdentifier() {
98         return sourceIdentifier;
99     }
100
101     public Class<? extends T> getRepresentation() {
102         return representation;
103     }
104
105     public int getCost() {
106         return cost;
107     }
108
109     @Override
110     public int hashCode() {
111         return Objects.hash(cost, representation, sourceIdentifier);
112     }
113
114     @Override
115     public boolean equals(final Object obj) {
116         if (this == obj) {
117             return true;
118         }
119         if (!(obj instanceof PotentialSchemaSource)) {
120             return false;
121         }
122         final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
123         return cost == other.cost && representation.equals(other.representation)
124                 && sourceIdentifier.equals(other.sourceIdentifier);
125     }
126 }