-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.java
123 lines (106 loc) · 2.32 KB
/
Cell.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
/**
* Generic helper class to control synchronized operations on a Field.
*/
public class Cell<T> implements Comparable<Cell<T>>
{
/**
* Creates a new Cell.
*
* @param row
* the row of the Cell on a Field.
* @param col
* the row of the Cell on a Field.
*/
public Cell(int row, int col, T occupant)
{
setRow(row);
setCol(col);
setOccupant(occupant);
}
/**
* Sets the col of the Cell.
*
* @param col
* the col to set for the Cell.
*/
public void setCol(int col)
{
p_col = col;
}
/**
* Returns the col of the Cell.
*
* @return the col of the Cell.
*/
public int getCol()
{
return p_col;
}
/**
* Returns the row of the Cell.
*
* @return the row of the Cell.
*/
public int getRow()
{
return p_row;
}
/**
* Sets the row of the Cell.
*
* @param row
* the row to set for the Cell.
*/
public void setRow(int row)
{
p_row = row;
}
/**
* Gets the FieldOccupant of this Cell.
*
* @return the FieldOccupant of this Cell.
*/
public T getOccupant()
{
return p_occupant;
}
/**
* Sets the FieldOccupant of this Cell.
*
* @param occupant
* the occupant of this Cell.
*/
public void setOccupant(T occupant)
{
p_occupant = occupant;
Simulation.DISPLAY_FIELD.getAndSet(true);
}
/**
* Returns a String representation of this Cell.
*
* @return a String representation of this Cell.
*/
@Override
public String toString()
{
return getOccupant() == null ? " " : getOccupant().toString();
}
/**
* Returns a comparison of this Cell to the Cell passed as a parameter.
*
* @param o
* the Cell to compare this Cell to.
* @return positive if this Cell has a greater value than the Cell passed as
* an parameter, 0 if they are equal, negative otherwise.
*/
@Override
public int compareTo(Cell<T> o)
{
return getRow() - o.getRow() == 0 ? getCol() - o.getCol()
: getRow() - o.getRow();
}
// A Cell is located at coordinates and can hold an occupant.
private int p_row;
private int p_col;
private T p_occupant;
} // Cell