Skip to content

Commit

Permalink
Add explanatory comments to the new code in BackgroundFunctionExecutor.
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnmcmanus committed Sep 4, 2020
1 parent 1055468 commit f3e0994
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private BackgroundFunctionExecutor(FunctionExecutor<?> functionExecutor) {
this.functionExecutor = functionExecutor;
}

enum FunctionKind {
private enum FunctionKind {
BACKGROUND(BackgroundFunction.class),
RAW_BACKGROUND(RawBackgroundFunction.class),
CLOUD_EVENTS(ExperimentalCloudEventsFunction.class);
Expand All @@ -69,11 +69,21 @@ enum FunctionKind {
this.functionClass = functionClass;
}

/** Returns the {@link FunctionKind} that the given class implements, if any. */
static Optional<FunctionKind> forClass(Class<?> functionClass) {
return VALUES.stream().filter(v -> v.functionClass.isAssignableFrom(functionClass)).findFirst();
}
}

/**
* Optionally makes a {@link BackgroundFunctionExecutor} for the given class, if it implements one
* of {@link BackgroundFunction}, {@link RawBackgroundFunction}, or
* {@link ExperimentalCloudEventsFunction}. Otherwise returns {@link Optional#empty()}.
*
* @param functionClass the class of a possible background function implementation.
* @throws RuntimeException if the given class does implement one of the required interfaces, but we are
* unable to construct an instance using its no-arg constructor.
*/
public static Optional<BackgroundFunctionExecutor> maybeForClass(Class<?> functionClass) {
Optional<FunctionKind> maybeFunctionKind = FunctionKind.forClass(functionClass);
if (!maybeFunctionKind.isPresent()) {
Expand All @@ -83,11 +93,12 @@ public static Optional<BackgroundFunctionExecutor> maybeForClass(Class<?> functi
}

/**
* Makes a {@link HttpFunctionExecutor} for the given class.
* Makes a {@link BackgroundFunctionExecutor} for the given class.
*
* @throws RuntimeException if either the class does not implement one of
* {@link BackgroundFunction} or {@link RawBackgroundFunction},
* or we are unable to construct an instance using its no-arg constructor.
* {@link BackgroundFunction}, {@link RawBackgroundFunction}, or
* {@link ExperimentalCloudEventsFunction}; or we are unable to construct an instance using its no-arg
* constructor.
*/
public static BackgroundFunctionExecutor forClass(Class<?> functionClass) {
Optional<FunctionKind> maybeFunctionKind = FunctionKind.forClass(functionClass);
Expand Down

0 comments on commit f3e0994

Please sign in to comment.