com.facebook.buck.core.build.event.BuildEvent.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.buck.core.build.event.BuildEvent.java

Source

/*
 * Copyright 2013-present Facebook, Inc.
 *
 * 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
 *
 *     http://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 com.facebook.buck.core.build.event;

import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.event.AbstractBuckEvent;
import com.facebook.buck.event.EventKey;
import com.facebook.buck.event.WorkAdvanceEvent;
import com.facebook.buck.util.ExitCode;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;

/** Base class for events about building. */
public abstract class BuildEvent extends AbstractBuckEvent implements WorkAdvanceEvent {

    public BuildEvent(EventKey eventKey) {
        super(eventKey);
    }

    public static Started started(Iterable<String> buildArgs) {
        return new Started(ImmutableSet.copyOf(buildArgs));
    }

    public static Reset reset() {
        return new Reset();
    }

    public static Finished finished(Started started, ExitCode exitCode) {
        return new Finished(started, exitCode);
    }

    public static DistBuildStarted distBuildStarted() {
        return new DistBuildStarted();
    }

    public static DistBuildFinished distBuildFinished(DistBuildStarted started, int exitCode) {
        return new DistBuildFinished(started, exitCode);
    }

    public static RuleCountCalculated ruleCountCalculated(ImmutableSet<BuildTarget> buildTargets, int ruleCount) {
        return new RuleCountCalculated(buildTargets, ruleCount);
    }

    public static UnskippedRuleCountUpdated unskippedRuleCountUpdated(int ruleCount) {
        return new UnskippedRuleCountUpdated(ruleCount);
    }

    public static BuildReport buildReport(String buildReportJson) {
        return new BuildReport(buildReportJson);
    }

    public static class Started extends BuildEvent {

        private final ImmutableSet<String> buildArgs;

        protected Started(ImmutableSet<String> buildArgs) {
            super(EventKey.unique());
            this.buildArgs = buildArgs;
        }

        @Override
        public String getEventName() {
            return BUILD_STARTED;
        }

        @Override
        protected String getValueString() {
            return Joiner.on(", ").join(buildArgs);
        }

        public ImmutableSet<String> getBuildArgs() {
            return buildArgs;
        }
    }

    /** Event used to mark a fresh start for build completion estimation. */
    public static class Reset extends BuildEvent {
        protected Reset() {
            super(EventKey.unique());
        }

        @Override
        public String getEventName() {
            return BUILD_RESET;
        }

        @Override
        protected String getValueString() {
            return "";
        }
    }

    public static class Finished extends BuildEvent {

        private final ImmutableSet<String> buildArgs;
        private final ExitCode exitCode;

        protected Finished(Started started, ExitCode exitCode) {
            super(started.getEventKey());
            this.buildArgs = started.getBuildArgs();
            this.exitCode = exitCode;
        }

        public ImmutableSet<String> getBuildArgs() {
            return buildArgs;
        }

        public ExitCode getExitCode() {
            return exitCode;
        }

        @Override
        public String getEventName() {
            return BUILD_FINISHED;
        }

        @Override
        protected String getValueString() {
            return String.format("exit code: %d", exitCode.getCode());
        }

        @Override
        public boolean equals(Object o) {
            if (!super.equals(o)) {
                return false;
            }
            // Because super.equals compares the EventKey, getting here means that we've somehow managed
            // to create 2 Finished events for the same Started event.
            throw new UnsupportedOperationException("Multiple conflicting Finished events detected.");
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(super.hashCode(), buildArgs, exitCode);
        }
    }

    public static class DistBuildStarted extends BuildEvent {

        protected DistBuildStarted() {
            super(EventKey.unique());
        }

        @Override
        public String getEventName() {
            return DIST_BUILD_STARTED;
        }

        @Override
        protected String getValueString() {
            return "";
        }
    }

    public static class DistBuildFinished extends BuildEvent {

        private final int exitCode;

        protected DistBuildFinished(DistBuildStarted started, int exitCode) {
            super(started.getEventKey());
            this.exitCode = exitCode;
        }

        public int getExitCode() {
            return exitCode;
        }

        @Override
        public String getEventName() {
            return DIST_BUILD_FINISHED;
        }

        @Override
        protected String getValueString() {
            return String.format("exit code: %d", exitCode);
        }

        @Override
        public boolean equals(Object o) {
            if (!super.equals(o)) {
                return false;
            }
            // Because super.equals compares the EventKey, getting here means that we've somehow managed
            // to create 2 Finished events for the same Started event.
            throw new UnsupportedOperationException("Multiple conflicting Finished events detected.");
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(super.hashCode(), exitCode);
        }
    }

    public static class RuleCountCalculated extends BuildEvent {

        private final ImmutableSet<BuildTarget> buildRules;
        private final int numRules;

        protected RuleCountCalculated(ImmutableSet<BuildTarget> buildRules, int numRulesToBuild) {
            super(EventKey.unique());
            this.buildRules = buildRules;
            this.numRules = numRulesToBuild;
        }

        public ImmutableSet<BuildTarget> getBuildRules() {
            return buildRules;
        }

        public int getNumRules() {
            return numRules;
        }

        @Override
        public String getEventName() {
            return "RuleCountCalculated";
        }

        @Override
        protected String getValueString() {
            return Joiner.on(", ").join(buildRules);
        }

        @Override
        public boolean equals(Object o) {
            if (!super.equals(o)) {
                return false;
            }
            // Because super.equals compares the EventKey, getting here means that we've somehow managed
            // to create 2 Finished events for the same Started event.
            throw new UnsupportedOperationException("Multiple conflicting Finished events detected.");
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(super.hashCode(), buildRules, numRules);
        }
    }

    public static class UnskippedRuleCountUpdated extends BuildEvent {

        private final int numRules;

        protected UnskippedRuleCountUpdated(int numRulesToBuild) {
            super(EventKey.unique());
            this.numRules = numRulesToBuild;
        }

        public int getNumRules() {
            return numRules;
        }

        @Override
        public String getEventName() {
            return "UnskippedRuleCountUpdated";
        }

        @Override
        protected String getValueString() {
            return Integer.toString(numRules);
        }

        @Override
        public boolean equals(Object o) {
            return this == o;
        }

        @Override
        public int hashCode() {
            return System.identityHashCode(this);
        }
    }

    /** Event used to post build reports */
    public static class BuildReport extends BuildEvent {

        private final String buildReportJson;

        protected BuildReport(String buildReportJson) {
            super(EventKey.unique());
            this.buildReportJson = buildReportJson;
        }

        public String getBuildReport() {
            return buildReportJson;
        }

        @Override
        public String getEventName() {
            return BUILD_REPORT;
        }

        @Override
        protected String getValueString() {
            return buildReportJson;
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof BuildReport) {
                return this.getBuildReport().equals(((BuildReport) o).getBuildReport());
            }
            return false;
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(super.hashCode(), buildReportJson);
        }
    }
}