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

com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContext Maven / Gradle / Ivy

The newest version!
/*
 *  The MIT License
 *
 *  Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;

import com.sonyericsson.hudson.plugins.gerrit.gerritevents.dto.events.PatchsetCreated;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;

import java.util.LinkedList;
import java.util.List;

/**
 * This bean contains information to the
 * {@link com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause}
 * about what other builds was involved in the same event.
 *
 * For backwards compatibility reasons this class is serialized by the help of the
 * XStream converter {@link com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContextConverter}
 * so any future additions to this class needs to be handled in that class as well or it won't be serialized correctly.
 *
 * @author Robert Sandell <[email protected]>
 */
public class TriggerContext {

    private PatchsetCreated event;
    private TriggeredItemEntity thisBuild;
    private List others;

    /**
     * standard constructor.
     *
     * @param thisBuild this build.
     * @param event     the event for this context.
     * @param others    the other building and untriggered builds.
     */
    public TriggerContext(AbstractBuild thisBuild,
                          PatchsetCreated event,
                          List others) {
        this.thisBuild = new TriggeredItemEntity(thisBuild);
        this.event = event;
        this.others = others;
    }

    /**
     * Standard constructor.
     *
     * @param event the event for this context.
     */
    public TriggerContext(PatchsetCreated event) {
        this.event = event;
    }

    /**
     * Default constructor.
     */
    public TriggerContext() {
    }

    /**
     * A list of builds that was triggered by the same event as "this" build.
     *
     * @return the builds.
     */
    public synchronized List getOthers() {
        return others;
    }

    /**
     * A list of builds that was triggered by the same event as "this" build.
     * Could contain non triggered builds represented by
     * {@link TriggeredItemEntity#getBuild()} == null.
     * 
     * Do not use this method unless you are a serializer,
     * use {@link #addOtherBuild(hudson.model.AbstractBuild)} for adding builds.
     * 
     *
     * @param otherBuilds the builds.
     */
    public synchronized void setOthers(List otherBuilds) {
        this.others = otherBuilds;
    }

    /**
     * The build that this context represents.
     *
     * @return the build.
     */
    public synchronized TriggeredItemEntity getThisBuild() {
        return thisBuild;
    }

    /**
     * The build that this context represents.
     *
     * @param thisBuild the build.
     */
    public synchronized void setThisBuild(TriggeredItemEntity thisBuild) {
        this.thisBuild = thisBuild;
    }

    /**
     * The build that this context represents.
     *
     * @param thisBuild the build.
     */
    public synchronized void setThisBuild(AbstractBuild thisBuild) {
        this.thisBuild = new TriggeredItemEntity(thisBuild);
    }

    /**
     * The event for this context.
     *
     * @return the event.
     */
    public PatchsetCreated getEvent() {
        return event;
    }

    /**
     * The event for this context.
     *
     * @param event the event.
     */
    void setEvent(PatchsetCreated event) {
        this.event = event;
    }

    /**
     * Adds a build to the list of other builds if it doesn't exist in the list.
     * Also if the build's project exists in the list of other projects,
     * the project will be removed from that list.
     *
     * @param build the build to add.
     * @see #getOtherBuilds()
     */
    public synchronized void addOtherBuild(AbstractBuild build) {
        if (others == null) {
            others = new LinkedList();
        }
        TriggeredItemEntity other = findOtherBuild(build);
        if (other == null) {
            other = findOtherProject(build.getProject());
            if (other != null) {
                other.setBuild(build);
            } else {
                others.add(new TriggeredItemEntity(build));
            }
        }
    }

    /**
     * Adds a project to the list of other projects if it doesn't exist in the list.
     *
     * @param project the project to add.
     * @see #getOtherProjects()
     */
    public synchronized void addOtherProject(AbstractProject project) {
        if (others == null) {
            others = new LinkedList();
        }
        if (findOtherProject(project) == null) {
            others.add(new TriggeredItemEntity(project));
        }
    }

    /**
     * Tells if there are any other builds or projects in this context.
     *
     * @return true if it is so.
     * @see #getOtherBuilds()
     * @see #getOtherProjects()
     */
    public synchronized boolean hasOthers() {
        return (others != null && !others.isEmpty());
    }

    /**
     * finds the orther object for the specified build, or null if the build does not exist.
     *
     * @param build a build.
     * @return the other object if there is some, null if there is none.
     */
    private synchronized TriggeredItemEntity findOtherBuild(AbstractBuild build) {
        for (TriggeredItemEntity other : others) {
            if (other.equals(build)) {
                return other;
            }
        }
        return null;
    }

    /**
     * Finds the object for the specified project, or null if the project does not exists in the list.
     *
     * @param project the project.
     * @return the other object, or null if none.
     */
    private synchronized TriggeredItemEntity findOtherProject(AbstractProject project) {
        for (TriggeredItemEntity other : others) {
            if (other.equals(project)) {
                return other;
            }
        }
        return null;
    }

    /**
     * Gets all the other builds in this context.
     * If some project hasn't started a build yet, that project will be unrepresented in this list.
     *
     * @return a list of builds from this context.
     */
    public synchronized List getOtherBuilds() {
        List list = new LinkedList();
        if (others != null) {
            for (TriggeredItemEntity entity : others) {
                if (entity.getBuild() != null) {
                    list.add(entity.getBuild());
                }
            }
        }
        return list;
    }

    /**
     * Gets all the other projects in this context.
     *
     * @return a list of projects from this context.
     */
    public synchronized List getOtherProjects() {
        List list = new LinkedList();
        if (others != null) {
            for (TriggeredItemEntity entity : others) {
                if (entity.getProject() != null) {
                    list.add(entity.getProject());
                }
            }
        }
        return list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy