-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
142 lines (115 loc) · 3.52 KB
/
Server.java
File metadata and controls
142 lines (115 loc) · 3.52 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
132
133
134
135
136
137
138
139
140
141
package cs2030.simulator;
import java.util.Optional;
import java.util.function.Supplier;
import cs2030.util.ImList;
class Server {
private static final int DEFAULT_MAXQ = 1;
private static final double ZERO = 0.0;
private final int id;
private final double waitUntil;
private final Optional<Customer> currCustomer;
private final ImList<Customer> queue;
private final int maxQ;
private final Supplier<Double> restTime;
private final boolean idle;
Server(int id) {
this(id, DEFAULT_MAXQ);
}
Server(int id, int maxQ) {
this(id, ZERO, Optional.empty(), ImList.<Customer>of(), maxQ);
}
Server(int id, int maxQ, ImList<Customer> queue) {
this(id, ZERO, Optional.empty(), queue, maxQ);
}
Server(int id, int maxQ, Supplier<Double> restTime) {
this(id, ZERO, Optional.empty(), ImList.<Customer>of(), maxQ, restTime);
}
Server(int id, double waitUntil, Optional<Customer> currCustomer, ImList<Customer> queue,
int maxQ) {
this(id, waitUntil, currCustomer, queue, maxQ, () -> ZERO);
}
Server(int id, double waitUntil, Optional<Customer> currCustomer, ImList<Customer> queue,
int maxQ, Supplier<Double> restTime) {
this(id, waitUntil, currCustomer, queue, maxQ, restTime, true);
}
Server(int id, double waitUntil, Optional<Customer> currCustomer, ImList<Customer> queue,
int maxQ, Supplier<Double> restTime, boolean idle) {
this.id = id;
this.waitUntil = waitUntil;
this.currCustomer = currCustomer;
this.queue = queue;
this.maxQ = maxQ;
this.restTime = restTime;
this.idle = idle;
}
Server serve(Customer customer, double time) {
return new Server(this.id,
time,
Optional.<Customer>of(customer),
(this.queue.size() != 0) ? this.queue.remove(0).second() : this.queue,
this.maxQ,
this.restTime,
false);
}
Server queue(Customer customer) {
return new Server(this.id,
this.waitUntil,
this.currCustomer,
this.queue.add(customer),
this.maxQ,
this.restTime,
false);
}
Server done(Customer customer) {
return new Server(this.id,
(hasQ()) ? this.waitUntil : ZERO,
Optional.empty(),
this.queue,
this.maxQ,
this.restTime,
true);
}
Server maybeRest() {
return new Server(this.id,
this.waitUntil + this.restTime.get(),
Optional.empty(),
this.queue,
this.maxQ,
this.restTime,
false);
}
int getID() {
return this.id;
}
int getMaxQ() {
return this.maxQ;
}
Optional<Customer> getCurrCustomer() {
return this.currCustomer;
}
ImList<Customer> getQueue() {
return this.queue;
}
Supplier<Double> getRestTime() {
return this.restTime;
}
double getWaitUntil() {
return this.waitUntil;
}
boolean isIdle() {
return this.idle;
}
boolean canQ() {
return this.queue.size() < this.maxQ;
}
boolean hasQ() {
return this.queue.size() >= 1;
}
boolean isSelfCheck() {
return false;
}
@Override
public String toString() {
return String.format("%d", id);
}
}