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.java;
020
021import java.util.Objects;
022import java.util.Optional;
023import java.util.jar.Attributes;
024import org.jdrupes.builder.api.Resource;
025import org.jdrupes.builder.api.ResourceType;
026import org.jdrupes.builder.core.ResourceObject;
027import static org.jdrupes.builder.java.JavaTypes.ManifestAttributesType;
028
029/// A wrapper around [Attributes] to allow their usage as [Resource].
030/// Because [Attributes] is a class and not an interface, this resource
031/// type cannot be specialized without an actual implementation.
032///
033public class ManifestAttributes extends Attributes implements Resource {
034
035    private final ResourceObject resourceDelegee;
036
037    /// Initializes a new manifest attributes.
038    ///
039    public ManifestAttributes() {
040        this.resourceDelegee = new ResourceObject(ManifestAttributesType) {};
041    }
042
043    @Override
044    public ResourceType<?> type() {
045        return resourceDelegee.type();
046    }
047
048    @Override
049    public Optional<String> name() {
050        return resourceDelegee.name();
051    }
052
053    /// Sets the name of the resource.
054    ///
055    /// @param name the name
056    /// @return the resource object
057    ///
058    @SuppressWarnings("PMD.LooseCoupling")
059    public final ManifestAttributes name(String name) {
060        resourceDelegee.name(name);
061        return this;
062    }
063
064    @Override
065    public Object put(Object name, Object value) {
066        if (resourceDelegee.isLocked()) {
067            throw new IllegalStateException(
068                "Attributes may only be set immediately after creation.");
069        }
070        return super.put(name, value);
071    }
072
073    @Override
074    public Object remove(Object name) {
075        if (resourceDelegee.isLocked()) {
076            throw new IllegalStateException(
077                "Attributes may only be removed immediately after creation.");
078        }
079        return super.remove(name);
080    }
081
082    @Override
083    public int hashCode() {
084        final int prime = 31;
085        int result = super.hashCode();
086        result = prime * result + Objects.hash(resourceDelegee);
087        return result;
088    }
089
090    @Override
091    public boolean equals(Object obj) {
092        if (this == obj) {
093            return true;
094        }
095        if (!super.equals(obj)) {
096            return false;
097        }
098        if (!(obj instanceof ManifestAttributes)) {
099            return false;
100        }
101        @SuppressWarnings("PMD.LooseCoupling")
102        ManifestAttributes other = (ManifestAttributes) obj;
103        return Objects.equals(resourceDelegee, other.resourceDelegee);
104    }
105
106}