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.checksum; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import java.util.Collection; 026import java.util.Collections; 027import java.util.List; 028import java.util.Map; 029 030import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; 031import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector; 032 033import static java.util.Objects.requireNonNull; 034import static java.util.stream.Collectors.toList; 035 036/** 037 * Default implementation. 038 * 039 * @since 1.8.0 040 */ 041@Singleton 042@Named 043public class DefaultChecksumAlgorithmFactorySelector implements ChecksumAlgorithmFactorySelector { 044 private final Map<String, ChecksumAlgorithmFactory> factories; 045 046 @Inject 047 public DefaultChecksumAlgorithmFactorySelector(Map<String, ChecksumAlgorithmFactory> factories) { 048 this.factories = requireNonNull(factories); 049 } 050 051 @Override 052 public ChecksumAlgorithmFactory select(String algorithmName) { 053 requireNonNull(algorithmName, "algorithmMame must not be null"); 054 ChecksumAlgorithmFactory factory = factories.get(algorithmName); 055 if (factory == null) { 056 throw new IllegalArgumentException(String.format( 057 "Unsupported checksum algorithm %s, supported ones are %s", 058 algorithmName, 059 getChecksumAlgorithmFactories().stream() 060 .map(ChecksumAlgorithmFactory::getName) 061 .collect(toList()))); 062 } 063 return factory; 064 } 065 066 @Override 067 public List<ChecksumAlgorithmFactory> selectList(Collection<String> algorithmNames) { 068 return algorithmNames.stream().map(this::select).collect(toList()); 069 } 070 071 @Override 072 public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() { 073 return Collections.unmodifiableCollection(factories.values()); 074 } 075 076 @Override 077 public boolean isChecksumExtension(String extension) { 078 requireNonNull(extension); 079 if (extension.contains(".")) { 080 return factories.values().stream().anyMatch(a -> extension.endsWith("." + a.getFileExtension())); 081 } else { 082 return factories.values().stream().anyMatch(a -> extension.equals(a.getFileExtension())); 083 } 084 } 085}