-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
159 lines (127 loc) · 4.64 KB
/
Server.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cs2030.simulator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Optional;
class Server {
private final int serverId;
private final Queue<Customer> customerQueue;
private final int maxQueueLength;
private final Queue<Double> restTimes;
private final Optional<Customer> customerServiced;
private final double nextServiceTime;
protected Server(int serverId, int maxQueueLength,
Queue<Customer> customerQueue, Queue<Double> restTimes,
Optional<Customer> customerServiced,
double nextServiceTime) {
this.serverId = serverId;
this.maxQueueLength = maxQueueLength;
this.customerQueue = customerQueue;
this.restTimes = restTimes;
this.customerServiced = customerServiced;
this.nextServiceTime = nextServiceTime;
}
static Server createServer(int serverId, int maxQueueLength, Queue<Double> restTimes) {
return new Server(serverId, maxQueueLength, new LinkedList<Customer>(),
restTimes, Optional.<Customer>empty(), 0);
}
protected Server update(Queue<Customer> customerQueue,
Optional<Customer> customerServiced,
double nextServiceTime) {
return new Server(serverId, maxQueueLength,
customerQueue, restTimes,
customerServiced,
nextServiceTime);
}
int getServerId() {
return this.serverId;
}
protected Queue<Customer> getCustomerQueue() {
return this.customerQueue;
}
protected int getMaxQueueLength() {
return this.maxQueueLength;
}
protected Optional<Customer> getCustomerServiced() {
return this.customerServiced;
}
protected Queue<Double> getRestTimes() {
return this.restTimes;
}
protected double getNextServiceTime() {
return this.nextServiceTime;
}
Server queueCustomer(Customer customer, double currTime) {
if (canQueue(currTime, customer)) {
this.customerQueue.add(customer);
return update(this.customerQueue, this.customerServiced, this.nextServiceTime);
}
System.out.println("Unable to queue " + customer);
return this;
}
Server serveCustomer(Customer customer, double currTime) {
if (isNextInQueue(customer)) {
this.customerQueue.poll();
}
double nextServiceTime = calculateNextServiceTime(currTime, customer.getServiceTime());
return update(this.customerQueue, Optional.<Customer>of(customer), nextServiceTime);
}
private double calculateNextServiceTime(double currTime, double serveTime) {
return Math.max(currTime, this.nextServiceTime) + serveTime;
}
Server completeService(double currTime, Customer customer) {
if (isServing(customer)) {
double nextServiceTime = this.nextServiceTime + nextRestTime();
return update(this.customerQueue, Optional.<Customer>empty(), nextServiceTime);
}
return this;
}
private double nextRestTime() {
return this.restTimes.size() > 0 ? this.restTimes.poll() : 0;
}
boolean canServeNow(double time, Customer customer) {
return customerServiced
.map(c -> false)
.orElse(
time >= this.nextServiceTime &&
(getQueueSize() == 0 || isNextInQueue(customer))
);
}
boolean canQueue(double time, Customer customer) {
return !(isInQueue(customer) || isQueueFull());
}
boolean isQueueFull() {
return getQueueSize() >= this.maxQueueLength;
}
int getQueueSize() {
return this.customerQueue.size();
}
boolean isServing(Customer customer) {
return this.customerServiced.map(c -> c == customer).orElse(false);
}
boolean isNextInQueue(Customer customer) {
return this.customerQueue.peek() == customer;
}
boolean isInQueue(Customer c) {
return this.customerQueue.contains(c);
}
Optional<Customer> nextInQueue() {
return Optional.<Customer>ofNullable(this.customerQueue.peek());
}
double estimateServeTime(Customer customer) {
double estimatedTime = this.nextServiceTime;
if (isInQueue(customer)) {
for (Customer c : this.customerQueue) {
if (c == customer) {
break;
}
estimatedTime += c.getServiceTime();
}
}
return estimatedTime;
}
@Override
public String toString() {
return String.format("server %d", getServerId());
}
}