Java Code Examples for java.util.concurrent.CopyOnWriteArraySet#add()

The following examples show how to use java.util.concurrent.CopyOnWriteArraySet#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TelemetryClientTest.java    From mapbox-events-android with MIT License 6 votes vote down vote up
@Test
public void checksDebugLoggingEnabledAttachment() throws Exception {
  Context mockedContext = getMockedContext();
  MapboxTelemetry.applicationContext = mockedContext;
  TelemetryClientSettings clientSettings = provideDefaultTelemetryClientSettings(mockedContext);
  Logger mockedLogger = mock(Logger.class);
  String anyUserAgent = "anyUserAgent";
  TelemetryClient telemetryClient = new TelemetryClient("anyAccessToken", anyUserAgent, anyUserAgent,
    clientSettings, mockedLogger, mock(CertificateBlacklist.class), false);
  telemetryClient.updateDebugLoggingEnabled(true);

  AttachmentListener attachmentListener = mock(AttachmentListener.class);
  CopyOnWriteArraySet<AttachmentListener> attachmentListeners = new CopyOnWriteArraySet<>();
  attachmentListeners.add(attachmentListener);

  saveFile(mockedContext, "test");
  telemetryClient.sendAttachment(createAttachment("test"), attachmentListeners);

  verify(mockedLogger, times(1))
    .debug(eq("TelemetryClient"), contains(" with 1 event(s) (user agent: anyUserAgent) with payload:"));
}
 
Example 2
Source File: ZKClient.java    From zkclient with Apache License 2.0 6 votes vote down vote up
/**
 * 监听子节点变化,包括数量的变化,和子节点内容的变化。
 * 这里的内容的变化指的版本的变化,例如修改的值与原来的值一样,也会被视为数据已改变。
 * @param path
 * @param listener
 * @return 
 * @return List<String>
 */
public List<String> listenChildDataChanges(String path, ZKChildDataListener listener) {
    LOG.debug("Listen child count and data  changes ["+path+"--"+listener+"] ");
    synchronized (childListenerMap) {
        CopyOnWriteArraySet<ZKListener> listeners = childListenerMap.get(path);
        if (listeners == null) {
            listeners = new CopyOnWriteArraySet<ZKListener>();
            childListenerMap.put(path, listeners);
        }
        listeners.add(listener);
    }
    
   List<String> children =  watchForChilds(path);
        //监听子节点数据
   listenNewChildPathWithData(path,children);
    return children;
}
 
Example 3
Source File: AggregatorPeer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void init()
{
	execution = FunctionService.onMembers(region.getCache().getDistributedSystem());
	int totalNumBuckets = region.getPartitionAttributes().getTotalNumBuckets();
	routingKeySet = new CopyOnWriteArraySet();
	for (int i = 0; i < totalNumBuckets; i++) {
		routingKeySet.add(i);
	}
}
 
Example 4
Source File: CopyOnWriteArraySetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray throws an ArrayStoreException when the given array can
 * not store the objects inside the set
 */
public void testToArray_ArrayStoreException() {
    CopyOnWriteArraySet c = new CopyOnWriteArraySet();
    c.add("zfasdfsdf");
    c.add("asdadasd");
    try {
        c.toArray(new Long[5]);
        shouldThrow();
    } catch (ArrayStoreException success) {}
}
 
Example 5
Source File: CopyOnWriteArraySetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
static CopyOnWriteArraySet populatedSet(Integer[] elements) {
    CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
    assertTrue(a.isEmpty());
    for (int i = 0; i < elements.length; i++)
        a.add(elements[i]);
    assertFalse(a.isEmpty());
    assertEquals(elements.length, a.size());
    return a;
}
 
Example 6
Source File: CopyOnWriteArraySetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
static CopyOnWriteArraySet<Integer> populatedSet(int n) {
    CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
    assertTrue(a.isEmpty());
    for (int i = 0; i < n; i++)
        a.add(i);
    assertEquals(n == 0, a.isEmpty());
    assertEquals(n, a.size());
    return a;
}
 
Example 7
Source File: AggregatorPeer.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void init()
{
	execution = FunctionService.onMembers(region.getCache().getDistributedSystem());
	int totalNumBuckets = region.getPartitionAttributes().getTotalNumBuckets();
	routingKeySet = new CopyOnWriteArraySet();
	for (int i = 0; i < totalNumBuckets; i++) {
		routingKeySet.add(i);
	}
}
 
Example 8
Source File: ZKClient.java    From zkclient with Apache License 2.0 5 votes vote down vote up
/**
 * 对节点进行监听,包括节点的创建、删除、数据改变
 * @param path 监听的路径
 * @param listener  监听器
 * @return void
 */
public void listenNodeChanges(String path, ZKListener nodeListener) {
    LOG.debug("listen Node ["+path+"--"+nodeListener+"] ");
    CopyOnWriteArraySet<ZKListener> listeners;
    synchronized (nodeListenerMap) {
        listeners = nodeListenerMap.get(path);
        if (listeners == null) {
            listeners = new CopyOnWriteArraySet<ZKListener>();
            nodeListenerMap.put(path, listeners);
        }
        listeners.add(nodeListener);
    }
    watchForData(path);
    LOG.debug("listened path: " + path);
}
 
Example 9
Source File: ZKClient.java    From zkclient with Apache License 2.0 5 votes vote down vote up
/**
 * 监听子节点的变化,只是监听数量的变化。
 * @param path 父路径
 * @param listener 监听器
 * @return 
 * @return List<String> 返回当前子节点路径集合
 */
public List<String> listenChildCountChanges(String path, ZKChildCountListener listener) {
    LOG.debug("Listen child count changes ["+path+"--"+listener+"] ");
    synchronized (childListenerMap) {
        CopyOnWriteArraySet<ZKListener> listeners = childListenerMap.get(path);
        if (listeners == null) {
            listeners = new CopyOnWriteArraySet<ZKListener>();
            childListenerMap.put(path, listeners);
        }
        listeners.add(listener);
    }
    
    return watchForChilds(path);
}
 
Example 10
Source File: DefaultEventBus.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * @see EventBus#register(Class, EventSubscriber)
 */
@Override
public void register(Class<? extends Event> eventClass, EventSubscriber eventSubscriber) {
    CopyOnWriteArraySet<EventSubscriber> set = eventSubscriberMap.get(eventClass);
    if (set == null) {
        set = new CopyOnWriteArraySet<EventSubscriber>();
        CopyOnWriteArraySet<EventSubscriber> old = eventSubscriberMap.putIfAbsent(eventClass,
            set);
        if (old != null) {
            set = old;
        }
    }
    set.add(eventSubscriber);
    LOGGER.debug("Register subscriber: {} of event: {}.", eventSubscriber, eventClass);
}
 
Example 11
Source File: JvmWinSvcAcctPasswordScrubberServiceImplTest.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    initMocks(this);
    final CopyOnWriteArraySet<String> copyOnWriteArraySet = new CopyOnWriteArraySet<>();
    copyOnWriteArraySet.add("secret");
    when(mockObjectStoreService.getIterable()).thenReturn(copyOnWriteArraySet);
    jvmWinSvcAcctPasswordScrubberService = new JvmWinSvcAcctPasswordScrubberServiceImpl(mockObjectStoreService,
            mockDecryptPassword);
}
 
Example 12
Source File: CopyOnWriteArraySetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray throws an ArrayStoreException when the given array can
 * not store the objects inside the set
 */
public void testToArray_ArrayStoreException() {
    CopyOnWriteArraySet c = new CopyOnWriteArraySet();
    c.add("zfasdfsdf");
    c.add("asdadasd");
    try {
        c.toArray(new Long[5]);
        shouldThrow();
    } catch (ArrayStoreException success) {}
}
 
Example 13
Source File: CopyOnWriteArraySetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static CopyOnWriteArraySet populatedSet(Integer[] elements) {
    CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
    assertTrue(a.isEmpty());
    for (int i = 0; i < elements.length; i++)
        a.add(elements[i]);
    assertFalse(a.isEmpty());
    assertEquals(elements.length, a.size());
    return a;
}
 
Example 14
Source File: CopyOnWriteArraySetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static CopyOnWriteArraySet<Integer> populatedSet(int n) {
    CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
    assertTrue(a.isEmpty());
    for (int i = 0; i < n; i++)
        a.add(i);
    assertEquals(n == 0, a.isEmpty());
    assertEquals(n, a.size());
    return a;
}
 
Example 15
Source File: EventBus.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 注册一个订阅者
 *
 * @param eventClass 事件类型
 * @param subscriber 订阅者
 */
public static void register(Class<? extends Event> eventClass, Subscriber subscriber) {
    CopyOnWriteArraySet<Subscriber> set = SUBSCRIBER_MAP.get(eventClass);
    if (set == null) {
        set = new CopyOnWriteArraySet<Subscriber>();
        CopyOnWriteArraySet<Subscriber> old = SUBSCRIBER_MAP.putIfAbsent(eventClass, set);
        if (old != null) {
            set = old;
        }
    }
    set.add(subscriber);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Register subscriber: {} of event: {}.", subscriber, eventClass);
    }
}
 
Example 16
Source File: EventAdminServiceImpl.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
@Override
public void register(EventHandler eventHandler) {
    CopyOnWriteArraySet<EventHandler> set = SUBSCRIBER_MAP.get(eventHandler.getClass()
        .getClassLoader());
    if (set == null) {
        set = new CopyOnWriteArraySet<>();
        CopyOnWriteArraySet<EventHandler> old = SUBSCRIBER_MAP.putIfAbsent(eventHandler
            .getClass().getClassLoader(), set);
        if (old != null) {
            set = old;
        }
    }
    set.add(eventHandler);
    LOGGER.debug(String.format("Register event handler: %s.", eventHandler));
}
 
Example 17
Source File: DeFiConsumerGroupInfo.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
public boolean registerClientId(final Set<SubscriptionData> subList, final String clientId) {
    boolean update = false;

    HashSet<String> subTopicSet = new HashSet<>();
    for (SubscriptionData sub : subList) {
        subTopicSet.add(sub.getTopic());
        if (clientIdMap.get(sub.getTopic()) == null) {
            update = true;
            CopyOnWriteArraySet<String> clientIdSet = new CopyOnWriteArraySet<>();
            clientIdSet.add(clientId);
            clientIdMap.put(sub.getTopic(), clientIdSet);
            log.info("add clientId {} into {}", clientId, sub.getTopic());
        } else {
            if (!clientIdMap.get(sub.getTopic()).contains(clientId)) {
                update = true;
                clientIdMap.get(sub.getTopic()).add(clientId);
                log.info("add clientId {} into {}", clientId, sub.getTopic());
            }
        }
    }

    Iterator<Map.Entry<String, CopyOnWriteArraySet<String>>> it = clientIdMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, CopyOnWriteArraySet<String>> entry = it.next();
        String topic = entry.getKey();
        if (entry.getValue().contains(clientId)) {
            if (!subTopicSet.contains(topic)) {
                entry.getValue().remove(clientId);
                update = true;
                log.info("remove clientId {} from {}", clientId, topic);
                if (entry.getValue().isEmpty()) {
                    it.remove();
                    log.info("remove clientId, clientId set of {} is empty, remove it.", topic);
                }
            }
        }
    }

    return update;
}
 
Example 18
Source File: CopyOnWriteArraySetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets with equal elements are equal
 */
public void testEquals() {
    CopyOnWriteArraySet a = populatedSet(3);
    CopyOnWriteArraySet b = populatedSet(3);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());
    assertEquals(a.size(), b.size());

    a.add(m1);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
    assertTrue(a.containsAll(b));
    assertFalse(b.containsAll(a));
    b.add(m1);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());

    Object x = a.iterator().next();
    a.remove(x);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
    assertFalse(a.containsAll(b));
    assertTrue(b.containsAll(a));
    a.add(x);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());
    assertEquals(a.size(), b.size());

    CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
    CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
    assertTrue(empty1.equals(empty1));
    assertTrue(empty1.equals(empty2));

    assertFalse(empty1.equals(a));
    assertFalse(a.equals(empty1));

    assertFalse(a.equals(null));
}
 
Example 19
Source File: CopyOnWriteArraySetTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Sets with equal elements are equal
 */
public void testEquals() {
    CopyOnWriteArraySet a = populatedSet(3);
    CopyOnWriteArraySet b = populatedSet(3);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());
    assertEquals(a.size(), b.size());

    a.add(m1);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
    assertTrue(a.containsAll(b));
    assertFalse(b.containsAll(a));
    b.add(m1);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());

    Object x = a.iterator().next();
    a.remove(x);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
    assertFalse(a.containsAll(b));
    assertTrue(b.containsAll(a));
    a.add(x);
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    assertTrue(a.containsAll(b));
    assertTrue(b.containsAll(a));
    assertEquals(a.hashCode(), b.hashCode());
    assertEquals(a.size(), b.size());

    CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
    CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
    assertTrue(empty1.equals(empty1));
    assertTrue(empty1.equals(empty2));

    assertFalse(empty1.equals(a));
    assertFalse(a.equals(empty1));

    assertFalse(a.equals(null));
}
 
Example 20
Source File: HALoadBalancerServlet.java    From database with GNU General Public License v2.0 3 votes vote down vote up
private static void addServlet(final ServletContext servletContext,
        final HALoadBalancerServlet servlet) {

    if (servletContext == null)
        throw new IllegalArgumentException();

    if (servlet == null)
        throw new IllegalArgumentException();

    synchronized(servletContext) {

        @SuppressWarnings("unchecked")
        CopyOnWriteArraySet<HALoadBalancerServlet> servletSet = (CopyOnWriteArraySet<HALoadBalancerServlet>) servletContext
                .getAttribute(BigdataServlet.ATTRIBUTE_LBS_INSTANCES);

        if (servletSet == null) {

            servletContext
                    .setAttribute(
                            BigdataServlet.ATTRIBUTE_LBS_INSTANCES,
                            servletSet = new CopyOnWriteArraySet<HALoadBalancerServlet>());

        }
        
        servletSet.add(servlet);
        
    }
    
}