public PrimitiveAwareVarArgsTransformer(Constructor<?> constructor) { super(constructor); defaultInstance = (componentType != null) ? defaultPrimitives.getInstance(componentType) : null; }
public void testNull() { try { map.put(null, new Integer(1)); fail(); } catch (NullPointerException expected) { } map.putInstance(Integer.class, null); assertNull(map.get(Integer.class)); assertNull(map.getInstance(Integer.class)); map.put(Long.class, null); assertNull(map.get(Long.class)); assertNull(map.getInstance(Long.class)); }
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> {
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 void testPrimitiveAndWrapper() { assertNull(map.getInstance(int.class)); assertNull(map.getInstance(Integer.class)); assertNull(map.putInstance(int.class, 0)); assertNull(map.putInstance(Integer.class, 1)); assertEquals(2, map.size()); assertEquals(0, (int) map.getInstance(int.class)); assertEquals(1, (int) map.getInstance(Integer.class)); assertEquals(0, (int) map.putInstance(int.class, null)); assertEquals(1, (int) map.putInstance(Integer.class, null)); assertNull(map.getInstance(int.class)); assertNull(map.getInstance(Integer.class)); assertEquals(2, map.size()); } }
public void testConstraint() { /** * We'll give ourselves a pass on testing all the possible ways of breaking the constraint, * because we know that newClassMap() is implemented using ConstrainedMap which is itself * well-tested. A purist would object to this, but what can I say, we're dirty cheaters. */ map.put(Integer.class, new Integer(5)); try { map.put(Double.class, new Long(42)); fail(); } catch (ClassCastException expected) { } // Won't compile: map.put(String.class, "x"); }
public static IInjectorProvider getOrCreateInjectorProvider(TestClass testClass) { InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class); if (injectWith != null) { Class<? extends IInjectorProvider> klass = injectWith.value(); IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass); if (injectorProvider == null) { try { injectorProvider = klass.newInstance(); injectorProviderClassCache.put(klass, injectorProvider); } catch (Exception e) { throwUncheckedException(e); } } return injectorProvider; } return null; }
private synchronized <T extends BindingAwareService> T tryToRetrieveSalService(Class<T> service) { final T potential = alreadyRetrievedServices.getInstance(service); if (potential != null) { return potential; } final T requested = serviceProvider.getInstance(service); if (requested == null) { throw new IllegalArgumentException("Requested service " + service.getName() + " is not available."); } final T retrieved = BindingContextUtils.createContextProxyOrReturnService(service,requested); alreadyRetrievedServices.put(service, retrieved); return retrieved; }
public void testCopyOf_imap_valid() { ImmutableMap<Class<? extends Number>, ? extends Number> in = ImmutableMap.of(Number.class, 0, Double.class, Math.PI); ClassToInstanceMap<Number> map = ImmutableClassToInstanceMap.copyOf(in); assertEquals(2, map.size()); Number zero = map.getInstance(Number.class); assertEquals(0, zero); Double pi = map.getInstance(Double.class); assertEquals(Math.PI, pi, 0.0); }
public Actions build() { Preconditions.checkArgument(actions.size() != 0, "'actions' must contain at least one action."); for (Class<? extends Action> action : actions.keySet()) { Preconditions.checkNotNull(actions.getInstance(action), action.getName() + " cannot have a null entry in actions."); if (actions.containsKey(AddTagAction.class) && actions.containsKey(RemoveTagAction.class)) { TagActionData addTagData = actions.getInstance(AddTagAction.class).getValue(); TagActionData removeTagData = actions.getInstance(RemoveTagAction.class).getValue(); if (actions.containsKey(AppDefinedAction.class) && actions.size() > 1) { List<String> fieldNames = Lists.newArrayList(actions.getInstance(AppDefinedAction.class).getValue().fieldNames()); for (Class<? extends Action> key : actions.keySet()) { if (key == AppDefinedAction.class) { continue; Action a = actions.getInstance(key);
/** * Returns the existing task whose class is exactly a given class (and not a subclass -- this * will always return null for an abstract type). * * @param clazz the class to look up * @return one of this manager's tasks that's an instance of that class and not a subclass, or * null if no such tasks are registered */ public EntityTask getTask(Class<? extends EntityTask> clazz) { return tasksByClass.get(clazz); }
/** * Adds a minion to the squad. * @param minion minion who should join the squad */ public <M extends Minion> void add(M minion) { minions.put(minion.getClass(), minion); Class sc = minion.getClass().getSuperclass(); while (sc != null && !Modifier.isAbstract(sc.getModifiers()) && !sc.equals(Minion.class) ) { minions.putInstance(sc, minion); sc = sc.getSuperclass(); } sorted.add(minion); }
/** * Sets a default value that can be used for any parameter of type {@code type}. Returns this * object. */ public <T> NullPointerTester setDefault(Class<T> type, T value) { defaults.putInstance(type, checkNotNull(value)); return this; }
@Override public <T extends IAEStack<T>, C extends IStorageChannel<T>> void registerStorageChannel( Class<C> channel, C factory ) { Preconditions.checkNotNull( channel ); Preconditions.checkNotNull( factory ); Preconditions.checkArgument( channel.isInstance( factory ) ); Preconditions.checkArgument( !this.channels.containsKey( channel ) ); this.channels.putInstance( channel, factory ); }
/** * Parses the arguments and configures the OptionBeans * * @param parser option parser instance * @param args command line arguments * @param checkNonOptions if true then it checks that non options are specified i.e. some NodeStore is * selected * @return optionSet returned from OptionParser */ public OptionSet parseAndConfigure(OptionParser parser, String[] args, boolean checkNonOptions) throws IOException { for (OptionsBeanFactory o : Iterables.concat(oakRunOptions, beanFactories)){ OptionsBean bean = o.newInstance(parser); optionBeans.put(bean.getClass(), bean); } parser.formatHelpWith(new OakHelpFormatter(optionBeans.values(), commandName, summary, connectionString)); optionSet = parser.parse(args); configure(optionSet); checkForHelp(parser); if (checkNonOptions) { checkNonOptions(); } return optionSet; }
private static <T> void setImplementation(Class<T> type, Class<? extends T> implementation) { checkArgument(type != implementation, "Don't register %s to itself!", type); checkArgument( !DEFAULTS.containsKey(type), "A default value was already registered for %s", type); checkArgument( implementations.put(type, implementation) == null, "Implementation for %s was already registered", type); }
@Override public Object entityFor(Class<?> clazz, Node node, Supplier<Object> newInstance) { ClassToInstanceMap<Object> typeToEntity = nodeToTypeToEntity.computeIfAbsent(node, (x) -> MutableClassToInstanceMap.create()); Object result = newInstance != null ? typeToEntity.computeIfAbsent(clazz, (type) -> newInstance.get()) : typeToEntity.get(clazz); return result; }
void shutdown() { for (Completable component : components.values()) { component.complete(); } components = null; processing = null; round = null; annotations = null; initialized = false; state.remove(); }
public void testPutAndGetInstance() { assertNull(map.putInstance(Integer.class, new Integer(5))); Integer oldValue = map.putInstance(Integer.class, new Integer(7)); assertEquals(5, (int) oldValue); Integer newValue = map.getInstance(Integer.class); assertEquals(7, (int) newValue); // Won't compile: map.putInstance(Double.class, new Long(42)); }
/** * Add the task with this name, or replace it with a new instance if it already exists. * * @param taskName the task name */ public void addTask(String taskName) { EntityTask oldTask = getTask(taskName); if (oldTask != null) { cancel(oldTask); } EntityTask newTask = getNewTask(taskName); tasksByName.put(taskName, newTask); tasksByClass.put(newTask.getClass(), newTask); tasks.add(newTask); } }