/** * Wrap a binding in a LensKit binding, with a coercion function to allow type conversions. * This will allow instances of other types to be bound, if the coercion function provides * a transformation. Ordinarily, the generic types prevent this feature from being used, but * a raw type (returned from {@link LenskitConfigContext#set(Class)} * or commonly arising in Groovy) will allow arbitrary objects, and the coercion function will * convert them. It should return an absent optional if the type * is unconvertable, and an optional wrapping the converted value if it is present. If the * coercion fails, the original value will be used as-is (which will usually result in an error * from the underlying binding). * * @param binding The binding to wrap. * @param coercion The coercion function. * @param <T> The bound type. * @return The */ static <T> LenskitBinding<T> wrap(Binding<T> binding, Function<Object,Optional<T>> coercion) { if (coercion == null && binding instanceof LenskitBinding) { return (LenskitBinding<T>) binding; } else { return new LenskitBindingImpl<>(binding, coercion); } }
@Override public void toInstance(@Nullable T instance) { to(instance); }
/** * Wrap a binding in a LensKit binding. * @param binding The binding to wrap. * @param <T> The bound type. * @return The LensKit binding wrapper. */ static <T> LenskitBinding<T> wrap(Binding<T> binding) { return wrap(binding, null); }
@Override public Binding<T> fixed() { return wrap(binding.fixed(), coercion); }
@Override public void toInstance(@Nullable T instance) { to(instance); }
/** * Wrap a binding in a LensKit binding, with a coercion function to allow type conversions. * This will allow instances of other types to be bound, if the coercion function provides * a transformation. Ordinarily, the generic types prevent this feature from being used, but * a raw type (returned from {@link LenskitConfigContext#set(Class)} * or commonly arising in Groovy) will allow arbitrary objects, and the coercion function will * convert them. It should return an absent optional if the type * is unconvertable, and an optional wrapping the converted value if it is present. If the * coercion fails, the original value will be used as-is (which will usually result in an error * from the underlying binding). * * @param binding The binding to wrap. * @param coercion The coercion function. * @param <T> The bound type. * @return The */ static <T> LenskitBinding<T> wrap(Binding<T> binding, Function<Object,Optional<T>> coercion) { if (coercion == null && binding instanceof LenskitBinding) { return (LenskitBinding<T>) binding; } else { return new LenskitBindingImpl<>(binding, coercion); } }
@Override public <T> LenskitBinding<T> bind(Class<T> type) { return LenskitBindingImpl.wrap(base.bind(type)); }
@Override public LenskitBinding<T> unqualified() { return wrap(binding.unqualified(), coercion); }
@Override public LenskitBinding<T> shared() { return wrap(binding.shared(), coercion); }
@Override public LenskitBinding<T> withQualifier(@Nonnull Class<? extends Annotation> qualifier) { return wrap(binding.withQualifier(qualifier), coercion); }
@Override public LenskitBinding<T> withQualifier(@Nonnull Annotation annot) { return wrap(binding.withQualifier(annot), coercion); }
@Override public LenskitBinding<T> exclude(@Nonnull Class<?> exclude) { return wrap(binding.exclude(exclude), coercion); }
@Override public LenskitBinding<T> withAnyQualifier() { return wrap(binding.withAnyQualifier(), coercion); }
@Override public LenskitBinding<T> unshared() { return wrap(binding.unshared(), coercion); }
@Override public <T> LenskitBinding<T> bind(Class<? extends Annotation> qual, Class<T> type) { return LenskitBindingImpl.wrap(super.bind(qual, type)); }
@Override public <T> LenskitBinding<T> bindAny(Class<T> type) { return LenskitBindingImpl.wrap(super.bindAny(type)); }
@Override @SuppressWarnings("rawtypes") public Binding set(@Nonnull Class<? extends Annotation> param) { Preconditions.checkNotNull(param); // Parameter annotation appears on the alias target Class<? extends Annotation> real = Qualifiers.resolveAliases(param); final Parameter annot = real.getAnnotation(Parameter.class); if (annot == null) { throw new IllegalArgumentException(param.toString() + "has no Parameter annotation"); } Class<?> type = annot.value(); Binding<?> binding; if (type.equals(File.class)) { binding = LenskitBindingImpl.wrap(bind(File.class), new StringToFileConversion()); } else { binding = bind(annot.value()); } return binding.withQualifier(param); }
/** * Wrap a binding in a LensKit binding. * @param binding The binding to wrap. * @param <T> The bound type. * @return The LensKit binding wrapper. */ static <T> LenskitBinding<T> wrap(Binding<T> binding) { return wrap(binding, null); }
@Override public LenskitBinding<T> unqualified() { return wrap(binding.unqualified(), coercion); }
@Override public LenskitBinding<T> withQualifier(@Nonnull Annotation annot) { return wrap(binding.withQualifier(annot), coercion); }