All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.jdisc.application.BindingSet Maven / Gradle / Ivy

There is a newer version: 8.410.10
Show newest version
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.google.common.collect.ImmutableList;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
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 */ public class BindingSet implements Iterable> { public static final String DEFAULT = "default"; private final Collection> bindings; BindingSet(Collection> bindings) { this.bindings = sort(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> sort(Collection> unsorted) { List> ret = new LinkedList<>(unsorted); Collections.sort(ret, new Comparator>() { @Override public int compare(Map.Entry lhs, Map.Entry rhs) { return lhs.getKey().compareTo(rhs.getKey()); } }); return ImmutableList.copyOf(ret); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy