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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
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;
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.
24 public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
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
32 * The source is immediately available, via a lookup or similar.
36 * The source is available via a computation. For transformation-type
37 * computation, the cost of acquiring the cost needs to be added, too.
41 * The source is available by performing local IO, such that reading
46 * The source is available by performing remote IO, such as fetching
47 * from an HTTP server or similar.
51 private final int value;
53 Costs(final int value) {
60 * @return Const constant.
62 public int getValue() {
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;
72 private PotentialSchemaSource(final SourceIdentifier sourceIdentifier, final Class<? extends T> representation,
74 this.representation = Preconditions.checkNotNull(representation);
75 this.sourceIdentifier = Preconditions.checkNotNull(sourceIdentifier);
76 Preconditions.checkArgument(cost >= 0, "cost has to be non-negative");
80 public static <T extends SchemaSourceRepresentation> PotentialSchemaSource<T> create(
81 final SourceIdentifier sourceIdentifier, final Class<? extends T> representation, final int cost) {
82 return new PotentialSchemaSource<>(sourceIdentifier, representation, cost);
86 * Return a cached reference to an object equal to this object.
88 * @return A potentially shared reference, not guaranteed to be unique.
90 public PotentialSchemaSource<T> cachedReference() {
91 return CACHE.getReference(this);
94 public SourceIdentifier getSourceIdentifier() {
95 return sourceIdentifier;
98 public Class<? extends T> getRepresentation() {
99 return representation;
102 public int getCost() {
107 public int hashCode() {
108 final int prime = 31;
110 result = prime * result + cost;
111 result = prime * result + representation.hashCode();
112 result = prime * result + sourceIdentifier.hashCode();
117 public boolean equals(final Object obj) {
121 if (!(obj instanceof PotentialSchemaSource)) {
124 final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
125 if (cost != other.cost) {
128 if (!representation.equals(other.representation)) {
131 if (!sourceIdentifier.equals(other.sourceIdentifier)) {