Merge branch 'master' of ../controller
[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 A constant cost.
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     public static <T extends SchemaSourceRepresentation> PotentialSchemaSource<T> create(
88             final SourceIdentifier sourceIdentifier, final Class<? extends T> representation, final Costs cost) {
89         return new PotentialSchemaSource<>(sourceIdentifier, representation, cost.getValue());
90     }
91
92     /**
93      * Return a cached reference to an object equal to this object.
94      *
95      * @return A potentially shared reference, not guaranteed to be unique.
96      */
97     @SuppressWarnings("unchecked")
98     public PotentialSchemaSource<T> cachedReference() {
99         return (PotentialSchemaSource<T>) INTERNER.intern(this);
100     }
101
102     public SourceIdentifier getSourceIdentifier() {
103         return sourceIdentifier;
104     }
105
106     public Class<? extends T> getRepresentation() {
107         return representation;
108     }
109
110     public int getCost() {
111         return cost;
112     }
113
114     @Override
115     public int hashCode() {
116         return Objects.hash(cost, representation, sourceIdentifier);
117     }
118
119     @Override
120     public boolean equals(final Object obj) {
121         if (this == obj) {
122             return true;
123         }
124         if (!(obj instanceof PotentialSchemaSource)) {
125             return false;
126         }
127         final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
128         return cost == other.cost && representation.equals(other.representation)
129                 && sourceIdentifier.equals(other.sourceIdentifier);
130     }
131 }