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.util.Objects;
022import org.jdrupes.builder.api.ResourceType;
023import org.jdrupes.builder.core.ResourceObject;
024
025/// Represents an artifact in a Maven repository.
026///
027public class DefaultMvnRepoResource extends ResourceObject
028        implements MvnRepoResource {
029
030    private final String groupId;
031    private final String artifactId;
032    private String classifier = "";
033    private String mvnType = "";
034    private String version;
035
036    /// Instantiates a new default Maven repository dependency. The
037    /// coordinate is parsed into its component parts following the schema
038    /// `groupId:artifactId[[:classifier]:type]:version`.
039    ///
040    /// @param type the type
041    /// @param coordinate the coordinate
042    ///
043    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
044    public DefaultMvnRepoResource(ResourceType<? extends MvnRepoResource> type,
045            String coordinate) {
046        super(type);
047        var parts = Objects.requireNonNull(coordinate).split(":");
048        switch (parts.length) {
049        case 5:
050            classifier = parts[2];
051            // fallthrough
052        case 4:
053            mvnType = parts[parts.length - 2];
054            // fallthrough
055        case 3:
056            version = parts[parts.length - 1];
057            // fallthrough
058        case 2:
059            artifactId = parts[1];
060            groupId = parts[0];
061            break;
062        default:
063            throw new IllegalArgumentException(
064                "Invalid maven coordinate: " + coordinate);
065        }
066        name(coordinates());
067    }
068
069    @Override
070    public String groupId() {
071        return groupId;
072    }
073
074    @Override
075    public String artifactId() {
076        return artifactId;
077    }
078
079    @Override
080    public String classifier() {
081        return classifier;
082    }
083
084    @Override
085    public String mvnType() {
086        return mvnType;
087    }
088
089    @Override
090    public String version() {
091        return version;
092    }
093
094    @Override
095    public int hashCode() {
096        final int prime = 31;
097        int result = super.hashCode();
098        result = prime * result
099            + Objects.hash(groupId, artifactId, classifier, mvnType, version);
100        return result;
101    }
102
103    @Override
104    public boolean equals(Object obj) {
105        if (this == obj) {
106            return true;
107        }
108        if (!super.equals(obj)) {
109            return false;
110        }
111        return (obj instanceof DefaultMvnRepoResource other)
112            && Objects.equals(artifactId, other.artifactId)
113            && Objects.equals(groupId, other.groupId)
114            && Objects.equals(classifier, other.classifier)
115            && Objects.equals(mvnType, other.mvnType)
116            && Objects.equals(version, other.version);
117    }
118
119    /// To string.
120    ///
121    /// @return the string
122    ///
123    @Override
124    public String toString() {
125        return type().toString() + " " + coordinates();
126    }
127}