Skip to content

Instantly share code, notes, and snippets.

@mariofusco
Created December 18, 2024 13:32
Show Gist options
  • Save mariofusco/c9071b51d18b5577d22f3e25cc9e003c to your computer and use it in GitHub Desktop.
Save mariofusco/c9071b51d18b5577d22f3e25cc9e003c to your computer and use it in GitHub Desktop.
// --- The ContentMetadata enumeration
public sealed interface ContentMetadata<T> extends TypePerEntryMap.Key<T> {
final class Score implements ContentMetadata<Double> { private Score() {} }
final class EmbeddingId implements ContentMetadata<String> { private EmbeddingId() {} }
final class RerankedScore implements ContentMetadata<Double> { private RerankedScore() {} }
Score SCORE = new Score();
EmbeddingId EMBEDDING_ID = new EmbeddingId();
RerankedScore RERANKED_SCORE = new RerankedScore();
}
// --- How to use it
private void test() {
TypePerEntryMap<ContentMetadata> map = TypePerEntryMap.of(ContentMetadata.SCORE, 10.0, ContentMetadata.EMBEDDING_ID, "test");
Double score = map.get(ContentMetadata.SCORE);
String embeddingId = map.get(ContentMetadata.EMBEDDING_ID);
}
// --- The TypePerEntryMap
public class TypePerEntryMap<K extends TypePerEntryMap.Key> {
public interface Key<T> { }
private final Map<Key<?>, Object> innerMap = new HashMap<>();
public <T> void put(Key<T> key, T value) {
innerMap.put(key, value);
}
public <T> T get(Key<T> key) {
return (T) innerMap.get(key);
}
public static TypePerEntryMap of() {
return new TypePerEntryMap();
}
public static <A> TypePerEntryMap of(Key<A> key, A value) {
TypePerEntryMap map = new TypePerEntryMap();
map.put(key, value);
return map;
}
public static <A, B> TypePerEntryMap of(Key<A> key1, A value1, Key<B> key2, B value2) {
TypePerEntryMap map = new TypePerEntryMap();
map.put(key1, value1);
map.put(key2, value2);
return map;
}
public static <A, B, C> TypePerEntryMap of(Key<A> key1, A value1, Key<B> key2, B value2, Key<C> key3, C value3) {
TypePerEntryMap map = new TypePerEntryMap();
map.put(key1, value1);
map.put(key2, value2);
map.put(key3, value3);
return map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment