1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.events; 15 16 import org.yaml.snakeyaml.error.Mark; 17 18 /** 19 * Basic unit of output from a {@link org.yaml.snakeyaml.parser.Parser} or input of a 20 * {@link org.yaml.snakeyaml.emitter.Emitter}. 21 */ 22 public abstract class Event { 23 24 public enum ID { 25 Alias, Comment, DocumentEnd, DocumentStart, MappingEnd, MappingStart, Scalar, SequenceEnd, SequenceStart, StreamEnd, StreamStart 26 } 27 28 private final Mark startMark; 29 private final Mark endMark; 30 Event(Mark startMark, Mark endMark)31 public Event(Mark startMark, Mark endMark) { 32 this.startMark = startMark; 33 this.endMark = endMark; 34 } 35 toString()36 public String toString() { 37 return "<" + this.getClass().getName() + "(" + getArguments() + ")>"; 38 } 39 getStartMark()40 public Mark getStartMark() { 41 return startMark; 42 } 43 getEndMark()44 public Mark getEndMark() { 45 return endMark; 46 } 47 48 /** 49 * Generate human readable representation of the Event 50 * 51 * @see "__repr__ for Event in PyYAML" 52 * @return representation fore humans 53 */ getArguments()54 protected String getArguments() { 55 return ""; 56 } 57 58 /** 59 * Check if the Event is of the provided kind 60 * 61 * @param id - the Event.ID enum 62 * @return true then this Event of the provided type 63 */ is(Event.ID id)64 public boolean is(Event.ID id) { 65 return getEventId() == id; 66 } 67 68 /** 69 * Get the type (kind) if this Event 70 * 71 * @return the ID of this Event 72 */ getEventId()73 public abstract Event.ID getEventId(); 74 75 /* 76 * for tests only 77 */ 78 @Override equals(Object obj)79 public boolean equals(Object obj) { 80 if (obj instanceof Event) { 81 return toString().equals(obj.toString()); 82 } else { 83 return false; 84 } 85 } 86 87 /* 88 * for tests only 89 */ 90 @Override hashCode()91 public int hashCode() { 92 return toString().hashCode(); 93 } 94 } 95