org.opentestsystem.delivery.logging.EventInfo.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.logging.EventInfo.java

Source

/***************************************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2017 Regents of the University of California
 *
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 *
 * SmarterApp Open Source Assessment Software Project: http://smarterapp.org
 * Developed by Fairway Technologies, Inc. (http://fairwaytech.com)
 * for the Smarter Balanced Assessment Consortium (http://smarterbalanced.org)
 **************************************************************************************************/

package org.opentestsystem.delivery.logging;

import com.google.common.base.Optional;
import org.opentestsystem.delivery.logging.EventLogger.EventData;

import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.lang.StringUtils.trimToEmpty;

/**
 * Event to be logged to central logging
 * Used to simplify the number of parameters sent to the logger
 */
public class EventInfo {
    private String event;
    private String checkpoint;
    private String message;
    private final Map<EventData, Object> data = new HashMap<>();

    /**
     * @return Name of the current event
     */
    public String event() {
        return event;
    }

    void setEvent(final String event) {
        this.event = event;
    }

    /**
     * @return Moment in time of the event, such as the entry, exit or error
     */
    public Optional<String> checkpoint() {
        return Optional.fromNullable(checkpoint);
    }

    void setCheckpoint(final String checkpoint) {
        this.checkpoint = checkpoint;
    }

    /**
     * @return Optional message for the event
     */
    public Optional<String> message() {
        return Optional.fromNullable(message);
    }

    void setMessage(final String message) {
        this.message = message;
    }

    /**
     * @return Event data payload that is used to access the event from log files
     */
    public Map<EventData, Object> data() {
        return data;
    }

    void setData(final Map<EventData, Object> data) {
        this.data.putAll(data);
    }

    /**
     * Create a builder containing this EventInfo's values.
     *
     * @return A builder
     */
    public EventInfoBuilder toBuilder() {
        return builder().copy(this);
    }

    /**
     * Create a new empty builder.
     *
     * @return A builder
     */
    public static EventInfoBuilder builder() {
        return new EventInfoBuilder();
    }

    /**
     * Copy of this EventInfo with checkpoint changed
     *
     * @param checkpoint new value of checkpoint
     * @return Copy of this EventInfo with a new checkpoint
     */
    public EventInfo withCheckpoint(final String checkpoint) {
        return toBuilder().checkpoint(checkpoint).build();
    }

    /**
     * Copy of this EventInfo with message changed
     *
     * @param message new value of message
     * @return Copy of this EventInfo with a new message
     */
    public EventInfo withMessage(final String message) {
        return toBuilder().message(message).build();
    }

    public static class EventInfoBuilder {
        private String event;
        private String checkpoint;
        private String message;
        private final Map<EventData, Object> data = new HashMap<>();

        public EventInfoBuilder event(final String event) {
            this.event = trimToEmpty(event);
            return this;
        }

        public EventInfoBuilder checkpoint(final String checkpoint) {
            this.checkpoint = checkpoint;
            return this;
        }

        public EventInfoBuilder message(final String message) {
            this.message = message;
            return this;
        }

        public EventInfoBuilder data(final Map<EventData, Object> data) {
            this.data.clear();
            this.data.putAll(data);
            return this;
        }

        public EventInfoBuilder copy(final EventInfo eventInfo) {
            event(eventInfo.event());
            checkpoint(eventInfo.checkpoint().orNull());
            message(eventInfo.message().orNull());
            data(eventInfo.data());
            return this;
        }

        public EventInfo build() {
            final EventInfo eventInfo = new EventInfo();
            eventInfo.setEvent(trimToEmpty(event));
            eventInfo.setCheckpoint(checkpoint);
            eventInfo.setMessage(message);
            eventInfo.setData(data);
            return eventInfo;
        }
    }
}