2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.spi;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
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.
26 public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
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
34 * The source is immediately available, via a lookup or similar.
38 * The source is available via a computation. For transformation-type
39 * computation, the cost of acquiring the cost needs to be added, too.
43 * The source is available by performing local IO, such that reading
48 * The source is available by performing remote IO, such as fetching
49 * from an HTTP server or similar.
53 private final int value;
55 Costs(final int value) {
62 * @return Const constant.
64 public int getValue() {
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;
74 private PotentialSchemaSource(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation,
76 this.representation = requireNonNull(representation);
77 this.sourceIdentifier = requireNonNull(sourceIdentifier);
78 checkArgument(cost >= 0, "cost has to be non-negative");
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);
88 * Return a cached reference to an object equal to this object.
90 * @return A potentially shared reference, not guaranteed to be unique.
92 @SuppressWarnings("unchecked")
93 public PotentialSchemaSource<T> cachedReference() {
94 return (PotentialSchemaSource<T>) INTERNER.intern(this);
97 public SourceIdentifier getSourceIdentifier() {
98 return sourceIdentifier;
101 public Class<? extends T> getRepresentation() {
102 return representation;
105 public int getCost() {
110 public int hashCode() {
111 return Objects.hash(cost, representation, sourceIdentifier);
115 public boolean equals(final Object obj) {
119 if (!(obj instanceof PotentialSchemaSource)) {
122 final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
123 return cost == other.cost && representation.equals(other.representation)
124 && sourceIdentifier.equals(other.sourceIdentifier);