BUG-1796: implement SourceIdentifier/PotentialSchemaSource caching
[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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.model.repo.spi;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12
13 import org.opendaylight.yangtools.objcache.ObjectCache;
14 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
16 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
17
18 /**
19  * A potential schema source. Instances of this class track the various
20  * representations of a schema source and the cost attached to obtaining
21  * the source from them.
22  */
23 @Beta
24 public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
25     /**
26      * Each registered source has a cost associated with it. Since a particular
27      * representation can be acquired by various means, here are general constants
28      * for common cases.
29      */
30     public enum Costs {
31         /**
32          * The source is immediately available, via a lookup or similar.
33          */
34         IMMEDIATE(0),
35         /**
36          * The source is available via a computation. For transformation-type
37          * computation, the cost of acquiring the cost needs to be added, too.
38          */
39         COMPUTATION(1),
40         /**
41          * The source is available by performing local IO, such that reading
42          * from a disk.
43          */
44         LOCAL_IO(4),
45         /**
46          * The source is available by performing remote IO, such as fetching
47          * from an HTTP server or similar.
48          */
49         REMOTE_IO(8);
50
51         private final int value;
52
53         private Costs(final int value) {
54             this.value = value;
55         }
56
57         /**
58          * The the cost value.
59          *
60          * @return Const constant.
61          */
62         public int getValue() {
63             return value;
64         }
65     }
66
67     private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(PotentialSchemaSource.class);
68     private final Class<? extends T> representation;
69     private final SourceIdentifier sourceIdentifier;
70     private final int cost;
71
72     private PotentialSchemaSource(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation, final int cost) {
73         this.representation = Preconditions.checkNotNull(representation);
74         this.sourceIdentifier = Preconditions.checkNotNull(sourceIdentifier);
75         Preconditions.checkArgument(cost >= 0, "cost has to be non-negative");
76         this.cost = cost;
77     }
78
79     public static final <T extends SchemaSourceRepresentation> PotentialSchemaSource<T> create(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation, final int cost) {
80         return new PotentialSchemaSource<>(sourceIdentifier, representation, cost);
81     }
82
83     /**
84      * Return a cached reference to an object equal to this object.
85      *
86      * @return A potentially shared reference, not guaranteed to be unique.
87      */
88     public PotentialSchemaSource<T> cachedReference() {
89         return CACHE.getReference(this);
90     }
91
92     public SourceIdentifier getSourceIdentifier() {
93         return sourceIdentifier;
94     }
95
96     public Class<? extends T> getRepresentation() {
97         return representation;
98     }
99
100     public int getCost() {
101         return cost;
102     }
103
104     @Override
105     public int hashCode() {
106         final int prime = 31;
107         int result = 1;
108         result = prime * result + cost;
109         result = prime * result + representation.hashCode();
110         result = prime * result + sourceIdentifier.hashCode();
111         return result;
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         if (cost != other.cost) {
124             return false;
125         }
126         if (!representation.equals(other.representation)) {
127             return false;
128         }
129         if (!sourceIdentifier.equals(other.sourceIdentifier)) {
130             return false;
131         }
132         return true;
133     }
134 }