001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.api.records.timeline;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlRootElement;
028
029import org.apache.hadoop.classification.InterfaceAudience.Private;
030import org.apache.hadoop.classification.InterfaceAudience.Public;
031import org.apache.hadoop.classification.InterfaceStability.Unstable;
032
033/**
034 * The class that contains the information of an event that is related to some
035 * conceptual entity of an application. Users are free to define what the event
036 * means, such as starting an application, getting allocated a container and
037 * etc.
038 */
039@XmlRootElement(name = "event")
040@XmlAccessorType(XmlAccessType.NONE)
041@Public
042@Unstable
043public class TimelineEvent implements Comparable<TimelineEvent> {
044
045  private long timestamp;
046  private String eventType;
047  private HashMap<String, Object> eventInfo = new HashMap<String, Object>();
048
049  public TimelineEvent() {
050  }
051
052  /**
053   * Get the timestamp of the event
054   * 
055   * @return the timestamp of the event
056   */
057  @XmlElement(name = "timestamp")
058  public long getTimestamp() {
059    return timestamp;
060  }
061
062  /**
063   * Set the timestamp of the event
064   * 
065   * @param timestamp
066   *          the timestamp of the event
067   */
068  public void setTimestamp(long timestamp) {
069    this.timestamp = timestamp;
070  }
071
072  /**
073   * Get the event type
074   * 
075   * @return the event type
076   */
077  @XmlElement(name = "eventtype")
078  public String getEventType() {
079    return eventType;
080  }
081
082  /**
083   * Set the event type
084   * 
085   * @param eventType
086   *          the event type
087   */
088  public void setEventType(String eventType) {
089    this.eventType = eventType;
090  }
091
092  /**
093   * Set the information of the event
094   * 
095   * @return the information of the event
096   */
097  public Map<String, Object> getEventInfo() {
098    return eventInfo;
099  }
100
101  // Required by JAXB
102  @Private
103  @XmlElement(name = "eventinfo")
104  public HashMap<String, Object> getEventInfoJAXB() {
105    return eventInfo;
106  }
107
108  /**
109   * Add one piece of the information of the event to the existing information
110   * map
111   * 
112   * @param key
113   *          the information key
114   * @param value
115   *          the information value
116   */
117  public void addEventInfo(String key, Object value) {
118    this.eventInfo.put(key, value);
119  }
120
121  /**
122   * Add a map of the information of the event to the existing information map
123   * 
124   * @param eventInfo
125   *          a map of of the information of the event
126   */
127  public void addEventInfo(Map<String, Object> eventInfo) {
128    this.eventInfo.putAll(eventInfo);
129  }
130
131  /**
132   * Set the information map to the given map of the information of the event
133   * 
134   * @param eventInfo
135   *          a map of of the information of the event
136   */
137  public void setEventInfo(Map<String, Object> eventInfo) {
138    if (eventInfo != null && !(eventInfo instanceof HashMap)) {
139      this.eventInfo = new HashMap<String, Object>(eventInfo);
140    } else {
141      this.eventInfo = (HashMap<String, Object>) eventInfo;
142    }
143  }
144
145  @Override
146  public int compareTo(TimelineEvent other) {
147    if (timestamp > other.timestamp) {
148      return -1;
149    } else if (timestamp < other.timestamp) {
150      return 1;
151    } else {
152      return eventType.compareTo(other.eventType);
153    }
154  }
155
156  @Override
157  public boolean equals(Object o) {
158    if (this == o)
159      return true;
160    if (o == null || getClass() != o.getClass())
161      return false;
162
163    TimelineEvent event = (TimelineEvent) o;
164
165    if (timestamp != event.timestamp)
166      return false;
167    if (!eventType.equals(event.eventType))
168      return false;
169    if (eventInfo != null ? !eventInfo.equals(event.eventInfo) :
170        event.eventInfo != null)
171      return false;
172
173    return true;
174  }
175
176  @Override
177  public int hashCode() {
178    int result = (int) (timestamp ^ (timestamp >>> 32));
179    result = 31 * result + eventType.hashCode();
180    result = 31 * result + (eventInfo != null ? eventInfo.hashCode() : 0);
181    return result;
182  }
183}