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 mvn repo dependency. The coordinate is
037    /// 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    /// Group id.
070    ///
071    /// @return the string
072    ///
073    @Override
074    public String groupId() {
075        return groupId;
076    }
077
078    /// Artifact id.
079    ///
080    /// @return the string
081    ///
082    @Override
083    public String artifactId() {
084        return artifactId;
085    }
086
087    @Override
088    public String classifier() {
089        return classifier;
090    }
091
092    @Override
093    public String mvnType() {
094        return mvnType;
095    }
096
097    /// Version.
098    ///
099    /// @return the string (defaults to "")
100    ///
101    @Override
102    public String version() {
103        return version;
104    }
105
106    @Override
107    public int hashCode() {
108        final int prime = 31;
109        int result = super.hashCode();
110        result = prime * result
111            + Objects.hash(groupId, artifactId, classifier, mvnType, version);
112        return result;
113    }
114
115    @Override
116    public boolean equals(Object obj) {
117        if (this == obj) {
118            return true;
119        }
120        if (!super.equals(obj)) {
121            return false;
122        }
123        return (obj instanceof DefaultMvnRepoResource other)
124            && Objects.equals(artifactId, other.artifactId)
125            && Objects.equals(groupId, other.groupId)
126            && Objects.equals(classifier, other.classifier)
127            && Objects.equals(mvnType, other.mvnType)
128            && Objects.equals(version, other.version);
129    }
130
131    /// To string.
132    ///
133    /// @return the string
134    ///
135    @Override
136    public String toString() {
137        return type().toString() + " " + coordinates();
138    }
139}