private static <T> T cast(Key<T> key, Object o) { return key.getType().cast(o); }
public static <T> T get(Map<Key<?>, ?> map, Key<T> key) { return key.cast(map.get(key)); }
@Override public <T> T getExtension(Key<T> key) { Assert.notNull(key, "key is null"); return Key.get(this.extensions, key); }
/** * Create key from MetatypeInfo * @param mi * @param <T> * @return */ @SuppressWarnings("unchecked") public static <T> Key<T> toKey(MetatypeInfo mi) { return (Key<T>) new Key<>(mi.name(), mi.type()); } }
/** * The single point for register all common formatters to our MetatypeFormatterRegistry and spring FormatterRegistry * @param metatypeFormatterRegistry * @param formatters */ @Autowired @SuppressWarnings("unchecked") void configureMetatypeFormatterRegistry(MetatypeFormatterRegistry metatypeFormatterRegistry, List<Formatter<?>> formatters) { for(final Formatter<?> formatter: formatters) { Set<Key<?>> set = FormatterUtils.getHandledMetatypes(formatter); if(set.isEmpty()) { continue; } final Set<Class<?>> types = new HashSet<>(); for(Key<?> key: set) { types.add(key.getType()); metatypeFormatterRegistry.addFormatter(key, (Formatter)formatter); } if(formatterRegistry != null) { formatterRegistry.addFormatterForFieldAnnotation(new AnnotationAnnotationFormatterFactory(types, formatter)); } } }
@Override public <T> T getOrCreateExtension(Key<T> key, ExtensionFactory<T, M> factory) { Assert.notNull(key, "key is null"); Object old = this.extensions.computeIfAbsent(key, (k) -> factory.create(key, this)); return key.cast(old); }