Last active
January 16, 2024 17:11
-
-
Save edeandrea/27ee1c61f05e640fc6fa1e19b8fb756e to your computer and use it in GitHub Desktop.
Functional builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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