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

com.automationrockstars.gunter.events.impl.AbstractTestEvent Maven / Gradle / Ivy

The newest version!
/*
 * 
 */
package com.automationrockstars.gunter.events.impl;

import com.automationrockstars.gunter.EventType;
import com.automationrockstars.gunter.IdUtils;
import com.automationrockstars.gunter.events.Event;
import com.automationrockstars.gunter.events.EventFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.util.Collections;
import java.util.Date;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class AbstractTestEvent implements Event {

    private String id;
    private String parentId;
    private Date timeStamp;
    private Map attributes = Maps.newHashMap();

    public AbstractTestEvent(String parentId) {
        init(parentId);
    }

    public AbstractTestEvent() {
        init(null);
    }

    /* (non-Javadoc)
     * @see com.automationrockstars.gunter.TestEventA#getType()
     */
    public abstract EventType getType();

    private void init(String parent) {
        timeStamp = new Date();
        this.parentId = parent;
        if (Strings.isNullOrEmpty(parentId)) {
            this.id = IdUtils.id(getType());
        } else {
            this.id = IdUtils.id(parentId, getType());
        }
    }

    /* (non-Javadoc)
     * @see com.automationrockstars.gunter.TestEventA#getId()
     */
    public String getId() {
        return id;
    }

    void setId(String id) {
        this.id = id;
    }

    /* (non-Javadoc)
     * @see com.automationrockstars.gunter.TestEventA#getParentId()
     */
    public String getParentId() {
        return parentId;
    }

    void setParentId(String parentId) {
        this.parentId = parentId;
    }

    /* (non-Javadoc)
     * @see com.automationrockstars.gunter.TestEventA#getTimeStamp()
     */
    public Date getTimeStamp() {
        return timeStamp;
    }

    public Map attributes() {
        return attributes;
    }

    public String toString() {
        return EventFactory.toJson(this);
    }

    @JsonIgnore
    public  T getAttribute(String name) {
        return (T) attributes().get(name);
    }

    @JsonIgnore
    public void setAttribute(String name, String value) {
        attributes().put(name, value);
    }

    @Override
    public boolean equals(Object obj) {
        if (!Event.class.isAssignableFrom(obj.getClass()))
            return false;
        return this.id.equals(((Event) obj).getId());
    }

    @SuppressWarnings("unchecked")
    public  T as(Class clazz) {
        return (T) this;

    }

    ;

    public Map getAttributes() {
        return Collections.unmodifiableMap(attributes);
    }

    public void setAttributes(Map attributes) {
        this.attributes = attributes;
    }

    public void setAttribute(String attribute, Object value) {
        attributes.put(attribute, value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy