2 * Copyright (c) 2019 PANTHEON.tech s.r.o. 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.mdsal.yanglib.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.collect.ImmutableList;
16 import java.util.Collection;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 * A reference to a YANG source, potentially containing a location hint.
27 public final class SourceReference extends AbstractSimpleIdentifiable<SourceIdentifier> implements Immutable {
28 // List vs. Set vs. Collection here is a class design decision based on use. We expect these objects to be used
29 // frequently, but for a short time. Locations may end up being ignored by SchemaContextResolver, hence it does not
30 // make sense to ensure a Set here just to de-duplicate URLs.
32 // This transports their iteration order, hence we accept Collection construction argument, but keep the immutable
34 private final ImmutableList<URL> locations;
36 private SourceReference(final SourceIdentifier identifier, final ImmutableList<URL> locations) {
38 this.locations = requireNonNull(locations);
41 public static SourceReference of(final SourceIdentifier identifier) {
42 return new SourceReference(identifier, ImmutableList.of());
45 public static SourceReference of(final SourceIdentifier identifier, final URL location) {
46 return new SourceReference(identifier, ImmutableList.of(location));
49 public static SourceReference of(final SourceIdentifier identifier, final Collection<URL> locations) {
50 return new SourceReference(identifier, ImmutableList.copyOf(locations));
53 public ImmutableList<URL> getLocations() {
58 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
59 super.addToStringAttributes(toStringHelper);
60 if (!locations.isEmpty()) {
61 toStringHelper.add("locations", locations);
63 return toStringHelper;