1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 package org.apache.harmony.jpda.tests.framework.jdwp;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * This class allows to build an {@link Event} with multiple modifiers.
26  */
27 public class EventBuilder {
28     private final byte eventKind;
29     private final byte suspendPolicy;
30     private final List<EventMod> modifiers = new ArrayList<EventMod>();
31 
EventBuilder(byte eventKind, byte suspendPolicy)32     public EventBuilder(byte eventKind, byte suspendPolicy) {
33         this.eventKind = eventKind;
34         this.suspendPolicy = suspendPolicy;
35     }
36 
37     /**
38      * Sets the Count modifier.
39      *
40      * @param count the count before event
41      * @return a reference to this object
42      */
setCount(int count)43     public EventBuilder setCount(int count) {
44         EventMod mod = createModifier(EventMod.ModKind.Count);
45         mod.count = count;
46         modifiers.add(mod);
47         return this;
48     }
49 
50     /**
51      * Sets the ThreadOnly modifier.
52      *
53      * @param thread the ID of the required thread object
54      * @return a reference to this object
55      */
setThreadOnly(long thread)56     public EventBuilder setThreadOnly(long thread) {
57         EventMod mod = createModifier(EventMod.ModKind.ThreadOnly);
58         mod.thread = thread;
59         modifiers.add(mod);
60         return this;
61     }
62 
63     /**
64      * Sets the ClassMatch modifier.
65      *
66      * @param pattern the required class pattern
67      * @return a reference to this object
68      */
setClassMatch(String pattern)69     public EventBuilder setClassMatch(String pattern) {
70         EventMod mod = createModifier(EventMod.ModKind.ClassMatch);
71         mod.classPattern = pattern;
72         modifiers.add(mod);
73         return this;
74     }
75 
76     /**
77      * Sets the LocationOnly modifier.
78      *
79      * @param location the required location
80      * @return a reference to this object
81      */
setLocationOnly(Location location)82     public EventBuilder setLocationOnly(Location location) {
83         EventMod mod = createModifier(EventMod.ModKind.LocationOnly);
84         mod.loc = location;
85         modifiers.add(mod);
86         return this;
87     }
88 
89     /**
90      * Sets the ExceptionOnly modifier.
91      *
92      * @param exceptionClassID the reference type ID of the exception class
93      * @param caught true to report caught exception, false otherwise.
94      * @param uncaught true to report uncaught exception, false otherwise
95      * @return a reference to this object
96      */
setExceptionOnly(long exceptionClassID, boolean caught, boolean uncaught)97     public EventBuilder setExceptionOnly(long exceptionClassID, boolean caught,
98             boolean uncaught) {
99         EventMod mod = createModifier(EventMod.ModKind.ExceptionOnly);
100         mod.caught = caught;
101         mod.uncaught = uncaught;
102         mod.exceptionOrNull = exceptionClassID;
103         modifiers.add(mod);
104         return this;
105     }
106 
107     /**
108      * Sets the FieldOnly modifier.
109      *
110      * @param typeID the reference type ID of the field's declaring class.
111      * @param fieldID the required field ID
112      * @return a reference to this object
113      */
setFieldOnly(long typeID, long fieldID)114     public EventBuilder setFieldOnly(long typeID, long fieldID) {
115         EventMod mod = createModifier(EventMod.ModKind.FieldOnly);
116         mod.declaring = typeID;
117         mod.fieldID = fieldID;
118         modifiers.add(mod);
119         return this;
120     }
121 
122     /**
123      * Sets the InstanceOnly modifier.
124      *
125      * @param instance the required object ID
126      * @return a reference to this object
127      */
setInstanceOnly(long instance)128     public EventBuilder setInstanceOnly(long instance) {
129         EventMod mod = createModifier(EventMod.ModKind.InstanceOnly);
130         mod.instance = instance;
131         modifiers.add(mod);
132         return this;
133     }
134 
135     /**
136      * Builds the event with all added modifiers.
137      *
138      * @return an {@link Event}
139      */
build()140     public Event build() {
141         EventMod[] mods = new EventMod[modifiers.size()];
142         mods = modifiers.toArray(mods);
143         return new Event(eventKind, suspendPolicy, mods);
144     }
145 
createModifier(byte modifierKind)146     private static EventMod createModifier(byte modifierKind) {
147         EventMod mod = new EventMod();
148         mod.modKind = modifierKind;
149         return mod;
150     }
151 }
152