001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.mvnrepo;
020
021import java.nio.file.Path;
022import java.util.List;
023import java.util.Optional;
024import org.eclipse.aether.repository.RemoteRepository;
025import org.jdrupes.builder.api.Project;
026import org.jdrupes.builder.api.Resource;
027import org.jdrupes.builder.api.ResourceFactory;
028import org.jdrupes.builder.api.ResourceType;
029import static org.jdrupes.builder.core.CoreResourceFactory.createNarrowed;
030
031/// A factory for creating Maven repository related resource objects.
032///
033public class MvnRepoResourceFactory implements ResourceFactory {
034
035    /// Instantiates a new Maven repository resource factory.
036    ///
037    public MvnRepoResourceFactory() {
038        // Make javadoc happy
039    }
040
041    @SuppressWarnings("unchecked")
042    @Override
043    public <T extends Resource> Optional<T> newResource(ResourceType<T> type,
044            Project project, Object... args) {
045        // ? extends MvnRepoResource
046        var candidate = createNarrowed(type, MvnRepoResource.class,
047            () -> new DefaultMvnRepoResource(
048                (ResourceType<? extends MvnRepoResource>) type,
049                (String) args[0]));
050        if (candidate.isPresent()) {
051            return candidate;
052        }
053
054        // ? extends MvnRepoLibraryJar
055        candidate = createNarrowed(type, MvnRepoLibraryJarFile.class,
056            () -> new DefaultMvnRepoLibraryJarFile(
057                (ResourceType<? extends MvnRepoLibraryJarFile>) type,
058                (List<RemoteRepository>) args[0], (String) args[1],
059                (Path) args[2]));
060        if (candidate.isPresent()) {
061            return candidate;
062        }
063
064        // ? extends MvnRepoJar
065        candidate = createNarrowed(type, MvnRepoJarFile.class,
066            () -> new DefaultMvnRepoJarFile(
067                (ResourceType<? extends MvnRepoJarFile>) type,
068                (List<RemoteRepository>) args[0], (String) args[1],
069                (Path) args[2]));
070        return candidate;
071    }
072
073}