50c14ec3e50cc429fecf7d91461ba816fc3c1bd0
[mdsal.git] / yanglib / mdsal-yanglib-api / src / main / java / org / opendaylight / mdsal / yanglib / api / SourceReference.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.yanglib.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.collect.ImmutableList;
15 import java.net.URL;
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;
21
22 /**
23  * A reference to a YANG source, potentially containing a location hint.
24  */
25 @Beta
26 @NonNullByDefault
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.
31     //
32     // This transports their iteration order, hence we accept Collection construction argument, but keep the immutable
33     // copy as a List
34     private final ImmutableList<URL> locations;
35
36     private SourceReference(final SourceIdentifier identifier, final ImmutableList<URL> locations) {
37         super(identifier);
38         this.locations = requireNonNull(locations);
39     }
40
41     public static SourceReference of(final SourceIdentifier identifier) {
42         return new SourceReference(identifier, ImmutableList.of());
43     }
44
45     public static SourceReference of(final SourceIdentifier identifier, final URL location) {
46         return new SourceReference(identifier, ImmutableList.of(location));
47     }
48
49     public static SourceReference of(final SourceIdentifier identifier, final Collection<URL> locations) {
50         return new SourceReference(identifier, ImmutableList.copyOf(locations));
51     }
52
53     public ImmutableList<URL> getLocations() {
54         return locations;
55     }
56
57     @Override
58     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
59         super.addToStringAttributes(toStringHelper);
60         if (!locations.isEmpty()) {
61             toStringHelper.add("locations", locations);
62         }
63         return toStringHelper;
64     }
65 }