int (RemoteServerNodeServiceViewContributor o1, RemoteServerNodeServiceViewContributor o2) { Function<RemoteServerNodeServiceViewContributor, String> getName = contributor -> Optional.ofNullable(contributor) .map(RemoteServerNodeServiceViewContributor::asService) .map(o -> ObjectUtils.tryCast(o, RemoteServerNode.class)) .map(node -> node.getServer().getName()) .orElse(null); String name1 = getName.fun(o1); String name2 = getName.fun(o2); if (name1 == null || name2 == null) { if (name1 == null && name2 == null) return 0; return name1 == null ? -1 : 1; } return name1.compareTo(name2); }