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.Collection;
022import java.util.Collections;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * A factory of {@link NamedLock}s.
028 */
029public interface NamedLockFactory {
030    /**
031     * Creates or reuses existing {@link NamedLock}. Returns instance MUST BE treated as "resource", best in
032     * try-with-resource block.
033     *
034     * @param key the lock key, must not be {@code null}.
035     * @return named lock instance, never {@code null}.
036     */
037    default NamedLock getLock(NamedLockKey key) {
038        requireNonNull(key, "key");
039        return getLock(Collections.singleton(key));
040    }
041
042    /**
043     * Creates or reuses existing {@link NamedLock}. Returns instance MUST BE treated as "resource", best in
044     * try-with-resource block.
045     *
046     * @param keys the lock keys, must not be {@code null} and must not be empty collection.
047     * @return named lock instance, never {@code null}.
048     * @since 2.0.0
049     */
050    NamedLock getLock(Collection<NamedLockKey> keys);
051
052    /**
053     * Performs a clean shut down of the factory.
054     */
055    void shutdown();
056
057    /**
058     * Method to notify factory about locking failure, to make it possible to provide more (factory specific)
059     * information about factory state when a locking operation failed. Factory may alter provided failure or
060     * provide information via some other side effect (for example via logging).
061     * <p>
062     * The default implementation merely does what happened before: adds no extra information.
063     *
064     * @since 1.9.11
065     */
066    default <E extends Throwable> E onFailure(E failure) {
067        return failure;
068    }
069}