Skip to content

Commit cf91c0c

Browse files
bedaHovorkaclaude
andcommitted
Refactor anonymous classes to lambda expressions for improved readability
Modernize Java code by replacing verbose anonymous class implementations with concise lambda expressions: - InOutWorker: Simplify queue wait condition - Train: Convert multiple Condition implementations to lambdas - Array2DMap: Replace PointComparator with lambda expression and use diamond operator Also remove redundant test in DoubletonTest that provided no meaningful verification. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent d8b9b55 commit cf91c0c

4 files changed

Lines changed: 15 additions & 47 deletions

File tree

src/main/java/cz/vutbr/fit/interlockSim/sim/InOutWorker.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ protected void iteration() {
108108

109109
//cekej na odchod vlaku z fronty (bez te anonymni tridy please)
110110
logger.debug("InOutWorker {} waiting for train {} to leave queue", inOut.getName(), first);
111-
waitUntil(new Condition() {
112-
public boolean test() {
113-
return first != queqe.first();
114-
}
115-
});
111+
waitUntil(() -> first != queqe.first());
116112
logger.debug("InOutWorker {} train left queue", inOut.getName());
117113
}
118114
myIdle = true;

src/main/java/cz/vutbr/fit/interlockSim/sim/Train.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ private abstract class Site extends Process { //lepsi nazev?
7676
private TrackSection current = null;
7777
private boolean onNext = false;
7878

79-
final Condition terminated = new Condition() {
80-
public boolean test() {
81-
return terminated();
82-
}
83-
};
79+
final Condition terminated = this::terminated;
8480

8581
@Override
8682
protected final void actions() {
@@ -100,11 +96,9 @@ protected final void actions() {
10096

10197
onNext = true;
10298
assert position.isActive() && pv.isActive();
103-
waitUntil(new Condition() {
104-
public boolean test() {
105-
//dtmin - horni odhad zmeny pri poslednim kroku numericke metody behem dobrzdovani k uzlu
106-
return position.state + dtMin >= nextLength;
107-
}
99+
waitUntil(() -> {
100+
//dtmin - horni odhad zmeny pri poslednim kroku numericke metody behem dobrzdovani k uzlu
101+
return position.state + dtMin >= nextLength;
108102
});
109103

110104
position.state -= nextLength;
@@ -237,11 +231,9 @@ private void semaphoreAction(final RailSemaphore semaphore, final PathSeparator
237231
// }
238232

239233
private Condition allowingSignal(final RailSemaphore semaphore) {
240-
return new Condition() {
241-
public boolean test() {
242-
final boolean allowing = semaphore.getSignal().isAllowing();
243-
return allowing;
244-
}
234+
return () -> {
235+
final boolean allowing = semaphore.getSignal().isAllowing();
236+
return allowing;
245237
};
246238
}
247239

@@ -539,11 +531,7 @@ protected void actions() {//spusten odsouhlasenim
539531

540532
activate(front);
541533

542-
waitUntil(new Condition() {
543-
public boolean test() {
544-
return front.getTotalDistance() >= getLength();
545-
}
546-
});
534+
waitUntil(() -> front.getTotalDistance() >= getLength());
547535
activate(tail);
548536

549537
out(); activate((Train) worker.getQueqe().first());

src/main/java/cz/vutbr/fit/interlockSim/util/Array2DMap.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,16 @@ public int size() {
150150

151151
}
152152

153-
private static final class PointComparator implements Comparator<Point> {
154-
public int compare(Point o1, Point o2) {
155-
int dy = (o1.y-o2.y);
156-
return dy==0 ? (o1.x-o2.x) : dy;
157-
}
158-
}
159153
/**
160154
* Compare points in order for grid
161155
*/
162-
public static final Comparator<Point> POINT_COMPARATOR = new PointComparator();
156+
public static final Comparator<Point> POINT_COMPARATOR = (o1, o2) -> {
157+
int dy = (o1.y-o2.y);
158+
return dy==0 ? (o1.x-o2.x) : dy;
159+
};
163160

164161
private final RelocableList<RelocableList<V>> array = new RelocableList<RelocableList<V>> ();
165-
private final TreeSet<Point> keys = new TreeSet<Point>(POINT_COMPARATOR);
162+
private final TreeSet<Point> keys = new TreeSet<>(POINT_COMPARATOR);
166163

167164
/* (non-Javadoc)
168165
* @see java.util.AbstractMap#entrySet()
@@ -224,7 +221,7 @@ public boolean containsKey(Object key) {
224221
public List<V> getRow(int y) {
225222
final RelocableList<V> list = array.get(y);
226223
//EXTENSION zatim unmodifieable
227-
return list==null ? Collections.<V>emptyList() : Collections.unmodifiableList(list);
224+
return list==null ? Collections.emptyList() : Collections.unmodifiableList(list);
228225
}
229226

230227
@Override

src/test/java/cz/vutbr/fit/interlockSim/util/DoubletonTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,6 @@ void hashCode_reversedElements_sameHashCode() {
234234
.isEqualTo(d2.hashCode());
235235
}
236236

237-
@Test
238-
void hashCode_differentElements_mayDiffer() {
239-
Doubleton<String, Integer> d1 = new Doubleton<>("A", "B");
240-
Doubleton<String, Integer> d2 = new Doubleton<>("C", "D");
241-
242-
// Not guaranteed to differ, but likely
243-
// Just verifying it doesn't crash and returns valid hash codes
244-
int h1 = d1.hashCode();
245-
int h2 = d2.hashCode();
246-
// Primitive ints are never null, so we just verify the method executes without error
247-
// The fact that we got here means hashCode() didn't throw an exception
248-
}
249-
250237
@Test
251238
void equals_sameElements_equal() {
252239
Doubleton<String, Integer> d1 = new Doubleton<>("A", "B");

0 commit comments

Comments
 (0)