Skip to content

Commit 598b4fc

Browse files
committed
[java] Refactoring SessionData to avoid using Optional without actual need in it
1 parent 7040cc0 commit 598b4fc

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

java/server/src/org/openqa/selenium/grid/graphql/SessionData.java

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public SessionData(Distributor distributor) {
4242
distributorStatus = Suppliers.memoize(distributor::getStatus);
4343
}
4444

45+
private static class SessionInSlot {
46+
private final org.openqa.selenium.grid.data.Session session;
47+
private final NodeStatus node;
48+
private final Slot slot;
49+
50+
SessionInSlot(org.openqa.selenium.grid.data.Session session, NodeStatus node, Slot slot) {
51+
this.session = session;
52+
this.node = node;
53+
this.slot = slot;
54+
}
55+
}
56+
4557
@Override
4658
public Object get(DataFetchingEnvironment environment) {
4759
String sessionId = environment.getArgument("id");
@@ -52,38 +64,35 @@ public Object get(DataFetchingEnvironment environment) {
5264

5365
Set<NodeStatus> nodeStatuses = distributorStatus.get().getNodes();
5466

55-
Optional<org.openqa.selenium.grid.data.Session> currentSession = Optional.empty();
56-
NodeStatus currentSessionNode = null;
57-
Slot currentSessionSlot = null;
67+
SessionInSlot currentSession = findSession(sessionId, nodeStatuses);
68+
69+
if (currentSession != null) {
70+
org.openqa.selenium.grid.data.Session session = currentSession.session;
71+
72+
return new org.openqa.selenium.grid.graphql.Session(
73+
session.getId().toString(),
74+
session.getCapabilities(),
75+
session.getStartTime(),
76+
session.getUri(),
77+
currentSession.node.getId().toString(),
78+
currentSession.node.getUri(),
79+
currentSession.slot);
80+
} else {
81+
throw new SessionNotFoundException("No ongoing session found with the requested session id.",
82+
sessionId);
83+
}
84+
}
5885

86+
private SessionInSlot findSession(String sessionId, Set<NodeStatus> nodeStatuses) {
5987
for (NodeStatus status : nodeStatuses) {
6088
for (Slot slot : status.getSlots()) {
6189
Optional<org.openqa.selenium.grid.data.Session> session = slot.getSession();
6290

63-
if (session.isPresent()
64-
&& sessionId.equals(session.get().getId().toString())) {
65-
currentSession = Optional.of(session.get());
66-
currentSessionNode = status;
67-
currentSessionSlot = slot;
68-
break;
91+
if (session.isPresent() && sessionId.equals(session.get().getId().toString())) {
92+
return new SessionInSlot(session.get(), status, slot);
6993
}
7094
}
7195
}
72-
73-
if (currentSession.isPresent()) {
74-
org.openqa.selenium.grid.data.Session session = currentSession.get();
75-
76-
return new org.openqa.selenium.grid.graphql.Session(
77-
session.getId().toString(),
78-
session.getCapabilities(),
79-
session.getStartTime(),
80-
session.getUri(),
81-
currentSessionNode.getId().toString(),
82-
currentSessionNode.getUri(),
83-
currentSessionSlot);
84-
} else {
85-
throw new SessionNotFoundException("No ongoing session found with the requested session id.",
86-
sessionId);
87-
}
96+
return null;
8897
}
8998
}

0 commit comments

Comments
 (0)