Repository navigation
Expand file tree
/
Copy pathGate.java
More file actions
66 lines (56 loc) · 1.76 KB
/
Copy pathGate.java
File metadata and controls
66 lines (56 loc) · 1.76 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
package Codes;
public class Gate {
private final int gateNumber;
private Plane occupyingPlane;
private boolean isOccupied;
public Gate(int gateNumber) {
this.gateNumber = gateNumber;
this.occupyingPlane = null;
this.isOccupied = false;
}
//Occupy this gate
//@param plane Operating plane
//@return Whether occupation was successful
public synchronized boolean occupy(Plane plane) {
if (!isOccupied) {
this.occupyingPlane = plane;
this.isOccupied = true;
return true;
}
return false;
}
//Release this gate
// @param plane Plane to release (for verification)
// @return Whether release was successful
public synchronized boolean release(Plane plane) {
if (isOccupied && this.occupyingPlane == plane) {
this.occupyingPlane = null;
this.isOccupied = false;
return true;
}
return false;
}
// Check if available
public synchronized boolean isAvailable() {
return !isOccupied;
}
// Get gate number
public int getGateNumber() {
return gateNumber;
}
//Get occupying plane
public synchronized Plane getOccupyingPlane() {
return occupyingPlane;
}
@Override
public String toString() {
synchronized (this) {
if (isOccupied && occupyingPlane != null) {
return String.format("Gate-%d [Occupied by Plane-%d]",
gateNumber, occupyingPlane.getPlaneId());
} else {
return String.format("Gate-%d [Available]", gateNumber);
}
}
}
}