I have a POJO as shown below which will have hostname
and other attributes.
public class MachineInfo {
private String hostname;
// getters and setters here
}
I have my below method from which I need to randomly pick a hostname
which is up for more than 10 minutes from machines
list. The checkHostUptime
method will return true
if a particular machine is up for more than 10 minutes.
public static String pickRandomMachine(List<MachineInfo> machines) {
String hostname = null;
Collections.shuffle(machines, new Random(System.nanoTime()));
for (MachineInfo info : machines) {
if (!checkHostUptime(info.getHostName())) {
continue;
}
hostname = info.getHostName();
break;
}
return hostname;
}
I want to keep on trying finding the hostname
until I find the hostname
which is up for more than 10 minutes from machines
list. If I am not able to find anything, then I would simply return null
.
Is there any better way of doing the same thing?