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 A constant cost.
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);
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());
93 * Return a cached reference to an object equal to this object.
95 * @return A potentially shared reference, not guaranteed to be unique.
97 @SuppressWarnings("unchecked")
98 public PotentialSchemaSource<T> cachedReference() {
99 return (PotentialSchemaSource<T>) INTERNER.intern(this);
102 public SourceIdentifier getSourceIdentifier() {
103 return sourceIdentifier;
106 public Class<? extends T> getRepresentation() {
107 return representation;
110 public int getCost() {
115 public int hashCode() {
116 return Objects.hash(cost, representation, sourceIdentifier);
120 public boolean equals(final Object obj) {
124 if (!(obj instanceof PotentialSchemaSource)) {
127 final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
128 return cost == other.cost && representation.equals(other.representation)
129 && sourceIdentifier.equals(other.sourceIdentifier);