-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.DesignanOnlineTextEditor.java
More file actions
131 lines (110 loc) · 4.14 KB
/
14.DesignanOnlineTextEditor.java
File metadata and controls
131 lines (110 loc) · 4.14 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
123
124
125
126
127
128
129
130
131
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.util.*;
// Command Pattern: Represents a reversible text operation
interface Command {
void execute(); // apply the operation
void undo(); // revert the operation
}
// Concrete Command implemented using Lambdas
class TextCommand implements Command {
private Runnable executeAction;
private Runnable undoAction;
// Constructor to bind actions
public TextCommand(Runnable executeAction, Runnable undoAction) {
this.executeAction = executeAction;
this.undoAction = undoAction;
}
// Execute command
public void execute() {
executeAction.run();
}
// Undo command
public void undo() {
undoAction.run();
}
}
// Main TextEditor class
public class OnlineTextEditor {
// Stores all user documents
private Map<String, Map<String, String>> documents;
// Undo and Redo stacks
private Deque<Command> undoStack;
private Deque<Command> redoStack;
public OnlineTextEditor() {
documents = new HashMap<>();
undoStack = new ArrayDeque<>();
redoStack = new ArrayDeque<>();
}
// Create a new document
public void createDoc(String user, String docName, String text) {
documents.putIfAbsent(user, new HashMap<>());
documents.get(user).put(docName, text);
System.out.println("Document '" + docName + "' created for " + user + ".");
}
// Edit a document and store command for undo/redo
public void editDoc(String user, String docName, String newText) {
String prevText = documents.get(user).get(docName);
// Define execute and undo using lambdas
Runnable execute = () -> documents.get(user).put(docName, newText);
Runnable undo = () -> documents.get(user).put(docName, prevText);
// Create command and execute
Command cmd = new TextCommand(execute, undo);
cmd.execute();
// Push to undo stack
undoStack.push(cmd);
// Clear redo stack as new edit resets redo history
redoStack.clear();
System.out.println("Edited '" + docName + "' for " + user + ".");
}
// Undo the last operation
public void undo() {
if (undoStack.isEmpty()) {
System.out.println("Nothing to undo.");
return;
}
Command cmd = undoStack.pop();
cmd.undo();
redoStack.push(cmd);
System.out.println("Undo operation performed.");
}
// Redo the last undone operation
public void redo() {
if (redoStack.isEmpty()) {
System.out.println("Nothing to redo.");
return;
}
Command cmd = redoStack.pop();
cmd.execute();
undoStack.push(cmd);
System.out.println("Redo operation performed.");
}
// Display current document text
public void showDoc(String user, String docName) {
System.out.println("Current Text of '" + docName + "': " + documents.get(user).get(docName));
}
// -----------------------
// DEMO MAIN
// -----------------------
public static void main(String[] args) {
OnlineTextEditor editor = new OnlineTextEditor();
editor.createDoc("user1", "doc1", "Hello");
editor.editDoc("user1", "doc1", "Hello World!");
editor.showDoc("user1", "doc1");
editor.undo();
editor.showDoc("user1", "doc1");
editor.redo();
editor.showDoc("user1", "doc1");
}
}