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