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
19package org.apache.harmony.jpda.tests.framework.jdwp;
20
21import java.util.ArrayList;
22import java.util.List;
23
24/**
25 * This class allows to build an {@link Event} with multiple modifiers.
26 */
27public class EventBuilder {
28    private final byte eventKind;
29    private final byte suspendPolicy;
30    private final List<EventMod> modifiers = new ArrayList<EventMod>();
31
32    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     */
43    public EventBuilder setCount(int count) {
44        EventMod mod = new EventMod(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     */
56    public EventBuilder setThreadOnly(long thread) {
57        EventMod mod = new EventMod(EventMod.ModKind.ThreadOnly);
58        mod.thread = thread;
59        modifiers.add(mod);
60        return this;
61    }
62
63    /**
64     * Sets the ClassOnly modifier.
65     *
66     * @param classId the required class ID
67     * @return a reference to this object
68     */
69    public EventBuilder setClassOnly(long classId) {
70        EventMod mod = new EventMod(EventMod.ModKind.ClassOnly);
71        mod.clazz = classId;
72        modifiers.add(mod);
73        return this;
74    }
75
76    /**
77     * Sets the ClassMatch modifier.
78     *
79     * @param pattern the required class pattern
80     * @return a reference to this object
81     */
82    public EventBuilder setClassMatch(String pattern) {
83        EventMod mod = new EventMod(EventMod.ModKind.ClassMatch);
84        mod.classPattern = pattern;
85        modifiers.add(mod);
86        return this;
87    }
88
89    /**
90     * Sets the ClassExclude modifier.
91     *
92     * @param pattern the required class pattern
93     * @return a reference to this object
94     */
95    public EventBuilder setClassExclude(String pattern) {
96        EventMod mod = new EventMod(EventMod.ModKind.ClassExclude);
97        mod.classPattern = pattern;
98        modifiers.add(mod);
99        return this;
100    }
101
102    /**
103     * Sets the LocationOnly modifier.
104     *
105     * @param location the required location
106     * @return a reference to this object
107     */
108    public EventBuilder setLocationOnly(Location location) {
109        EventMod mod = new EventMod(EventMod.ModKind.LocationOnly);
110        mod.loc = location;
111        modifiers.add(mod);
112        return this;
113    }
114
115    /**
116     * Sets the ExceptionOnly modifier.
117     *
118     * @param exceptionClassID the reference type ID of the exception class
119     * @param caught true to report caught exception, false otherwise.
120     * @param uncaught true to report uncaught exception, false otherwise
121     * @return a reference to this object
122     */
123    public EventBuilder setExceptionOnly(long exceptionClassID, boolean caught,
124            boolean uncaught) {
125        EventMod mod = new EventMod(EventMod.ModKind.ExceptionOnly);
126        mod.caught = caught;
127        mod.uncaught = uncaught;
128        mod.exceptionOrNull = exceptionClassID;
129        modifiers.add(mod);
130        return this;
131    }
132
133    /**
134     * Sets the FieldOnly modifier.
135     *
136     * @param typeID the reference type ID of the field's declaring class.
137     * @param fieldID the required field ID
138     * @return a reference to this object
139     */
140    public EventBuilder setFieldOnly(long typeID, long fieldID) {
141        EventMod mod = new EventMod(EventMod.ModKind.FieldOnly);
142        mod.declaring = typeID;
143        mod.fieldID = fieldID;
144        modifiers.add(mod);
145        return this;
146    }
147
148    /**
149     * Sets the Step modifier.
150     *
151     * @param threadID the thread where to step
152     * @param stepSize the size of the step
153     * @param stepDepth the depth of the step
154     * @return a reference to this object
155     */
156    public EventBuilder setStep(long threadID, int stepSize, int stepDepth) {
157        EventMod mod = new EventMod(EventMod.ModKind.Step);
158        mod.thread = threadID;
159        mod.size = stepSize;
160        mod.depth = stepDepth;
161        modifiers.add(mod);
162        return this;
163    }
164
165    /**
166     * Sets the InstanceOnly modifier.
167     *
168     * @param instance the required object ID
169     * @return a reference to this object
170     */
171    public EventBuilder setInstanceOnly(long instance) {
172        EventMod mod = new EventMod(EventMod.ModKind.InstanceOnly);
173        mod.instance = instance;
174        modifiers.add(mod);
175        return this;
176    }
177
178    /**
179     * Sets the SourceNameMatch modifier.
180     *
181     * @param sourceNamePattern the source name pattern to match
182     * @return a reference to this object
183     */
184    public EventBuilder setSourceNameMatch(String sourceNamePattern) {
185        EventMod mod = new EventMod(EventMod.ModKind.SourceNameMatch);
186        mod.sourceNamePattern = sourceNamePattern;
187        modifiers.add(mod);
188        return this;
189    }
190
191    /**
192     * Builds the event with all added modifiers.
193     *
194     * @return an {@link Event}
195     */
196    public Event build() {
197        return new Event(eventKind, suspendPolicy, modifiers);
198    }
199}
200