Skip to main content
summaryrefslogtreecommitdiffstats
blob: 034c33b5bfb8c2049bed9e8cc754d654096ff9cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.jsdt.debug.internal.chrome.transport;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.wst.jsdt.debug.transport.packet.Event;

/**
 * Event packet
 * 
 * @since 1.0
 */
public class EventPacketImpl extends PacketImpl implements Event {

	/**
	 * The type of this packet
	 */
	public static final String EVENT = "event"; //$NON-NLS-1$
	
	private String event = null;
	protected Number result = null;
	protected Map body = null;
	
	/**
	 * Constructor
	 * 
	 * @param event
	 * @param tool the name of the tools service that issued this event
	 */
	public EventPacketImpl(String event, String tool) {
		super(EVENT, tool);
		if(event == null) {
			throw new IllegalArgumentException(Messages.cannot_create_packet_with_no_event);
		}
		this.event = event;
	}

	/**
	 * Constructor
	 * 
	 * @param json
	 */
	public EventPacketImpl(Map json) {
		super(json);
		this.event = (String) json.get(EVENT);
		if(event == null) {
			throw new IllegalArgumentException(Messages.no_event_found_in_json);
		}
		this.result = (Number) json.get(Attributes.RESULT);
		Object data = json.get(Attributes.DATA);
		if(data != null) {
			this.body = new HashMap();
			this.body.put(Attributes.DATA, data);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.transport.packet.Event#getEvent()
	 */
	public String getEvent() {
		return event;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.transport.packet.Event#getBody()
	 */
	public Map getBody() {
		if(this.body == null) {
			return Collections.EMPTY_MAP;
		}
		return this.body;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.internal.chrome.transport.PacketImpl#toJSON()
	 */
	public Map toJSON() {
		Map json = super.toJSON();
		json.put(Attributes.COMMAND, event);
		json.put(Attributes.RESULT, this.result);
		if(this.body != null) {
			json.put(Attributes.DATA, this.body.get(Attributes.DATA));
		}
		return json;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		Object json = toJSON();
		buffer.append("EventPacketImpl: "); //$NON-NLS-1$
		JSON.writeValue(json, buffer);
		return buffer.toString();
	}
}

Back to the top