-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageParser.java
More file actions
122 lines (112 loc) · 3.86 KB
/
MessageParser.java
File metadata and controls
122 lines (112 loc) · 3.86 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.*;
/**
* Utility class for creating and parsing messages for the collaborative sketch application.
*
* This class provides static methods to generate properly formatted messages for adding, moving,
* recoloring, and deleting shapes in the collaborative drawing application. It also includes a
* method to parse an incoming message string into tokens for further processing.
*
* Example usage:
* <pre>
* String addMsg = MessageParser.createAddMessage("rectangle", 100, 100, 200, 200, 16711680);
* String[] tokens = MessageParser.parseMessage(addMsg);
* </pre>
*
* @author
* Triumph Kia Teh | CS10 | Winter 2025
*/
public class MessageParser {
/**
* Creates an "add" message.
*
* The message format is:
* "add <shapeType> <x1> <y1> <x2> <y2> <rgb>"
*
* @param shapeType the type of shape (e.g., "rectangle", "ellipse", "polyline", "segment")
* @param x1 first x-coordinate
* @param y1 first y-coordinate
* @param x2 second x-coordinate
* @param y2 second y-coordinate
* @param rgb integer representation of the color
* @return the formatted "add" message string
*/
public static String createAddMessage(String shapeType, int x1, int y1, int x2, int y2, int rgb) {
return "add " + shapeType + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + rgb;
}
/**
* Creates a "move" message.
*
* The message format is:
* "move <id> <dx> <dy>"
*
* @param id the shape id
* @param dx change in x
* @param dy change in y
* @return the formatted "move" message string
*/
public static String createMoveMessage(int id, int dx, int dy) {
return "move " + id + " " + dx + " " + dy;
}
/**
* Creates a "recolor" message.
*
* The message format is:
* "recolor <id> <rgb>"
*
* @param id the shape id
* @param rgb the new color as an integer
* @return the formatted "recolor" message string
*/
public static String createRecolorMessage(int id, int rgb) {
return "recolor " + id + " " + rgb;
}
/**
* Creates a "delete" message.
*
* The message format is:
* "delete <id>"
*
* @param id the shape id to delete
* @return the formatted "delete" message string
*/
public static String createDeleteMessage(int id) {
return "delete " + id;
}
/**
* Parses a message into tokens.
*
* This method splits the given message string by whitespace.
* For example, the message "move 5 10 -5" would be split into:
* ["move", "5", "10", "-5"]
*
* @param msg the incoming message string
* @return an array of tokens parsed from the message
*/
public static String[] parseMessage(String msg) {
return msg.trim().split("\\s+");
}
/**
* Main method for simple testing of MessageParser functionality.
* It demonstrates the creation and parsing of "add", "move", "recolor", and "delete" messages.
*
* @param args command line arguments (not used)
*/
public static void main(String[] args) {
// Test "add" message creation and parsing.
String addMsg = createAddMessage("rectangle", 100, 100, 200, 200, 16711680);
System.out.println("Add Message: " + addMsg);
String[] tokens = parseMessage(addMsg);
for (String token : tokens) {
System.out.println("Token: " + token);
}
// Test "move" message.
String moveMsg = createMoveMessage(5, 10, -5);
System.out.println("Move Message: " + moveMsg);
// Test "recolor" message.
String recolorMsg = createRecolorMessage(3, 255);
System.out.println("Recolor Message: " + recolorMsg);
// Test "delete" message.
String deleteMsg = createDeleteMessage(2);
System.out.println("Delete Message: " + deleteMsg);
}
}