List of usage examples for java.util.concurrent CopyOnWriteArrayList add
public void add(int index, E element)
From source file:org.github.gitswarm.GitSwarm.java
/** * TODO This could be made to look a lot better. *///from ww w. j av a 2 s.c o m private void drawPopular() { CopyOnWriteArrayList<FileNode> al = new CopyOnWriteArrayList<>(); noStroke(); textFont(font); textAlign(RIGHT, TOP); fill(fontColor, 200); text("Popular Nodes (touches):", width - 120, 0); for (FileNode fn : nodes.values()) { if (fn.qualifies()) { // Insertion Sort if (al.size() > 0) { int j = 0; for (; j < al.size(); j++) { if (fn.compareTo(al.get(j)) > 0) { break; } } al.add(j, fn); } else { al.add(fn); } } } int i = 1; ListIterator<FileNode> it = al.listIterator(); while (it.hasNext()) { FileNode n = it.next(); // Limit to the top 10. if (i <= 10) { text(n.getName() + " (" + n.getTouches() + ")", width - 100, 10 * i++); } else if (i > 10) { break; } } }
From source file:android.bus.EventBus.java
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) { Class<?> eventType = subscriberMethod.eventType; CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType); Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority); if (subscriptions == null) { subscriptions = new CopyOnWriteArrayList<Subscription>(); subscriptionsByEventType.put(eventType, subscriptions); }//from w w w . j a v a 2 s . c om // FIXME ??? // else { // if (subscriptions.contains(newSubscription)) { // throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event " // + eventType); // } // } // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again) // subscriberMethod.method.setAccessible(true); int size = subscriptions.size(); for (int i = 0; i <= size; i++) { if (i == size || newSubscription.priority > subscriptions.get(i).priority) { subscriptions.add(i, newSubscription); break; } } List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber); if (subscribedEvents == null) { subscribedEvents = new ArrayList<Class<?>>(); typesBySubscriber.put(subscriber, subscribedEvents); } subscribedEvents.add(eventType); if (sticky) { if (eventInheritance) { // Existing sticky events of all subclasses of eventType have to be considered. // Note: Iterating over all events may be inefficient with lots of sticky events, // thus data structure should be changed to allow a more efficient lookup // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>). Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet(); for (Map.Entry<Class<?>, Object> entry : entries) { Class<?> candidateEventType = entry.getKey(); if (eventType.isAssignableFrom(candidateEventType)) { Object stickyEvent = entry.getValue(); checkPostStickyEventToSubscription(newSubscription, stickyEvent); } } } else { Object stickyEvent = stickyEvents.get(eventType); checkPostStickyEventToSubscription(newSubscription, stickyEvent); } } }
From source file:codeswarm.code_swarm.java
/** * TODO This could be made to look a lot better. *///from w w w .jav a 2s. com public void drawPopular() { CopyOnWriteArrayList<FileNode> al = new CopyOnWriteArrayList<FileNode>(); noStroke(); textFont(font); textAlign(RIGHT, TOP); fill(255, 200); text("Popular Nodes (touches):", width - 120, 0); for (int i = 0; i < nodes.size(); i++) { FileNode fn = nodes.get(i); if (fn.qualifies()) { // Insertion Sort if (al.size() > 0) { int j = 0; for (; j < al.size(); j++) { if (fn.compareTo(al.get(j)) <= 0) { continue; } else { break; } } al.add(j, fn); } else { al.add(fn); } } } int i = 1; ListIterator<FileNode> it = al.listIterator(); while (it.hasNext()) { FileNode n = it.next(); // Limit to the top 10. if (i <= 10) { text(n.getName() + " (" + n.getTouches() + ")", width - 100, 10 * i++); } else if (i > 10) { break; } } }