Example usage for java.util.concurrent CopyOnWriteArraySet add

List of usage examples for java.util.concurrent CopyOnWriteArraySet add

Introduction

In this page you can find the example usage for java.util.concurrent CopyOnWriteArraySet add.

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:com.devicehive.eventbus.SubscriberRegistry.java

/**
 * Registers subscription and subscriber in registry maps.
 * Performs following steps:/*  w  w  w  . j  a va  2s .  c o m*/
 *  - if subscriber doesn't have any subscriptions in {@link SubscriberRegistry#subscriberSubscriptions} - creates an empty list for him;
 *  - adds subscription into subscriber's list in {@link SubscriberRegistry#subscriberSubscriptions};
 *  - if nobody is subscribed to this subscription in {@link SubscriberRegistry#subscriptions} - initializes the list;
 *  - adds subscriber to this subscription's list in {@link SubscriberRegistry#subscriptions}
 *
 * @param subscriber - subscriber
 * @param subscription - subscription to subscribe to
 */
void register(Subscriber subscriber, Subscription subscription) {
    CopyOnWriteArraySet<Subscription> subscriptions = subscriberSubscriptions.get(subscriber.getId());
    if (subscriptions == null) {
        //initialize list in a thread safe manner
        CopyOnWriteArraySet<Subscription> newSet = new CopyOnWriteArraySet<>();
        subscriptions = firstNonNull(subscriberSubscriptions.putIfAbsent(subscriber.getId(), newSet), newSet);
    }
    subscriptions.add(subscription);

    CopyOnWriteArraySet<Subscriber> subscribers = this.subscriptions.get(subscription);
    if (subscribers == null) {
        //initialize list in a thread safe manner
        CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet<>();
        subscribers = firstNonNull(this.subscriptions.putIfAbsent(subscription, newSet), newSet);
    }
    subscribers.add(subscriber);
}

From source file:com.linkedin.helix.store.file.FilePropertyStore.java

@Override
public void subscribeForPropertyChange(String prefix, PropertyChangeListener<T> listener)
        throws PropertyStoreException {
    if (null != listener) {
        String path = getPath(prefix);
        synchronized (_fileChangeListeners) {
            CopyOnWriteArraySet<PropertyChangeListener<T>> listeners = _fileChangeListeners.get(path);
            if (listeners == null) {
                listeners = new CopyOnWriteArraySet<PropertyChangeListener<T>>();
                _fileChangeListeners.put(path, listeners);
            }//  w  w w.j  a  v  a  2  s  .c o m
            listeners.add(listener);
        }
    }

}

From source file:com.cburch.logisim.circuit.CircuitWires.java

void propagate(CircuitState circState, Set<Location> points) {
    BundleMap map = getBundleMap();/*  w  ww. j a  v a2  s  .c o  m*/
    CopyOnWriteArraySet<WireThread> dirtyThreads = new CopyOnWriteArraySet<WireThread>(); // affected threads

    // get state, or create a new one if current state is outdated
    State s = circState.getWireData();
    if (s == null || s.bundleMap != map) {
        // if it is outdated, we need to compute for all threads
        s = new State(map);
        for (WireBundle b : map.getBundles()) {
            WireThread[] th = b.threads;
            if (b.isValid() && th != null) {
                for (WireThread t : th) {
                    dirtyThreads.add(t);
                }
            }
        }
        circState.setWireData(s);
    }

    // determine affected threads, and set values for unwired points
    for (Location p : points) {
        WireBundle pb = map.getBundleAt(p);
        if (pb == null) { // point is not wired
            circState.setValueByWire(p, circState.getComponentOutputAt(p));
        } else {
            WireThread[] th = pb.threads;
            if (!pb.isValid() || th == null) {
                // immediately propagate NILs across invalid bundles
                CopyOnWriteArraySet<Location> pbPoints = pb.points;
                if (pbPoints == null) {
                    circState.setValueByWire(p, Value.NIL);
                } else {
                    for (Location loc2 : pbPoints) {
                        circState.setValueByWire(loc2, Value.NIL);
                    }
                }
            } else {
                for (WireThread t : th) {
                    dirtyThreads.add(t);
                }
            }
        }
    }

    if (dirtyThreads.isEmpty())
        return;

    // determine values of affected threads
    HashSet<ThreadBundle> bundles = new HashSet<ThreadBundle>();
    for (WireThread t : dirtyThreads) {
        Value v = getThreadValue(circState, t);
        s.thr_values.put(t, v);
        bundles.addAll(t.getBundles());
    }

    // now propagate values through circuit
    for (ThreadBundle tb : bundles) {
        WireBundle b = tb.b;

        Value bv = null;
        if (!b.isValid() || b.threads == null) {
            ; // do nothing
        } else if (b.threads.length == 1) {
            bv = s.thr_values.get(b.threads[0]);
        } else {
            Value[] tvs = new Value[b.threads.length];
            boolean tvs_valid = true;
            for (int i = 0; i < tvs.length; i++) {
                Value tv = s.thr_values.get(b.threads[i]);
                if (tv == null) {
                    tvs_valid = false;
                    break;
                }
                tvs[i] = tv;
            }
            if (tvs_valid)
                bv = Value.create(tvs);
        }

        if (bv != null) {
            for (Location p : b.points) {
                circState.setValueByWire(p, bv);
            }
        }
    }
}

From source file:org.apache.bval.jsr.xml.ValidationParser.java

public static ValidationParser processValidationConfig(final String file, final ConfigurationImpl targetConfig,
        final boolean ignoreXml) {
    final ValidationParser parser = new ValidationParser();

    if (!ignoreXml) {
        parser.xmlConfig = parseXmlConfig(file);
    }//from www  .  j a v a 2s .c  o m

    if (parser.xmlConfig != null) {
        if (parser.xmlConfig.getExecutableValidation() == null) {
            final ExecutableValidationType value = new ExecutableValidationType();
            value.setEnabled(true);

            final DefaultValidatedExecutableTypesType defaultValidatedExecutableTypes = new DefaultValidatedExecutableTypesType();
            value.setDefaultValidatedExecutableTypes(defaultValidatedExecutableTypes);
            defaultValidatedExecutableTypes.getExecutableType().add(ExecutableType.CONSTRUCTORS);
            defaultValidatedExecutableTypes.getExecutableType().add(ExecutableType.NON_GETTER_METHODS);

            parser.xmlConfig.setExecutableValidation(value);
        }

        applySimpleConfig(parser.xmlConfig, targetConfig);

        parser.bootstrap = new BootstrapConfigurationImpl(parser.xmlConfig.getDefaultProvider(),
                parser.xmlConfig.getConstraintValidatorFactory(), parser.xmlConfig.getMessageInterpolator(),
                parser.xmlConfig.getTraversableResolver(), parser.xmlConfig.getParameterNameProvider(),
                new CopyOnWriteArraySet<String>(parser.xmlConfig.getConstraintMapping()),
                parser.xmlConfig.getExecutableValidation().getEnabled(),
                new CopyOnWriteArraySet<ExecutableType>(targetConfig.getExecutableValidation()),
                toMap(parser.xmlConfig.getProperty()));
        return parser;
    } else { // default config
        final CopyOnWriteArraySet<ExecutableType> executableTypes = new CopyOnWriteArraySet<ExecutableType>();
        executableTypes.add(ExecutableType.CONSTRUCTORS);
        executableTypes.add(ExecutableType.NON_GETTER_METHODS);

        parser.bootstrap = new BootstrapConfigurationImpl(null, null, null, null, null,
                new CopyOnWriteArraySet<String>(), true, executableTypes, new HashMap<String, String>());

        targetConfig.setExecutableValidation(executableTypes);
    }

    return parser;
}