+ /**
+ * Returns the key/value mapping best matching specified criterion.
+ *
+ * @param storage namespace storage
+ * @param criterion selection criterion
+ * @return Selected mapping, if available.
+ */
+ public final Optional<Entry<K, V>> getFrom(final NamespaceStorageNode storage,
+ final NamespaceKeyCriterion<K> criterion) {
+ final Map<K, V> mappings = getAllFrom(storage);
+ if (mappings == null) {
+ return Optional.empty();
+ }
+
+ Entry<K, V> match = null;
+ for (Entry<K, V> entry : mappings.entrySet()) {
+ final K key = entry.getKey();
+ if (criterion.match(key)) {
+ if (match != null) {
+ final K selected = criterion.select(match.getKey(), key);
+ if (selected.equals(match.getKey())) {
+ continue;
+ }
+
+ Verify.verify(selected == key, "Criterion %s selected invalid key %s from candidates [%s %s]",
+ selected, match.getKey(), key);
+ }
+
+ match = entry;
+ }
+ }
+
+ return Optional.ofNullable(match);
+ }
+