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