All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jreleaser.model.internal.hooks.ScriptHooks Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * Copyright 2020-2024 The JReleaser authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jreleaser.model.internal.hooks;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jreleaser.model.Active;
import org.jreleaser.model.internal.common.AbstractActivatable;
import org.jreleaser.model.internal.common.Domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.toList;

/**
 * @author Andres Almiray
 * @since 1.6.0
 */
public final class ScriptHooks extends AbstractActivatable implements Domain {
    private static final long serialVersionUID = -8421249812402488941L;

    private final List before = new ArrayList<>();
    private final List success = new ArrayList<>();
    private final List failure = new ArrayList<>();
    private final Map environment = new LinkedHashMap<>();

    private String condition;

    @JsonIgnore
    private final org.jreleaser.model.api.hooks.ScriptHooks immutable = new org.jreleaser.model.api.hooks.ScriptHooks() {
        private static final long serialVersionUID = 6010707122339136759L;

        private List before;
        private List success;
        private List failure;

        @Override
        public String getCondition() {
            return ScriptHooks.this.getCondition();
        }

        @Override
        public List getBefore() {
            if (null == before) {
                before = ScriptHooks.this.before.stream()
                    .map(ScriptHook::asImmutable)
                    .collect(toList());
            }
            return before;
        }

        @Override
        public List getSuccess() {
            if (null == success) {
                success = ScriptHooks.this.success.stream()
                    .map(ScriptHook::asImmutable)
                    .collect(toList());
            }
            return success;
        }

        @Override
        public List getFailure() {
            if (null == failure) {
                failure = ScriptHooks.this.failure.stream()
                    .map(ScriptHook::asImmutable)
                    .collect(toList());
            }
            return failure;
        }

        @Override
        public Map getEnvironment() {
            return unmodifiableMap(ScriptHooks.this.getEnvironment());
        }

        @Override
        public Active getActive() {
            return ScriptHooks.this.getActive();
        }

        @Override
        public boolean isEnabled() {
            return ScriptHooks.this.isEnabled();
        }

        @Override
        public Map asMap(boolean full) {
            return Collections.unmodifiableMap(ScriptHooks.this.asMap(full));
        }
    };

    public ScriptHooks() {
        enabledSet(true);
    }

    public org.jreleaser.model.api.hooks.ScriptHooks asImmutable() {
        return immutable;
    }

    @Override
    public void merge(ScriptHooks source) {
        super.merge(source);
        this.condition = merge(this.condition, source.condition);
        setBefore(merge(this.before, source.before));
        setSuccess(merge(this.success, source.success));
        setFailure(merge(this.failure, source.failure));
        setEnvironment(merge(this.environment, source.getEnvironment()));
    }

    @Override
    public boolean isSet() {
        return super.isSet() ||
            !before.isEmpty() ||
            !success.isEmpty() ||
            !failure.isEmpty();
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public List getBefore() {
        return before;
    }

    public void setBefore(List before) {
        this.before.clear();
        this.before.addAll(before);
    }

    public List getSuccess() {
        return success;
    }

    public void setSuccess(List success) {
        this.success.clear();
        this.success.addAll(success);
    }

    public List getFailure() {
        return failure;
    }

    public void setFailure(List failure) {
        this.failure.clear();
        this.failure.addAll(failure);
    }

    public void addBefore(ScriptHook hook) {
        if (null != hook) {
            this.before.add(hook);
        }
    }

    public void addSuccess(ScriptHook hook) {
        if (null != hook) {
            this.success.add(hook);
        }
    }

    public void addFailure(ScriptHook hook) {
        if (null != hook) {
            this.failure.add(hook);
        }
    }

    public Map getEnvironment() {
        return environment;
    }

    public void setEnvironment(Map environment) {
        this.environment.clear();
        this.environment.putAll(environment);
    }

    public void addEnvironment(Map environment) {
        this.environment.putAll(environment);
    }

    @Override
    public Map asMap(boolean full) {
        Map map = new LinkedHashMap<>();
        map.put("enabled", isEnabled());
        map.put("active", getActive());
        map.put("condition", condition);
        map.put("environment", environment);

        Map> m = new LinkedHashMap<>();
        int i = 0;
        for (ScriptHook hook : getBefore()) {
            m.put("hook " + (i++), hook.asMap(full));
        }
        map.put("before", m);

        m = new LinkedHashMap<>();
        i = 0;
        for (ScriptHook hook : getSuccess()) {
            m.put("hook " + (i++), hook.asMap(full));
        }
        map.put("success", m);

        m = new LinkedHashMap<>();
        i = 0;
        for (ScriptHook hook : getFailure()) {
            m.put("hook " + (i++), hook.asMap(full));
        }
        map.put("failure", m);

        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy