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

org.jreleaser.model.internal.hooks.CommandHooks 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.2.0
 */
public final class CommandHooks extends AbstractActivatable implements Domain {
    private static final long serialVersionUID = 8839490415968211931L;

    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.CommandHooks immutable = new org.jreleaser.model.api.hooks.CommandHooks() {
        private static final long serialVersionUID = -7458290511859894710L;

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

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

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

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

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

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

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

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

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

    public CommandHooks() {
        enabledSet(true);
    }

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

    @Override
    public void merge(CommandHooks 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(CommandHook hook) {
        if (null != hook) {
            this.before.add(hook);
        }
    }

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

    public void addFailure(CommandHook 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 (CommandHook hook : getBefore()) {
            m.put("hook " + (i++), hook.asMap(full));
        }
        map.put("before", m);

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

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

        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy