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 final String version;
033
034    /// Instantiates a new default mvn repo dependency.
035    ///
036    /// @param type the type
037    /// @param coordinate the coordinate
038    ///
039    @SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
040    public DefaultMvnRepoResource(ResourceType<? extends MvnRepoResource> type,
041            String coordinate) {
042        super(type);
043        var parts = Objects.requireNonNull(coordinate).split(":");
044        if (parts.length != 3) {
045            throw new IllegalArgumentException(
046                "Invalid maven coordinate: " + coordinate);
047        }
048        groupId = parts[0];
049        artifactId = parts[1];
050        version = parts[2];
051    }
052
053    /// Group id.
054    ///
055    /// @return the string
056    ///
057    @Override
058    public String groupId() {
059        return groupId;
060    }
061
062    /// Artifact id.
063    ///
064    /// @return the string
065    ///
066    @Override
067    public String artifactId() {
068        return artifactId;
069    }
070
071    /// Version.
072    ///
073    /// @return the string
074    ///
075    @Override
076    public String version() {
077        return version;
078    }
079
080    @Override
081    public int hashCode() {
082        final int prime = 31;
083        int result = super.hashCode();
084        result = prime * result
085            + Objects.hash(artifactId, groupId, version);
086        return result;
087    }
088
089    @Override
090    public boolean equals(Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (!super.equals(obj)) {
095            return false;
096        }
097        return (obj instanceof DefaultMvnRepoResource other)
098            && Objects.equals(artifactId, other.artifactId)
099            && Objects.equals(groupId, other.groupId)
100            && Objects.equals(version, other.version);
101    }
102
103    /// To string.
104    ///
105    /// @return the string
106    ///
107    @Override
108    public String toString() {
109        return type().toString() + " " + coordinates();
110    }
111}