@Override public ComponentMap get() { ClassToInstanceMap<Object> components = MutableClassToInstanceMap.create(); components.putInstance(HolidaySource.class, _holidaySource); components.putInstance(ConfigSource.class, _configSource); components.putInstance(HistoricalTimeSeriesSource.class, _htsSource); components.putInstance(ConventionBundleSource.class, _conventionBundleSource); components.putInstance(HistoricalTimeSeriesResolver.class, _htsResolver); components.putInstance(SecuritySource.class, _securitySource); components.putInstance(ConventionSource.class, _conventionSource); components.putInstance(RegionSource.class, _regionSource); components.putInstance(PositionSource.class, _positionSource); components.putInstance(MarketDataSnapshotSource.class, _snapshotSource); components.putInstance(LegalEntitySource.class, _legalEntitySource); components.putInstance(ExchangeSource.class, _exchangeSource); ComponentMap componentMap = ComponentMap.of(components); ServiceContext serviceContext = ServiceContext.of(componentMap.getComponents()) .with(VersionCorrectionProvider.class, new FixedInstantVersionCorrectionProvider(Instant.now())); ThreadLocalServiceContext.init(serviceContext); return componentMap; }
@Test public void classToINstanceMap_example () { Person person = new Person("Jackson"); Jobs jobs = new Jobs("IT person"); Address address = new Address("505 Williams Street"); ClassToInstanceMap<Object> classToInstanceMap = MutableClassToInstanceMap.create(); classToInstanceMap.put(Person.class, person); classToInstanceMap.put(Jobs.class, jobs); classToInstanceMap.put(Address.class, address); logger.info(classToInstanceMap); assertEquals("IT person", classToInstanceMap.getInstance(Jobs.class).getJobName()); }
/** * Returns a copy of this configuration only including the given fragments (which the current * configuration is assumed to have). */ public BuildConfiguration clone( FragmentClassSet fragmentClasses, RuleClassProvider ruleClassProvider) { ClassToInstanceMap<Fragment> fragmentsMap = MutableClassToInstanceMap.create(); for (Fragment fragment : fragments.values()) { if (fragmentClasses.fragmentClasses().contains(fragment.getClass())) { fragmentsMap.put(fragment.getClass(), fragment); } } BuildOptions options = buildOptions.trim( getOptionsClasses(fragmentsMap.keySet(), ruleClassProvider)); BuildConfiguration newConfig = new BuildConfiguration( directories, fragmentsMap, options, mainRepositoryName.strippedName()); return newConfig; }
private List<String> getHighlights(ClassToInstanceMap<FieldDataSource<?>> context) { QueryResponse response = context.getInstance(QueryResponseDataSource.class).get(); Schema<?> schema = context.getInstance(SchemaDataSource.class).get(); String docId = schema.id.getField().getValue(context); if (docId == null) { return null; } Map<String, Map<String, List<String>>> hilighting = response.getHighlighting(); if (hilighting == null) { return null; } Map<String, List<String>> hilightingForDoc = hilighting.get(docId); if (hilightingForDoc == null) { return null; } return hilightingForDoc.get(getFieldName()); }
static <T extends Completable> T getInstance(Class<T> type, Supplier<T> supplier) { ClassToInstanceMap<Completable> components = state().components; @Nullable T instance = components.getInstance(type); if (instance == null) { instance = supplier.get(); components.putInstance(type, instance); } return instance; }
public static void main(String[] args) { ClassToInstanceMap<Number> numbers = MutableClassToInstanceMap.create(); numbers.putInstance(Integer.class, Integer.valueOf(0)); numbers.putInstance(Double.class, Double.valueOf(1)); numbers.putInstance(Float.class, Float.valueOf(3)); double myNum = numbers.getInstance(Double.class); System.out.println(myNum); }
/** * Creates a class to instance map, from the given coordinates. The map will contain the classes of the coordinates * as keys and the coordinates themselves as values. Duplicate keys (dimensions) are not allowed and will result in * an {@link IllegalArgumentException}. * * @param coordinates the coordinates to be added to the map * @return an immutable map from dimensions (coordinate classes) to coordinate * @throws IllegalArgumentException if more than one coordinate per dimension are provided * @deprecated */ @Deprecated public static <C> ClassToInstanceMap<C> mapOf(Iterable<? extends C> coordinates) { ImmutableClassToInstanceMap.Builder<C> coordinateBuilder = ImmutableClassToInstanceMap.builder(); for (C coordinate : coordinates) { @SuppressWarnings("unchecked") Class<C> coordinateClass = (Class<C>) coordinate.getClass(); coordinateBuilder.put(coordinateClass, coordinate); } return coordinateBuilder.build(); }
private <T1 extends Option<T1>> ImmutableOptionRegistry(Collection<T> options) { /* * we first have to create a mutable map, because the collection might contain options of the same class, where * later ones will override previous ones. This would not be allowed by the builder of the immutable map. */ ClassToInstanceMap<T> mutableOptions = MutableClassToInstanceMap.create(); addToMap(mutableOptions, options); this.options = ImmutableClassToInstanceMap.copyOf(mutableOptions); }
private void addToMap(ClassToInstanceMap<T> mutableMap, Collection<T> toAdd) { for (T option : toAdd) { @SuppressWarnings("unchecked") Class<T> markerInterface = (Class<T>) option.getMarkerInterface(); mutableMap.putInstance(markerInterface, option); } }
/** * Gets the map of SETTE annotations. * * @param element * an annotated element * @return a map of SETTE annotations */ public static ClassToInstanceMap<Annotation> getSetteAnnotations( @NonNull AnnotatedElement element) { ClassToInstanceMap<Annotation> setteAnnots = MutableClassToInstanceMap.create(); Arrays.stream(element.getAnnotations()) .filter(SetteAnnotationUtils::isSetteAnnotation) .forEach(a -> setteAnnots.put(a.annotationType(), a)); return setteAnnots; }
static PlaceholderMethod create( CharSequence name, UType returnType, ImmutableMap<UVariableDecl, ImmutableClassToInstanceMap<Annotation>> parameters, ClassToInstanceMap<Annotation> annotations) { final boolean allowsIdentity = annotations.getInstance(Placeholder.class).allowsIdentity(); final Class<? extends Matcher<? super ExpressionTree>> matchesClass = annotations.containsKey(Matches.class) ? UTemplater.getValue(annotations.getInstance(Matches.class)) : null; final Class<? extends Matcher<? super ExpressionTree>> notMatchesClass = annotations.containsKey(NotMatches.class) ? UTemplater.getValue(annotations.getInstance(NotMatches.class)) : null; final Predicate<Tree.Kind> allowedKinds = annotations.containsKey(OfKind.class) ? Predicates.<Tree.Kind>in(Arrays.asList(annotations.getInstance(OfKind.class).value())) : Predicates.<Tree.Kind>alwaysTrue(); class PlaceholderMatcher implements Serializable, Matcher<ExpressionTree> { @Override public boolean matches(ExpressionTree t, VisitorState state) { try { return (allowsIdentity || !(t instanceof PlaceholderParamIdent)) && (matchesClass == null || matchesClass.newInstance().matches(t, state)) && (notMatchesClass == null || !notMatchesClass.newInstance().matches(t, state)) && allowedKinds.apply(t.getKind()); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } } return new AutoValue_PlaceholderMethod( StringName.of(name), returnType, parameters, new PlaceholderMatcher(), ImmutableClassToInstanceMap.<Annotation, Annotation>copyOf(annotations)); }
@Test public void exhaustive() throws ReflectiveOperationException { Field f = ArbitraryInstances.class.getDeclaredField("DEFAULTS"); f.setAccessible(true); ClassToInstanceMap<?> actual = (ClassToInstanceMap<?>) f.get(null); assertThat(UnnecessarySetDefault.DEFAULTS.keySet()) .containsAnyIn( actual.keySet().stream().map(Class::getCanonicalName).collect(toImmutableList())); }
@Override public Connector create(final String connectorId, Map<String, String> requiredConfig) { checkNotNull(requiredConfig, "requiredConfig is null"); checkNotNull(optionalConfig, "optionalConfig is null"); try { // // A plugin is not required to use Guice; it is just very convenient // Bootstrap app = new Bootstrap(new JsonModule(), new ExampleModule(connectorId)); // // Injector injector = app.strictConfig().doNotInitializeLogging() // .setRequiredConfigurationProperties(requiredConfig) // .setOptionalConfigurationProperties(optionalConfig).initialize(); ClassToInstanceMap<Object> services = ImmutableClassToInstanceMap.builder() .put(ConnectorMetadata.class, new CloudataConnectorMetadata(connectorId, store)) .put(ConnectorSplitManager.class, new CloudataSplitManager(nodeManager, connectorId)) .put(ConnectorRecordSetProvider.class, new CloudataConnectorRecordSetProvider()) .put(ConnectorHandleResolver.class, new CloudataConnectorHandleResolver()).build(); CloudataConnector connector = new CloudataConnector(store, services); connectors.put(connectorId, connector); return connector; } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public void storeValue(Map<DataKey<?>, ? super Object> map, ClassToInstanceMap<FieldDataSource<?>> context) { R rawValue = field.getValue(context); if (eagerTransform) { map.put(this, formatRawValue(rawValue)); } else { map.put(this, rawValue); } }
public <T extends ContentItem> ImmutableList<T> fromResponse(QueryResponse response, ContentItem.Schema<T> forcedSchema, FieldSet... additionalFields) { ClassToInstanceMap<FieldDataSource<?>> context = MutableClassToInstanceMap.create(); context.put(QueryResponseDataSource.class, new QueryResponseDataSource(response)); Builder<T> builder = ImmutableList.builder(); for(SolrDocument doc : response.getResults()) { context.put(SolrDocDataSource.class, new SolrDocDataSource(doc)); context.put(SchemaDataSource.class, new SchemaDataSource(forcedSchema)); builder.add(buildItem(forcedSchema, context, additionalFields)); } return builder.build(); }
public <T extends ContentItem> ImmutableList<T> fromDocumentList(SolrDocumentList list, ContentItem.Schema<T> forcedSchema, FieldSet... additionalFields) { ClassToInstanceMap<FieldDataSource<?>> context = MutableClassToInstanceMap.create(); Builder<T> builder = ImmutableList.builder(); for(SolrDocument doc : list) { context.put(SolrDocDataSource.class, new SolrDocDataSource(doc)); context.put(SchemaDataSource.class, new SchemaDataSource(forcedSchema)); builder.add(buildItem(forcedSchema, context, additionalFields)); } return builder.build(); }
public ImmutableList<ContentItem> fromResponse(QueryResponse response, FieldSet... additionalFields) { ClassToInstanceMap<FieldDataSource<?>> context = MutableClassToInstanceMap.create(); context.put(QueryResponseDataSource.class, new QueryResponseDataSource(response)); Builder<ContentItem> builder = ImmutableList.builder(); for(SolrDocument doc : response.getResults()) { ContentItem.Schema<?> schema = getSchemaForDocument(doc); context.put(SchemaDataSource.class, new SchemaDataSource(schema)); context.put(SolrDocDataSource.class, new SolrDocDataSource(doc)); builder.add(buildItem(schema, context, additionalFields)); } return builder.build(); }
public ImmutableList<ContentItem> fromDocumentList(SolrDocumentList list, FieldSet... additionalFields) { ClassToInstanceMap<FieldDataSource<?>> context = MutableClassToInstanceMap.create(); Builder<ContentItem> builder = ImmutableList.builder(); for(SolrDocument doc : list) { ContentItem.Schema<?> schema = getSchemaForDocument(doc); context.put(SchemaDataSource.class, new SchemaDataSource(schema)); context.put(SolrDocDataSource.class, new SolrDocDataSource(doc)); builder.add(buildItem(schema, context, additionalFields)); } return builder.build(); }
@Override public String getValue(ClassToInstanceMap<FieldDataSource<?>> context) { List<String> highlights = getHighlights(context); if (highlights == null || highlights.size() == 0) { return null; } return highlights.get(0); }
public void testGet_collections() { assertEquals(ImmutableSet.of().iterator(), ArbitraryInstances.get(Iterator.class)); assertFalse(ArbitraryInstances.get(PeekingIterator.class).hasNext()); assertFalse(ArbitraryInstances.get(ListIterator.class).hasNext()); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Iterable.class)); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Set.class)); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(ImmutableSet.class)); assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(SortedSet.class)); assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(ImmutableSortedSet.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(Collection.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableCollection.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(List.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableList.class)); assertEquals(ImmutableMap.of(), ArbitraryInstances.get(Map.class)); assertEquals(ImmutableMap.of(), ArbitraryInstances.get(ImmutableMap.class)); assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(SortedMap.class)); assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(ImmutableSortedMap.class)); assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(Multiset.class)); assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(ImmutableMultiset.class)); assertTrue(ArbitraryInstances.get(SortedMultiset.class).isEmpty()); assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(Multimap.class)); assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(ImmutableMultimap.class)); assertTrue(ArbitraryInstances.get(SortedSetMultimap.class).isEmpty()); assertEquals(ImmutableTable.of(), ArbitraryInstances.get(Table.class)); assertEquals(ImmutableTable.of(), ArbitraryInstances.get(ImmutableTable.class)); assertTrue(ArbitraryInstances.get(RowSortedTable.class).isEmpty()); assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(BiMap.class)); assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(ImmutableBiMap.class)); assertTrue(ArbitraryInstances.get(ImmutableClassToInstanceMap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ClassToInstanceMap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ListMultimap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ImmutableListMultimap.class).isEmpty()); assertTrue(ArbitraryInstances.get(SetMultimap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ImmutableSetMultimap.class).isEmpty()); assertTrue(ArbitraryInstances.get(MapDifference.class).areEqual()); assertTrue(ArbitraryInstances.get(SortedMapDifference.class).areEqual()); assertEquals(Range.all(), ArbitraryInstances.get(Range.class)); assertTrue(ArbitraryInstances.get(NavigableSet.class).isEmpty()); assertTrue(ArbitraryInstances.get(NavigableMap.class).isEmpty()); assertTrue(ArbitraryInstances.get(LinkedList.class).isEmpty()); assertTrue(ArbitraryInstances.get(Deque.class).isEmpty()); assertTrue(ArbitraryInstances.get(Queue.class).isEmpty()); assertTrue(ArbitraryInstances.get(PriorityQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(BitSet.class).isEmpty()); assertTrue(ArbitraryInstances.get(TreeSet.class).isEmpty()); assertTrue(ArbitraryInstances.get(TreeMap.class).isEmpty()); assertFreshInstanceReturned( LinkedList.class, Deque.class, Queue.class, PriorityQueue.class, BitSet.class, TreeSet.class, TreeMap.class); }
public BrokerImpl(final DOMRpcRouter router,final ClassToInstanceMap<BrokerService> services) { this(router, router, services); }
public BrokerImpl(final DOMRpcService rpcService, final DOMRpcProviderService rpcProvider, final ClassToInstanceMap<BrokerService> services) { this.rpcService = Preconditions.checkNotNull(rpcService, "DOMRpcService must not be null"); this.rpcProvider = Preconditions.checkNotNull(rpcProvider, "DOMRpcProviderService must not be null"); this.services = ImmutableClassToInstanceMap.copyOf(services); }
public static final SimpleDOMMountPoint create(final YangInstanceIdentifier identifier, final ClassToInstanceMap<DOMService> services, final SchemaContext ctx) { return new SimpleDOMMountPoint(identifier, services, ctx); }
private SimpleDOMMountPoint(final YangInstanceIdentifier identifier, final ClassToInstanceMap<DOMService> services, final SchemaContext ctx) { this.identifier = identifier; this.services = ImmutableClassToInstanceMap.copyOf(services); this.schemaContext = ctx; }
public static ConsumerContext createConsumerContext(BindingAwareConsumer consumer, ClassToInstanceMap<BindingAwareService> serviceProvider) { checkNotNull(consumer,"Consumer should not be null"); checkNotNull(serviceProvider,"Service map should not be null"); return new SingleConsumerContextImpl(serviceProvider); }
public static ProviderContext createProviderContext(BindingAwareProvider provider, ClassToInstanceMap<BindingAwareService> serviceProvider) { checkNotNull(provider,"Provider should not be null"); checkNotNull(serviceProvider,"Service map should not be null"); return new SingleProviderContextImpl(serviceProvider); }
public static ConsumerContext createConsumerContextAndInitialize(BindingAwareConsumer consumer, ClassToInstanceMap<BindingAwareService> serviceProvider) { ConsumerContext context = createConsumerContext(consumer, serviceProvider); consumer.onSessionInitialized(context); return context; }
public static ProviderContext createProviderContextAndInitialize(BindingAwareProvider provider, ClassToInstanceMap<BindingAwareService> serviceProvider) { ProviderContext context = createProviderContext(provider, serviceProvider); provider.onSessionInitiated(context); return context; }
public SingleConsumerContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) { this.alreadyRetrievedServices = MutableClassToInstanceMap.create(); this.serviceProvider = serviceProvider; }
public SingleProviderContextImpl(ClassToInstanceMap<BindingAwareService> serviceProvider) { super(serviceProvider); }
@Override protected final T createInstance(final ClassToInstanceMap<DOMService> delegates) { Preconditions.checkState(codec != null); return createInstance(codec,delegates); }
@Override protected DataBroker createInstance(final BindingToNormalizedNodeCodec codec, final ClassToInstanceMap<DOMService> delegates) { final DOMDataBroker domDataBroker = delegates.getInstance(DOMDataBroker.class); return new BindingDOMDataBrokerAdapter(domDataBroker, codec); }
@Override protected RpcConsumerRegistry createInstance(final BindingToNormalizedNodeCodec codec, final ClassToInstanceMap<DOMService> delegates) { final DOMRpcService domRpc = delegates.getInstance(DOMRpcService.class); return new BindingDOMRpcServiceAdapter(domRpc , codec); }
@Override protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec, final ClassToInstanceMap<DOMService> delegates) { final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class); return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish); }
@Override protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec, final ClassToInstanceMap<DOMService> delegates) { final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class); return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification); }
@Override public java.lang.AutoCloseable createInstance() { // The services are provided via blueprint so retrieve then from the OSGi service registry for // backwards compatibility. final List<AutoCloseable> closeables = new ArrayList<>(); DOMNotificationService domNotificationService = newTracker( DOMNotificationService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES); DOMNotificationPublishService domNotificationPublishService = newTracker( DOMNotificationPublishService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES); DOMRpcService domRpcService = newTracker( DOMRpcService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES); DOMRpcProviderService domRpcProvider = newTracker( DOMRpcProviderService.class, closeables).waitForService(WaitingServiceTracker.FIVE_MINUTES); DOMMountPointService mountService = newTracker(DOMMountPointService.class, closeables). waitForService(WaitingServiceTracker.FIVE_MINUTES); SchemaService globalSchemaService = newTracker(SchemaService.class, closeables). waitForService(WaitingServiceTracker.FIVE_MINUTES); final DOMDataBroker dataBroker = getAsyncDataBrokerDependency(); final ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create(); services.putInstance(DOMNotificationService.class, domNotificationService); services.putInstance(DOMNotificationPublishService.class, domNotificationPublishService); final SchemaService schemaService = getSchemaServiceImpl(globalSchemaService); services.putInstance(SchemaService.class, schemaService); services.putInstance(DOMDataBroker.class, dataBroker); services.putInstance(DOMRpcService.class, domRpcService); services.putInstance(DOMRpcProviderService.class, domRpcProvider); services.putInstance(DOMMountPointService.class, mountService); BrokerImpl broker = new BrokerImpl(domRpcService, domRpcProvider, services); broker.setDeactivator(() -> { for (AutoCloseable ac : closeables) { try { ac.close(); } catch (Exception e) { LOG.warn("Exception while closing {}", ac, e); } } }); return broker; }
@BeforeClass public void init() throws IOException { //builds graph, initializing mocks ClassToInstanceMap<Object> mocks = MockUtils.mocks(MOCK_CLASSES); initConfigSource(mocks.getInstance(ConfigSource.class)); initConventionSource(mocks.getInstance(ConventionSource.class)); initSecuritySource(mocks.getInstance(SecuritySource.class)); initRegionSource(mocks.getInstance(RegionSource.class)); mocks.putInstance(RootFinderConfiguration.class, ROOT_FINDER_CONFIG); mocks.putInstance(HolidaySource.class, new WeekendHolidaySource()); mocks.putInstance(LegalEntitySource.class, mock(LegalEntitySource.class)); mocks.putInstance(ConventionBundleSource.class, mock(ConventionBundleSource.class)); ComponentMap components = ComponentMap.of(mocks); FunctionModelConfig config = config( arguments( function(DefaultDiscountingMulticurveBundleFn.class, argument("impliedCurveNames", StringSet.of("Implied Deposit Curve KRW"))), function(DefaultCurveNodeConverterFn.class, argument("timeSeriesDuration", RetrievalPeriod.of(Period.ofDays(1))))), implementations(CurveDefinitionFn.class, DefaultCurveDefinitionFn.class, CurveSpecificationFn.class, DefaultCurveSpecificationFn.class, CurveSpecificationMarketDataFn.class, DefaultCurveSpecificationMarketDataFn.class, CurveNodeConverterFn.class, DefaultCurveNodeConverterFn.class, FXMatrixFn.class, DefaultFXMatrixFn.class, DiscountingMulticurveBundleFn.class, DefaultDiscountingMulticurveBundleFn.class, DiscountingMulticurveBundleResolverFn.class, DefaultDiscountingMulticurveBundleResolverFn.class, MarketDataFn.class, DefaultMarketDataFn.class)); _multicurveBundleFn = FunctionModel.build(DiscountingMulticurveBundleResolverFn.class, config, components); ZonedDateTime valuationDate = ZonedDateTime.of(2014, 1, 10, 11, 0, 0, 0, ZoneId.of("America/Chicago")); MarketDataBundle marketDataBundle = MarketDataResourcesLoader.getPreloadedBundle( "/regression/curve_testing/usdMarketQuotes.properties", "Ticker"); _environment = new SimpleEnvironment(valuationDate, marketDataBundle); VersionCorrectionProvider vcProvider = new TestVersionCorrectionProvider(); ServiceContext serviceContext = ServiceContext.of(mocks).with(VersionCorrectionProvider.class, vcProvider); ThreadLocalServiceContext.init(serviceContext); _usdDiscountingCCC = createUSDCurveConstructionConfig(); }