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.internal.impl.synccontext.named;
020
021import java.util.Collection;
022import java.util.List;
023import java.util.stream.Collectors;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.metadata.Metadata;
028import org.eclipse.aether.named.NamedLockKey;
029import org.eclipse.aether.spi.locking.LockingInhibitor;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * Wrapping {@link NameMapper} class that applies discovered {@link org.eclipse.aether.spi.locking.LockingInhibitor}.
035 *
036 * @since 2.0.14
037 */
038public class InhibitingNameMapper implements NameMapper {
039    private final NameMapper delegate;
040    private final List<LockingInhibitor> lockingInhibitors;
041
042    public InhibitingNameMapper(NameMapper delegate, List<LockingInhibitor> lockingInhibitors) {
043        this.delegate = requireNonNull(delegate);
044        this.lockingInhibitors = requireNonNull(lockingInhibitors);
045    }
046
047    @Override
048    public boolean isFileSystemFriendly() {
049        return delegate.isFileSystemFriendly();
050    }
051
052    @Override
053    public Collection<NamedLockKey> nameLocks(
054            RepositorySystemSession session,
055            Collection<? extends Artifact> artifacts,
056            Collection<? extends Metadata> metadatas) {
057        if (artifacts != null) {
058            artifacts = artifacts.stream()
059                    .filter(a -> lockingInhibitors.stream().noneMatch(i -> i.preventArtifactLocking(a)))
060                    .collect(Collectors.toList());
061        }
062        if (metadatas != null) {
063            metadatas = metadatas.stream()
064                    .filter(m -> lockingInhibitors.stream().noneMatch(i -> i.preventMetadataLocking(m)))
065                    .collect(Collectors.toList());
066        }
067        return delegate.nameLocks(session, artifacts, metadatas);
068    }
069}