Cake { public String getPublishedKeyForValue(Object value, boolean fullyQualified) { List<String> matchingKeys = entries.entrySet().stream() .filter(e -> e.getValue().equals(value)) .map(Map.Entry::getKey) .collect(Collectors.toList()); if (matchingKeys.size() == 1) { if (fullyQualified) { return matchingKeys.get(0); } String[] splitKey = StringUtils.split(matchingKeys.get(0), Cake.SEPARATOR); return splitKey[splitKey.length - 1]; } if (matchingKeys.size() > 1) { throw new IllegalArgumentException("multiple keys found for object " + value); } throw new IllegalArgumentException("no key found for object " + value); } Cake();  Cake(Cake other); static String key(String... subKeys); @SuppressWarnings("unchecked") T get(String... key); @SuppressWarnings("unchecked") T get(Class<T> clazz, String... key); void publish(String key, Object value); void inNamespace(String key, Runnable runnable); String getPublishedKeyForValue(Object value, boolean fullyQualified); @JsonIgnore String getNamespace(); boolean hasContext(); @JsonIgnore T getContext(); @JsonIgnore @SuppressWarnings("unchecked") T getContext(Class<T> clazz); @JsonIgnore T getOrGetContext(String... key); @JsonIgnore T getOrGetContext(Class<T> clazz, String... key); static final String SEPARATOR; }
@Test(expected = RuntimeException.class) public void testGetPublishedKeyForValue_throwsOnNonExistentValue() { cake.getPublishedKeyForValue("foo", false); }