/** * Creates a new {@link EnricherSpec} instance for an enricher of the given type. The returned * {@link EnricherSpec} can then be customized. * * @param type A {@link Enricher} class */ public static <T extends Enricher> EnricherSpec<T> create(Class<T> type) { return new EnricherSpec<T>(type); }
protected EnricherSpec(Class<T> type) { checkIsImplementation(type); checkIsNewStyleImplementation(type); this.type = type; }
/** * Creates a new {@link EnricherSpec} instance with the given config, for an enricher of the given type. * * This is primarily for groovy code; equivalent to {@code EnricherSpec.create(type).configure(config)}. * * @param config The spec's configuration (see {@link EnricherSpec#configure(Map)}). * @param type An {@link Enricher} class */ public static <T extends Enricher> EnricherSpec<T> create(Map<?,?> config, Class<T> type) { return EnricherSpec.create(type).configure(config); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public <T extends Enricher> T createEnricher(EnricherSpec<T> spec) { if (spec.getFlags().containsKey("parent")) { throw new IllegalArgumentException("Spec's flags must not contain parent; use spec.parent() instead for "+spec); Class<? extends T> clazz = spec.getType(); if (spec.getDisplayName()!=null) ((AbstractEnricher)enricher).setName(spec.getDisplayName()); Map<String, Object> config = ConfigBag.newInstance().putAll(spec.getFlags()).putAll(spec.getConfig()).getAllConfig(); for (Map.Entry<ConfigKey<?>, Object> entry : spec.getConfig().entrySet()) { ((AbstractEnricher)enricher).setConfig((ConfigKey)entry.getKey(), entry.getValue());
public <V> EnricherSpec<T> configureIfNotNull(ConfigKey<V> key, V val) { return (val != null) ? configure(key, val) : this; }
private <T extends Enricher> T construct(Class<? extends T> clazz, EnricherSpec<T> spec) throws InstantiationException, IllegalAccessException, InvocationTargetException { if (isNewStyleEnricher(clazz)) { return clazz.newInstance(); } else { return constructOldStyle(clazz, MutableMap.copyOf(spec.getFlags())); } }