001/*
002 * JDrupes Builder
003 * Copyright (C) 2026 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.core;
020
021import java.util.Objects;
022import org.jdrupes.builder.api.Project;
023import org.jdrupes.builder.api.ProjectVersion;
024
025/// Default implementation of [ProjectVersion].
026///
027public class DefaultProjectVersion extends ResourceObject
028        implements ProjectVersion {
029
030    private final Project project;
031    private final String version;
032
033    /// Initializes a new default project version.
034    ///
035    /// @param project the project
036    /// @param version the version
037    ///
038    protected DefaultProjectVersion(Project project, String version) {
039        this.project = Objects.requireNonNull(project);
040        this.version = Objects.requireNonNull(version);
041    }
042
043    @Override
044    public Project project() {
045        return project;
046    }
047
048    @Override
049    public String version() {
050        return version;
051    }
052
053    @Override
054    public int hashCode() {
055        final int prime = 31;
056        int result = super.hashCode();
057        result = prime * result + Objects.hash(project, version);
058        return result;
059    }
060
061    @Override
062    public boolean equals(Object obj) {
063        if (this == obj) {
064            return true;
065        }
066        if (!super.equals(obj)) {
067            return false;
068        }
069        if (!(obj instanceof DefaultProjectVersion)) {
070            return false;
071        }
072        DefaultProjectVersion other = (DefaultProjectVersion) obj;
073        return Objects.equals(project, other.project)
074            && Objects.equals(version, other.version);
075    }
076
077    @Override
078    public String toString() {
079        var dir = project().rootProject().relativize(project().directory())
080            .toString();
081        return project().name() + ": " + version() + " ("
082            + (dir.isBlank() ? "." : dir) + ")";
083    }
084}