/** * returns docs with ALL given sentiments. special cases: sentiments can be an array of length 1 * and be "None", in which case all documents with no sentiments are returned. special cases: * sentiments can be an array of length 1 and be "all", in which case all documents with any * sentiments are returned. * * @param captions */ public Collection<Document> getDocsWithSentiments( String sentiments[], Indexer indexer, Collection<Document> docs, int cluster, boolean originalContentOnly, String... captions) { Collection<Document> result = null; // note: multiple sentiments are possible, they are ANDED if (sentiments == null || sentiments.length == 0) return result; Set<Document> docs_set = Util.castOrCloneAsSet(docs); if (sentiments.length == 1 && "all".equalsIgnoreCase(sentiments[0])) return getDocsWithAnyEmotions(indexer, docs_set, originalContentOnly); // note: we'll pass in null for docs, and intersect with the given set of docs later // otherwise we'd just be doing it again and again for each category and lexer Map<String, Collection<Document>> map = getEmotions(indexer, null, false, originalContentOnly, captions); for (int i = 0; i < sentiments.length; i++) { Collection<Document> temp1 = ("None".equalsIgnoreCase(sentiments[i])) ? getDocsWithNoEmotions(indexer, docs_set, originalContentOnly) : map.get(sentiments[i]); if (temp1 == null) { // no matches, just return result = new LinkedHashSet<Document>(); return result; } if (result == null) result = temp1; else result.retainAll(temp1); } // result.retainAll(docs); return Util.setIntersection(result, docs_set); }
private void processElements( Attribute attribute, CssElementType elementType, Map<String, Collection<FileObject>> elements2files) { CharSequence value = attribute.unquotedValue(); if (value == null) { return; } if (value.length() == 0) { return; // ignore empty value } // all files containing the id declaration Collection<FileObject> filesWithTheId = elements2files.get(value.toString()); // all referred files with the id declaration Collection<FileObject> referredFilesWithTheId = new LinkedList<FileObject>(); if (filesWithTheId != null) { referredFilesWithTheId.addAll(filesWithTheId); referredFilesWithTheId.retainAll(referredFiles); } if (referredFilesWithTheId.isEmpty()) { // unknown id hints.add( new MissingCssElement( rule, context, getAttributeValueOffsetRange(attribute, context), filesWithTheId)); } }
public static void main(String[] args) { Collection c = new ArrayList(); // 添加元素 c.add("孙悟空"); // 虽然集合里不能放基本类型的值,但Java支持自动装箱 c.add(6); System.out.println("c集合的元素个数为:" + c.size()); // 删除指定元素 c.remove(6); System.out.println("c集合的元素个数为:" + c.size()); // 判断是否包含指定字符串 System.out.println("c集合的是否包含\"孙悟空\"字符串:" + c.contains("孙悟空")); c.add("轻量级Java EE企业应用实战"); System.out.println("c集合的元素:" + c); Collection books = new HashSet(); books.add("轻量级Java EE企业应用实战"); books.add("疯狂Java讲义"); System.out.println("c集合是否完全包含books集合?" + c.containsAll(books)); // 用c集合减去books集合里的元素 c.removeAll(books); System.out.println("c集合的元素:" + c); // 删除c集合里所有元素 c.clear(); System.out.println("c集合的元素:" + c); // books集合里只剩下c集合里也包含的元素 books.retainAll(c); System.out.println("books集合的元素:" + books); }
protected void setInitialState(Collection<Partition> partitionSpace) throws Switch.InfeasibleStateException { final List<Switch> edges = topology.findEdges(); for (Switch edge : edges) { topology.getHelper(edge).initToNotOnSrc(edge, sourcePartitions.get(edge), true); } partitionSpace.retainAll(forwardingRules.keySet()); }
@SmallTest @MediumTest @LargeTest public void testCollectionRetainAllThrows() throws JSONException { try { Collection<Integer> collection = GraphObject.Factory.createList(Integer.class); collection.retainAll(Arrays.asList()); fail("Expected exception"); } catch (UnsupportedOperationException exception) { } }
/** * Merge all MergeData. All MergeData elements should be disjunct (both views and digests). * However, this method is prepared to resolve duplicate entries (for the same member). * Resolution strategy for views is to merge only 1 of the duplicate members. Resolution * strategy for digests is to take the higher seqnos for duplicate digests. * * <p>After merging all members into a Membership and subsequent sorting, the first member of * the sorted membership will be the new coordinator. This method has a lock on merge_rsps. * * @param merge_rsps A list of MergeData items. Elements with merge_rejected=true were removed * before. Is guaranteed not to be null and to contain at least 1 member. */ private MergeData consolidateMergeData(List<MergeData> merge_rsps) { long logical_time = 0; // for new_vid List<View> subgroups = new ArrayList<View>(11); // contains a list of Views, each View is a subgroup Collection<Collection<Address>> sub_mbrships = new ArrayList<Collection<Address>>(); for (MergeData tmp_data : merge_rsps) { View tmp_view = tmp_data.getView(); if (tmp_view != null) { ViewId tmp_vid = tmp_view.getVid(); if (tmp_vid != null) { // compute the new view id (max of all vids +1) logical_time = Math.max(logical_time, tmp_vid.getId()); } // merge all membership lists into one (prevent duplicates) sub_mbrships.add(new ArrayList<Address>(tmp_view.getMembers())); subgroups.add(tmp_view.copy()); } } // determine the new digest Digest new_digest = consolidateDigests(merge_rsps, merge_rsps.size()); if (new_digest == null) return null; // remove all members from the new member list that are not in the digest Collection<Address> digest_mbrs = new_digest.getMembers(); for (Collection<Address> coll : sub_mbrships) coll.retainAll(digest_mbrs); List<Address> merged_mbrs = gms.computeNewMembership(sub_mbrships); // the new coordinator is the first member of the consolidated & sorted membership list Address new_coord = merged_mbrs.isEmpty() ? null : merged_mbrs.get(0); if (new_coord == null) return null; // should be the highest view ID seen up to now plus 1 ViewId new_vid = new ViewId(new_coord, logical_time + 1); // determine the new view MergeView new_view = new MergeView(new_vid, merged_mbrs, subgroups); if (log.isTraceEnabled()) log.trace( gms.local_addr + ": consolidated view=" + new_view + "\nconsolidated digest=" + new_digest); return new MergeData(gms.local_addr, new_view, new_digest); }
public boolean retainAll(Collection<?> c) { activateForWrite(); return _delegate.retainAll(c); }
/** @throws Exception Thrown if test failed. */ public void testA() throws Exception { Collection<Integer> set = new GridConcurrentWeakHashSet<>(); Integer i = 1; assert set.add(i); assert !set.add(i); assert set.contains(i); assert set.size() == 1; Collection<Integer> c = F.asList(2, 3, 4, 5); assert set.addAll(c); assert !set.addAll(c); assert set.containsAll(c); assert set.size() == 1 + c.size(); assert set.remove(i); assert !set.remove(i); assert !set.contains(i); assert set.size() == c.size(); assert set.removeAll(c); assert !set.removeAll(c); assert !set.containsAll(c); assert set.isEmpty(); Collection<Integer> c1 = Arrays.asList(1, 3, 5, 7, 9); int cnt = 0; for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++) c1.contains(iter.next()); assert set.size() == cnt; assert set.size() == set.toArray().length; assert set.addAll(c1); assert set.retainAll(c); assert !set.retainAll(c); Collection<Integer> c2 = F.retain(c1, true, c); assert set.containsAll(c2); assert !set.containsAll(c1); assert !set.containsAll(c); assert set.size() == c2.size(); set.clear(); assert set.isEmpty(); try { set.iterator().next(); assert false; } catch (NoSuchElementException ignored) { assert true; } try { set.add(null); assert false; } catch (NullPointerException ignored) { assert true; } }
public void handleMembershipChange(Collection<Request> requests) { boolean joinAndStateTransferInitiated = false; boolean useFlushIfPresent = gms.use_flush_if_present; Collection<Address> new_mbrs = new LinkedHashSet<>(requests.size()); Collection<Address> suspected_mbrs = new LinkedHashSet<>(requests.size()); Collection<Address> leaving_mbrs = new LinkedHashSet<>(requests.size()); boolean self_leaving = false; // is the coord leaving for (Request req : requests) { switch (req.type) { case Request.JOIN: new_mbrs.add(req.mbr); if (req.useFlushIfPresent) useFlushIfPresent = true; break; case Request.JOIN_WITH_STATE_TRANSFER: new_mbrs.add(req.mbr); joinAndStateTransferInitiated = true; if (req.useFlushIfPresent) useFlushIfPresent = true; break; case Request.LEAVE: if (req.suspected) suspected_mbrs.add(req.mbr); else { leaving_mbrs.add(req.mbr); if (gms.local_addr != null && gms.local_addr.equals(req.mbr)) self_leaving = true; } break; case Request.SUSPECT: suspected_mbrs.add(req.mbr); break; } } new_mbrs.remove(gms.local_addr); // remove myself - cannot join myself (already joined) if (gms.getViewId() == null) { // we're probably not the coord anymore (we just left ourselves), let someone else do it // (client will retry when it doesn't get a response) log.debug( "gms.view_id is null, I'm not the coordinator anymore (leaving=%b); " + "the new coordinator will handle the leave request", self_leaving); return; } List<Address> current_members = gms.members.getMembers(); leaving_mbrs.retainAll( current_members); // remove all elements of leaving_mbrs which are not current members if (suspected_mbrs.remove(gms.local_addr)) log.warn("I am the coord and I'm being suspected -- will probably leave shortly"); suspected_mbrs.retainAll( current_members); // remove all elements of suspected_mbrs which are not current members // for the members that have already joined, return the current digest and membership for (Iterator<Address> it = new_mbrs.iterator(); it.hasNext(); ) { Address mbr = it.next(); if (gms.members.contains(mbr)) { // already joined: return current digest and membership log.trace( "%s: %s already present; returning existing view %s", gms.local_addr, mbr, gms.view); Tuple<View, Digest> tuple = gms.getViewAndDigest(); if (tuple != null) gms.sendJoinResponse(new JoinRsp(tuple.getVal1(), tuple.getVal2()), mbr); else log.warn( "%s: did not find a digest matching view %s; dropping JOIN-RSP", gms.local_addr, gms.view); it .remove(); // remove it anyway, even if we didn't find a digest matching the view // (joiner will retry) } } if (new_mbrs.isEmpty() && leaving_mbrs.isEmpty() && suspected_mbrs.isEmpty()) { log.trace("%s: found no members to add or remove, will not create new view", gms.local_addr); return; } View new_view = gms.getNextView(new_mbrs, leaving_mbrs, suspected_mbrs); if (new_view.size() == 0 && gms.local_addr != null && gms.local_addr.equals(new_view.getCreator())) { if (self_leaving) gms.initState(); // in case connect() is called again return; } log.trace( "%s: joiners=%s, suspected=%s, leaving=%s, new view: %s", gms.local_addr, new_mbrs, suspected_mbrs, leaving_mbrs, new_view); JoinRsp join_rsp = null; boolean hasJoiningMembers = !new_mbrs.isEmpty(); try { boolean successfulFlush = !useFlushIfPresent || !gms.flushProtocolInStack || gms.startFlush(new_view); if (!successfulFlush && hasJoiningMembers) { // Don't send a join response if the flush fails // (http://jira.jboss.org/jira/browse/JGRP-759) // The joiner should block until the previous FLUSH completed sendLeaveResponses(leaving_mbrs); // we still have to send potential leave responses // but let the joining client timeout and send another join request return; } // we cannot garbage collect during joining a new member *if* we're the only member // Example: {A}, B joins, after returning JoinRsp to B, A garbage collects messages higher // than those // in the digest returned to the client, so the client will *not* be able to ask for // retransmission // of those messages if he misses them if (hasJoiningMembers) { gms.getDownProtocol().down(new Event(Event.SUSPEND_STABLE, MAX_SUSPEND_TIMEOUT)); // create a new digest, which contains the new members, minus left members MutableDigest join_digest = new MutableDigest(new_view.getMembersRaw()).set(gms.getDigest()); for (Address member : new_mbrs) join_digest.set(member, 0, 0); // ... and set the new members. their first seqno will be 1 // If the digest from NAKACK doesn't include all members of the view, we try once more; if // it is still // incomplete, we don't send a JoinRsp back to the joiner(s). This shouldn't be a problem as // they will retry if (join_digest.allSet() || join_digest.set(gms.getDigest()).allSet()) join_rsp = new JoinRsp(new_view, join_digest); else log.warn( "%s: digest does not match view (missing seqnos for %s); dropping JOIN-RSP", gms.local_addr, Arrays.toString(join_digest.getNonSetMembers())); } sendLeaveResponses(leaving_mbrs); // no-op if no leaving members // we don't need to send the digest to existing members: // https://issues.jboss.org/browse/JGRP-1317 gms.castViewChange(new_view, null, new_mbrs); gms.sendJoinResponses(join_rsp, new_mbrs); } finally { if (hasJoiningMembers) gms.getDownProtocol().down(new Event(Event.RESUME_STABLE)); if (!joinAndStateTransferInitiated && useFlushIfPresent) gms.stopFlush(); if (self_leaving) gms.initState(); // in case connect() is called again } }
public boolean retainAll(Collection<?> c) { return wrapped.retainAll(c); }
public boolean retainAll(Collection<?> c) { return theValues.retainAll(c); }