Model Picker — Java source
Filter every major model by context window, price, modality, and tier — sort by cost, context, or tokens-per-dollar to find the right model for the task.
This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.
// model-picker — Java port: filter + rank the AI model catalog.
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/** Filter + rank the AI model catalog, ported from the canonical TS lib. */
public final class ModelPicker {
/** Pricing projection of the TS AiModel record — null fields play the
* role of TS null (unpriced model / unknown release date). */
public record Model(String id, String tier, long contextWindow,
Double inputPerM, Double outputPerM, String released) {}
/** Projection of the TS ModelFilter — the three constraints the presets use. */
public record ModelFilter(String tier, Long minContext, Double maxInputPerM) {
public static final ModelFilter NONE = new ModelFilter(null, null, null);
}
/** Curated entry points: a filter + the sort that makes that filter useful. */
public static final Map<String, ModelFilter> PRESET_FILTERS = Map.of(
"long-context", new ModelFilter(null, 500_000L, null),
"cheap-bulk", new ModelFilter(null, null, 1d),
"flagship", new ModelFilter("flagship", null, null));
/** id ascending — the shared stable tie-break for every sort. */
static final Comparator<Model> BY_ID = Comparator.comparing(Model::id);
/** Output tokens per USD: 1e6 / outputPerM, or null when unpriced. */
static Double tokensPerDollar(Model m) {
return m.outputPerM() == null ? null : 1e6 / m.outputPerM();
}
/** Sort comparator per key: value order first, null last, id asc on ties. */
static final Map<String, Comparator<Model>> COMPARATORS = Map.of(
"price", Comparator.comparing(Model::inputPerM,
Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(BY_ID),
"context", Comparator.comparingLong(Model::contextWindow).reversed().thenComparing(BY_ID),
"tokensPerDollar", Comparator.comparing(ModelPicker::tokensPerDollar,
Comparator.nullsLast(Comparator.reverseOrder())).thenComparing(BY_ID),
"released", Comparator.comparing(Model::released,
Comparator.nullsLast(Comparator.reverseOrder())).thenComparing(BY_ID));
/** Filter step, inlined from listModels in src/lib/ai/models.ts (the
* pricing snapshot accessor): null prices never satisfy maxInputPerM. */
static List<Model> listModels(List<Model> models, ModelFilter f) {
return models.stream().filter(m ->
(f.tier() == null || m.tier().equals(f.tier())) &&
(f.minContext() == null || m.contextWindow() >= f.minContext()) &&
(f.maxInputPerM() == null ||
(m.inputPerM() != null && m.inputPerM() <= f.maxInputPerM())))
.toList();
}
/** Filter the model list, then sort it. See COMPARATORS for the exact order. */
static List<Model> pickModels(List<Model> models, ModelFilter f, String sort) {
return listModels(models, f).stream().sorted(COMPARATORS.get(sort)).toList();
}
public static void main(String[] args) {
List<Model> models = List.of(
new Model("atlas-flagship", "flagship", 1_000_000, 3.0, 15.0, "2025-03-01"),
new Model("atlas-mini", "fast", 200_000, 0.5, 2.0, "2025-01-15"),
new Model("nova-open", "balanced", 1_000_000, null, null, null), // unpriced
new Model("zeta-budget", "budget", 128_000, 0.5, 1.0, "2024-12-01"));
System.out.println("long-context (minContext >= 500000, sort context):");
for (Model m : pickModels(models, PRESET_FILTERS.get("long-context"), "context"))
System.out.printf(" %-14s ctx %d%n", m.id(), m.contextWindow());
System.out.println("price sort (all models, unpriced last, ties by id):");
for (Model m : pickModels(models, ModelFilter.NONE, "price")) {
if (m.inputPerM() != null)
System.out.printf(" %-14s $%.2f/M in%n", m.id(), m.inputPerM());
else
System.out.printf(" %-14s unpriced%n", m.id());
}
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →