View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Objects;
25  
26  /**
27   * A named lock key.
28   * <p>
29   * Instances of this class are immutable, implement hash/equals and toString method (for easier debug).
30   *
31   * @since 2.0.0
32   */
33  public final class NamedLockKey {
34      private final String name;
35  
36      private final Collection<String> resources;
37  
38      private NamedLockKey(String name, Collection<String> resources) {
39          this.name = Objects.requireNonNull(name, "name");
40          this.resources = Collections.unmodifiableCollection(Objects.requireNonNull(resources, "resources"));
41      }
42  
43      /**
44       * Returns this key name, never {@code null}. This is the identity of this lock key instance and is the only
45       * thing that is used in implementations of hash/equals, etc.
46       */
47      public String name() {
48          return name;
49      }
50  
51      /**
52       * Auxiliary information, not used by Resolver. Meant to return resource name(s) or any kind of identifiers
53       * protected by this key, never {@code null}.
54       * <p>
55       * Contents on this field is consumer project specific, and should be used only as "extra information":
56       * resolver itself uses these only for logging purposes.
57       */
58      public Collection<String> resources() {
59          return resources;
60      }
61  
62      @Override
63      public boolean equals(Object o) {
64          if (this == o) {
65              return true;
66          }
67          if (o == null || getClass() != o.getClass()) {
68              return false;
69          }
70          NamedLockKey that = (NamedLockKey) o;
71          return Objects.equals(name, that.name);
72      }
73  
74      @Override
75      public int hashCode() {
76          return Objects.hash(name);
77      }
78  
79      @Override
80      public String toString() {
81          return getClass().getSimpleName() + "{" + "name='" + name + '\'' + ", resources=" + resources + '}';
82      }
83  
84      public static NamedLockKey of(String name, String... resources) {
85          return of(name, Arrays.asList(resources));
86      }
87  
88      public static NamedLockKey of(String name, Collection<String> resources) {
89          return new NamedLockKey(name, resources);
90      }
91  }