src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordedEvent.java
branchJEP-349-branch
changeset 57452 6fabe73e5d9a
parent 57376 8e8a06a3059c
child 57460 bcbc53560c77
equal deleted inserted replaced
57449:099789ceff7d 57452:6fabe73e5d9a
    38  *
    38  *
    39  * @since 9
    39  * @since 9
    40  */
    40  */
    41 public final class RecordedEvent extends RecordedObject {
    41 public final class RecordedEvent extends RecordedObject {
    42     private final EventType eventType;
    42     private final EventType eventType;
    43     long startTime;
    43     long startTimeTicks;
    44     long endTime;
    44     long endTimeTicks;
    45 
    45 
    46     // package private
    46     // package private
    47     RecordedEvent(EventType type, List<ValueDescriptor> vds, Object[] values, long startTime, long endTime, TimeConverter timeConverter) {
    47     RecordedEvent(EventType type, List<ValueDescriptor> vds, Object[] values, long startTimeTicks, long endTimeTicks, TimeConverter timeConverter) {
    48         super(vds, values, timeConverter);
    48         super(vds, values, timeConverter);
    49         this.eventType = type;
    49         this.eventType = type;
    50         this.startTime = startTime;
    50         this.startTimeTicks = startTimeTicks;
    51         this.endTime = endTime;
    51         this.endTimeTicks = endTimeTicks;
    52     }
    52     }
    53 
    53 
    54     /**
    54     /**
    55      * Returns the stack trace that was created when the event was committed, or
    55      * Returns the stack trace that was created when the event was committed, or
    56      * {@code null} if the event lacks a stack trace.
    56      * {@code null} if the event lacks a stack trace.
    86      * If the event is an instant event, then the start time and end time are the same.
    86      * If the event is an instant event, then the start time and end time are the same.
    87      *
    87      *
    88      * @return the start time, not {@code null}
    88      * @return the start time, not {@code null}
    89      */
    89      */
    90     public Instant getStartTime() {
    90     public Instant getStartTime() {
    91         return Instant.ofEpochSecond(0, startTime);
    91         return Instant.ofEpochSecond(0, getStartTimeNanos());
    92     }
    92     }
    93 
    93 
    94     /**
    94     /**
    95      * Returns the end time of the event.
    95      * Returns the end time of the event.
    96      * <p>
    96      * <p>
    97      * If the event is an instant event, then the start time and end time are the same.
    97      * If the event is an instant event, then the start time and end time are the same.
    98      *
    98      *
    99      * @return the end time, not {@code null}
    99      * @return the end time, not {@code null}
   100      */
   100      */
   101     public Instant getEndTime() {
   101     public Instant getEndTime() {
   102         return Instant.ofEpochSecond(0, endTime);
   102         return Instant.ofEpochSecond(0, getEndTimeNanos());
   103     }
   103     }
   104 
   104 
   105     /**
   105     /**
   106      * Returns the duration of the event, measured in nanoseconds.
   106      * Returns the duration of the event, measured in nanoseconds.
   107      *
   107      *
   108      * @return the duration in nanoseconds, not {@code null}
   108      * @return the duration in nanoseconds, not {@code null}
   109      */
   109      */
   110     public Duration getDuration() {
   110     public Duration getDuration() {
   111         return Duration.ofNanos(endTime - startTime);
   111         return Duration.ofNanos(getEndTimeNanos() - getStartTimeNanos());
   112     }
   112     }
   113 
   113 
   114     /**
   114     /**
   115      * Returns the list of descriptors that describes the fields of the event.
   115      * Returns the list of descriptors that describes the fields of the event.
   116      *
   116      *
   118      */
   118      */
   119     @Override
   119     @Override
   120     public List<ValueDescriptor> getFields() {
   120     public List<ValueDescriptor> getFields() {
   121         return getEventType().getFields();
   121         return getEventType().getFields();
   122     }
   122     }
       
   123 
       
   124     protected final Object objectAt(int index) {
       
   125         if (index == 0) {
       
   126             return startTimeTicks;
       
   127         }
       
   128         if (hasDuration()) {
       
   129             if (index == 1) {
       
   130                 return endTimeTicks - startTimeTicks;
       
   131             }
       
   132             return objects[index - 2];
       
   133         }
       
   134         return objects[index - 1];
       
   135     }
       
   136 
       
   137     private boolean hasDuration() {
       
   138         return objects.length + 2 == descriptors.size();
       
   139     }
       
   140 
       
   141     private long getStartTimeNanos() {
       
   142         return timeConverter.convertTimestamp(startTimeTicks);
       
   143     }
       
   144 
       
   145     private long getEndTimeNanos() {
       
   146         return timeConverter.convertTimestamp(endTimeTicks);
       
   147     }
   123 }
   148 }