com.yahoo.jdisc.application.BindingSet Maven / Gradle / Ivy
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import java.net.URI;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* This is an immutable set of ordered bindings from {@link UriPattern}s to some target type T. To create an instance
* of this class, you must 1) create a {@link BindingRepository}, 2) configure it using the {@link
* BindingRepository#bind(String, Object)} method, and finally 3) call {@link BindingRepository#activate()}.
*
* @author Simon Thoresen Hult
*/
public class BindingSet implements Iterable> {
public static final String DEFAULT = "default";
private final Collection> bindings;
BindingSet(Collection> bindings) {
this.bindings = sorted(bindings);
}
/**
* Resolves the binding that best matches (see commentary on {@link BindingRepository#bind(String, Object)}) the
* given {@link URI}, and returns a {@link BindingMatch} object that describes the match and contains the
* matched target. If there is no binding that matches the given URI, this method returns null.
*
* @param uri The URI to match against the bindings in this set.
* @return A {@link BindingMatch} object describing the match found, or null if not found.
*/
public BindingMatch match(URI uri) {
for (Map.Entry entry : bindings) {
UriPattern pattern = entry.getKey();
UriPattern.Match match = pattern.match(uri);
if (match != null) {
return new BindingMatch<>(match, entry.getValue(), pattern);
}
}
return null;
}
/**
* Resolves the binding that best matches (see commentary on {@link BindingRepository#bind(String, Object)}) the
* given {@link URI}, and returns that target. If there is no binding that matches the given URI, this method
* returns null.
*
* Apart from a null-guard, this is equal to return match(uri).target()
.
*
* @param uri The URI to match against the bindings in this set.
* @return The best matched target, or null.
* @see #match(URI)
*/
public T resolve(URI uri) {
BindingMatch match = match(uri);
if (match == null) {
return null;
}
return match.target();
}
@Override
public Iterator> iterator() {
return bindings.iterator();
}
private static Collection> sorted(Collection> unsorted) {
return unsorted.stream().sorted(Map.Entry.comparingByKey()).toList();
}
}