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