Machines { @Nonnull @SuppressWarnings("unchecked") public static <T> Maybe<T> findUniqueElement(Iterable<?> items, Class<T> type) { if (items == null) { return Maybe.absent(new NullPointerException("Null iterable")); } Iterator<?> i = items.iterator(); T result = null; while (i.hasNext()) { Object candidate = i.next(); if (type.isInstance(candidate)) { if (result==null) result = (T)candidate; else { if (log.isTraceEnabled()) log.trace("Multiple instances of "+type+" in "+items+"; ignoring"); return Maybe.absent(new IllegalStateException("Multiple instances of "+type+" in "+items+"; expected a single one")); } } } if (result==null) return Maybe.absent(new IllegalStateException("No instances of "+type+" available (in "+items+")")); return Maybe.of(result); }  static Maybe<String> getSubnetHostname(Location where); static Maybe<String> getSubnetIp(Location where); @Nonnull @SuppressWarnings("unchecked") static Maybe<T> findUniqueElement(Iterable<?> items, Class<T> type); static Maybe<MachineLocation> findUniqueMachineLocation(Iterable<? extends Location> locations); static Maybe<T> findUniqueMachineLocation(Iterable<? extends Location> locations, Class<T> clazz); static Maybe<String> findSubnetHostname(Iterable<? extends Location> ll); static Maybe<String> findSubnetHostname(Entity entity); static Maybe<String> findSubnetOrPublicHostname(Entity entity); static Maybe<String> findSubnetOrPrivateIp(Entity entity); static Maybe<String> findSubnetIp(Entity entity); static Maybe<String> findSubnetIp(Iterable<? extends Location> ll); static boolean warnIfLocalhost(Collection<? extends Location> locations, String message);  }
@Test public void testFindUniqueElementAbsentWhenItemsNull() throws Exception { assertTrue(Machines.findUniqueElement(null, SshMachineLocation.class).isAbsent()); }