001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.named;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.Objects;
025
026/**
027 * A named lock key.
028 * <p>
029 * Instances of this class are immutable, implement hash/equals and toString method (for easier debug).
030 *
031 * @since 2.0.0
032 */
033public final class NamedLockKey {
034    private final String name;
035
036    private final Collection<String> resources;
037
038    private NamedLockKey(String name, Collection<String> resources) {
039        this.name = Objects.requireNonNull(name, "name");
040        this.resources = Collections.unmodifiableCollection(Objects.requireNonNull(resources, "resources"));
041    }
042
043    /**
044     * Returns this key name, never {@code null}. This is the identity of this lock key instance and is the only
045     * thing that is used in implementations of hash/equals, etc.
046     */
047    public String name() {
048        return name;
049    }
050
051    /**
052     * Auxiliary information, not used by Resolver. Meant to return resource name(s) or any kind of identifiers
053     * protected by this key, never {@code null}.
054     * <p>
055     * Contents on this field is consumer project specific, and should be used only as "extra information":
056     * resolver itself uses these only for logging purposes.
057     */
058    public Collection<String> resources() {
059        return resources;
060    }
061
062    @Override
063    public boolean equals(Object o) {
064        if (this == o) {
065            return true;
066        }
067        if (o == null || getClass() != o.getClass()) {
068            return false;
069        }
070        NamedLockKey that = (NamedLockKey) o;
071        return Objects.equals(name, that.name);
072    }
073
074    @Override
075    public int hashCode() {
076        return Objects.hash(name);
077    }
078
079    @Override
080    public String toString() {
081        return getClass().getSimpleName() + "{" + "name='" + name + '\'' + ", resources=" + resources + '}';
082    }
083
084    public static NamedLockKey of(String name, String... resources) {
085        return of(name, Arrays.asList(resources));
086    }
087
088    public static NamedLockKey of(String name, Collection<String> resources) {
089        return new NamedLockKey(name, resources);
090    }
091}