Skip to content

Instantly share code, notes, and snippets.

@edeandrea
Last active January 16, 2024 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edeandrea/27ee1c61f05e640fc6fa1e19b8fb756e to your computer and use it in GitHub Desktop.
Save edeandrea/27ee1c61f05e640fc6fa1e19b8fb756e to your computer and use it in GitHub Desktop.
Functional builder
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class SomeModel {
public interface SomeModelOption extends Consumer<SomeModel> {}
private String modelName;
private Float temperature;
private Integer maxOutputTokens;
public SomeModel(SomeModelOption... options) {
Optional.ofNullable(options)
.map(Stream::of)
.orElseGet(Stream::empty)
.forEach(option -> option.accept(this));
}
public static SomeModelOption modelName(String modelName) {
return model -> model.modelName = modelName;
}
public static SomeModelOption temperature(Float temperature) {
return model -> model.temperature = temperature;
}
public static SomeModelOption maxOutputTokens(Integer maxOutputTokens) {
return model -> model.maxOutputTokens = maxOutputTokens;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment