public static <V> MutableSet<V> copyOf(@Nullable Iterator<? extends V> elements) { if (elements == null || !elements.hasNext()) { return of(); } return new MutableSet.Builder<V>().addAll(elements).build(); }
.addAll(referencedLocs) .removeAll(alreadyReferencedLocations) .removeAll(result)
@SuppressWarnings("unchecked") protected <T extends Entity> T createEntityProxy(Iterable<Class<?>> interfaces, T entity) { // We don't especially want the proxy to have to implement EntityLocal, // but required by how AbstractEntity.parent is used (e.g. parent.getAllConfig). // However within EntityProxyImpl we place add'l guards to prevent read-only access to such methods Set<Class<?>> allInterfaces = MutableSet.<Class<?>>builder() .add(EntityProxy.class, Entity.class, EntityLocal.class, EntityInternal.class) .addAll(interfaces) .build(); // TODO OSGi strangeness! The classloader obtained from the type should be enough. // If an OSGi class loader, it should delegate to find things like Entity.class etc. // However, we get errors such as: // NoClassDefFoundError: org.apache.brooklyn.api.sensor.AttributeSensor not found by ....brooklyn-test-osgi-entities // Building our own aggregating class loader gets around this. // But we really should not have to do this! What are the consequences? // // The reason for the error is that the proxy tries to load all classes // referenced from the entity and its interfaces with the single passed loader // while a normal class loading would nest the class loaders (loading interfaces' // references with their own class loaders which in our case are different). AggregateClassLoader aggregateClassLoader = classLoaderCache.getClassLoaderForProxy(entity.getClass(), allInterfaces); return (T) java.lang.reflect.Proxy.newProxyInstance( aggregateClassLoader, allInterfaces.toArray(new Class[allInterfaces.size()]), new EntityProxyImpl(entity)); }
@Test public void testUpdateCalledWithAddressesOfNewChildren() { // First child cluster.resize(1); Entity child = Iterables.getOnlyElement(cluster.getMembers()); List<Collection<String>> u = Lists.newArrayList(controller.getUpdates()); assertTrue(u.isEmpty(), "expected empty list but got "+u); child.sensors().set(WebServerEntity.HTTP_PORT, 1234); child.sensors().set(Startable.SERVICE_UP, true); assertEventuallyAddressesMatchCluster(); // Second child cluster.resize(2); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEquals(cluster.getMembers().size(), 2); }}); Entity child2 = Iterables.getOnlyElement(MutableSet.<Entity>builder().addAll(cluster.getMembers()).remove(child).build()); child2.sensors().set(WebServerEntity.HTTP_PORT, 1234); child2.sensors().set(Startable.SERVICE_UP, true); assertEventuallyAddressesMatchCluster(); // And remove all children; expect all addresses to go away cluster.resize(0); assertEventuallyAddressesMatchCluster(); }
@Test public void testRemoveSensors() throws Exception { entity.getMutableEntityType().removeSensor(SENSOR_ADDED.getName()); entity.getMutableEntityType().removeSensor(POLICY_ADDED.getName()); assertEquals(entity.getEntityType().getSensors(), MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).remove(POLICY_ADDED).build().asUnmodifiable()); final RecordingSensorEventListener<?> listener = this.listener; Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEquals(Iterables.size(listener.getEvents()), 2); } }); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, SENSOR_ADDED), BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, POLICY_ADDED))); }
public void testBuilderAddArray() throws Exception { Set<Object> vals = MutableSet.builder().addAll(new Object[] {1,2,3}).build(); Assert.assertEquals(vals, ImmutableSet.of(1,2,3)); }
@Test public void testRemoveSensor() throws Exception { entity.getMutableEntityType().removeSensor(SENSOR_ADDED); assertEquals(entity.getEntityType().getSensors(), MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).build().asUnmodifiable()); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, SENSOR_ADDED))); }
@Test public void testRemoveConfigKey() throws Exception { entity.getMutableEntityType().removeConfigKey(ConfigEntityForTesting.INSTALL_UNIQUE_LABEL); assertEquals(entity.getEntityType().getConfigKeys(), MutableSet.builder().addAll(DEFAULT_CONFIG_KEYS).remove(ConfigEntityForTesting.INSTALL_UNIQUE_LABEL).build().asUnmodifiable()); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(AbstractEntity.CONFIG_KEY_REMOVED, entity, ConfigEntityForTesting.INSTALL_UNIQUE_LABEL))); }
public void testBuilderAddIterator() throws Exception { Set<Object> vals = MutableSet.builder().addAll(ImmutableSet.of(1,2).iterator()).build(); Assert.assertEquals(vals, ImmutableSet.of(1,2)); }