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.api.source.SourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
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.
25 public final class PotentialSchemaSource<T extends SourceRepresentation> {
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
33 * The source is immediately available, via a lookup or similar.
37 * The source is available via a computation. For transformation-type
38 * computation, the cost of acquiring the cost needs to be added, too.
42 * The source is available by performing local IO, such that reading
47 * The source is available by performing remote IO, such as fetching
48 * from an HTTP server or similar.
52 private final int value;
54 Costs(final int value) {
61 * @return A constant cost.
63 public int getValue() {
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;
73 private PotentialSchemaSource(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation,
75 this.representation = requireNonNull(representation);
76 this.sourceIdentifier = requireNonNull(sourceIdentifier);
77 checkArgument(cost >= 0, "cost has to be non-negative");
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);
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());
92 * Return a cached reference to an object equal to this object.
94 * @return A potentially shared reference, not guaranteed to be unique.
96 @SuppressWarnings("unchecked")
97 public PotentialSchemaSource<T> cachedReference() {
98 return (PotentialSchemaSource<T>) INTERNER.intern(this);
101 public SourceIdentifier getSourceIdentifier() {
102 return sourceIdentifier;
105 public Class<? extends T> getRepresentation() {
106 return representation;
109 public int getCost() {
114 public int hashCode() {
115 return Objects.hash(cost, representation, sourceIdentifier);
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);