@Test public void one() { final List<String> values = asList("a", "b", "a", "c", "a", "d", "e"); assertTrue(Algorithms.one(values, Conditions.eq("c"))); assertTrue(Algorithms.one(values, Conditions.eq("a"))); assertTrue(Algorithms.one(values, Conditions.eq("e"))); assertFalse(Algorithms.one(values, Conditions.eq("f"))); }
@SuppressWarnings({"ImplicitNumericConversion"}) @Test public void count() { final List<String> values = asList("a", "b", "a", "c", "a", "d", "e"); assertEquals(3, Algorithms.count(values, Conditions.eq("a"))); assertEquals(1, Algorithms.count(values, Conditions.eq("e"))); assertEquals(0, Algorithms.count(values, Conditions.eq("f"))); }
@Test public void first() { final List<String> values = asList("a", "b", "a", "c", "a", "d", "e"); assertEquals("c", Algorithms.first(values, Conditions.eq("c"))); assertEquals("a", Algorithms.first(values, Conditions.eq("a"))); assertEquals("e", Algorithms.first(values, Conditions.eq("e"))); assertNull(Algorithms.first(values, Conditions.eq("f"))); }
@Test public void select2() { final List<String> values = asList("a", "b", "a", "c", "a", "d", "e"); assertEquals( asList("a", "a", "a"), Algorithms.select(values, new ArrayList<String>(10), Conditions.eq("a"))); }
static List<? extends LookupElement> createLookupElements( CompletionElement completionElement, PsiJavaReference reference) { Object completion = completionElement.getElement(); assert !(completion instanceof LookupElement); if (reference instanceof PsiJavaCodeReferenceElement) { if (completion instanceof PsiMethod && ((PsiJavaCodeReferenceElement) reference).getParent() instanceof PsiImportStaticStatement) { return Collections.singletonList( JavaLookupElementBuilder.forMethod((PsiMethod) completion, PsiSubstitutor.EMPTY)); } if (completion instanceof PsiClass) { return JavaClassNameCompletionContributor.createClassLookupItems( (PsiClass) completion, JavaClassNameCompletionContributor.AFTER_NEW.accepts(reference), JavaClassNameInsertHandler.JAVA_CLASS_INSERT_HANDLER, Conditions.<PsiClass>alwaysTrue()); } } if (reference instanceof PsiMethodReferenceExpression && completion instanceof PsiMethod && ((PsiMethod) completion).isConstructor()) { return Collections.singletonList( JavaLookupElementBuilder.forMethod( (PsiMethod) completion, "new", PsiSubstitutor.EMPTY, null)); } PsiSubstitutor substitutor = completionElement.getSubstitutor(); if (substitutor == null) substitutor = PsiSubstitutor.EMPTY; if (completion instanceof PsiClass) { return Collections.singletonList( JavaClassNameCompletionContributor.createClassLookupItem((PsiClass) completion, true) .setSubstitutor(substitutor)); } if (completion instanceof PsiMethod) { return Collections.singletonList( new JavaMethodCallElement((PsiMethod) completion).setQualifierSubstitutor(substitutor)); } if (completion instanceof PsiVariable) { return Collections.singletonList( new VariableLookupItem((PsiVariable) completion).setSubstitutor(substitutor)); } return Collections.singletonList(LookupItemUtil.objectToLookupItem(completion)); }
@Test public void all() { final List<String> values = asList("a", "b", "a", "c", "a", "d", "e"); assertTrue(Algorithms.all(values, Conditions.gte("a"))); assertTrue(Algorithms.all(values, Conditions.lt("z"))); assertTrue(Algorithms.all(values, Conditions.lte("e"))); assertFalse(Algorithms.all(values, Conditions.gt("b"))); assertFalse(Algorithms.all(values, Conditions.eq("a"))); assertFalse(Algorithms.all(values, Conditions.eq("f"))); }
private static List<? extends LookupElement> createLookupElements( CompletionElement completionElement, PsiJavaReference reference) { Object completion = completionElement.getElement(); assert !(completion instanceof LookupElement); if (reference instanceof PsiJavaCodeReferenceElement) { if (completion instanceof PsiMethod && ((PsiJavaCodeReferenceElement) reference).getParent() instanceof PsiImportStaticStatement) { return Collections.singletonList( JavaLookupElementBuilder.forMethod((PsiMethod) completion, PsiSubstitutor.EMPTY)); } if (completion instanceof PsiClass) { return JavaClassNameCompletionContributor.createClassLookupItems( (PsiClass) completion, JavaClassNameCompletionContributor.AFTER_NEW.accepts(reference), JavaClassNameInsertHandler.JAVA_CLASS_INSERT_HANDLER, Conditions.<PsiClass>alwaysTrue()); } } if (reference instanceof PsiMethodReferenceExpression && completion instanceof PsiMethod && ((PsiMethod) completion).isConstructor()) { return Collections.singletonList( JavaLookupElementBuilder.forMethod( (PsiMethod) completion, "new", PsiSubstitutor.EMPTY, null)); } LookupElement _ret = LookupItemUtil.objectToLookupItem(completion); if (_ret instanceof LookupItem) { final PsiSubstitutor substitutor = completionElement.getSubstitutor(); if (substitutor != null) { ((LookupItem<?>) _ret).setAttribute(LookupItem.SUBSTITUTOR, substitutor); } } return Collections.singletonList(_ret); }
/** * recursive method to get all conditions from "condition tree" * * @param conds - at the beginning root conditions, at lower level conditions under * CompoundCondition * @param op - at the beginning null, at lower level operator of Compound Condition * @return group of its subgroups and of conditions */ private GenPredicGroup getGroupConditions(Conditions conds, GenPredicGroup.EOperator op) { GenPredicGroup myConds = new GenPredicGroup(); myConds.setOperator((op != null) ? op : GenPredicGroup.EOperator.ALL); Iterator<Condition> it = conds.iterator(); while (it.hasNext()) { Condition cond = it.next(); // if true, a new condition group is created and filled if (cond instanceof CompoundCondition) { // set group operator GenPredicGroup.EOperator actualOp = null; if (cond instanceof ConditionAll) { actualOp = GenPredicGroup.EOperator.ALL; } else if (cond instanceof ConditionAny) { actualOp = GenPredicGroup.EOperator.ANY; } else if (cond instanceof ConditionNone) { actualOp = GenPredicGroup.EOperator.NONE; } if (myConds.getMyGroups() == null) { myConds.setMyGroups(new ArrayList<GenPredicGroup>()); } // call recursively to get conditions of actual group myConds .getMyGroups() .add(getGroupConditions(((CompoundCondition) cond).getConditions(), actualOp)); } else { // get condition if (myConds.getGeneratorPredicts() == null) { myConds.setGeneratorPredicts(new ArrayList<GeneratorPredicate>()); } myConds.getGeneratorPredicts().add(getGP(cond)); } } return myConds; }
@Override public void run() { try { int size = 0; LogUtils.debug( log, "LOG_LDAP_REQ_SEARCH", currentMessageId, dn, scope, sizeLimit, timelimit, ldapFilter.toString(), returningAttributes); if (scope == Ldap.SCOPE_BASE_OBJECT) { log.debug("Check type of search... scope is BASE OBJECT"); if ("".equals(dn)) { size = 1; log.info("send root DSE"); responseHandler.sendRootDSE(currentMessageId); } else if (Ldap.BASE_CONTEXT.equals(dn)) { size = 1; // root // root log.info("send base context"); responseHandler.sendBaseContext(currentMessageId); } else if (dn.startsWith("uid=") && dn.indexOf(',') > 0) { if (user != null) { // single user request // single user request String uid = dn.substring("uid=".length(), dn.indexOf(',')); Set<LdapContact> persons = null; // first search in contact // first search in contact try { // check if this is a contact uid Integer.parseInt(uid); persons = contactFind(conditions.isEqualTo("imapUid", uid), returningAttributes, sizeLimit); } catch (NumberFormatException e) { // ignore, this is not a contact uid } // then in GAL if (persons == null || persons.isEmpty()) { List<LdapContact> galContacts = null; try { log.info("do GAL search: " + uid); galContacts = userFactory.galFind(conditions.isEqualTo("imapUid", uid), sizeLimit); } catch (NotAuthorizedException ex) { log.error("not auth", ex); } catch (BadRequestException ex) { log.error("bad req", ex); } if (galContacts != null && galContacts.size() > 0) { LdapContact person = galContacts.get(0); if (persons == null) { persons = new HashSet<LdapContact>(); } persons.add(person); } } size = persons == null ? 0 : persons.size(); try { sendPersons( currentMessageId, dn.substring(dn.indexOf(',')), persons, returningAttributes); } catch (NotAuthorizedException ex) { log.error("Not authorised", ex); } catch (BadRequestException ex) { log.error("bad req", ex); } } else { LogUtils.debug( log, "LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn); } } else { LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_INVALID_DN (1)", currentMessageId, dn); } } else if (Ldap.COMPUTER_CONTEXT.equals(dn) || Ldap.COMPUTER_CONTEXT_LION.equals(dn)) { size = 1; // computer context for iCal log.info("send computer context"); responseHandler.sendComputerContext(currentMessageId, returningAttributes); } else if ((dn.equals("") // Outlook 2010 by default sends no DN || Ldap.BASE_CONTEXT.equalsIgnoreCase(dn) || Ldap.OD_USER_CONTEXT.equalsIgnoreCase(dn)) || Ldap.MSLIVE_BASE_CONTEXT.equals(dn) || Ldap.OD_USER_CONTEXT_LION.equalsIgnoreCase(dn)) { log.info("not a weird search... check for normal conditions"); if (user != null) { log.debug("we have a user..."); Set<LdapContact> persons = new HashSet<LdapContact>(); if (ldapFilter.isFullSearch()) { // append personal contacts first log.info("do personcal contact search"); Set<LdapContact> contacts = contactFind(null, returningAttributes, sizeLimit); LogUtils.debug(log, "fullSearch: results:", contacts.size()); for (LdapContact person : contacts) { persons.add(person); if (persons.size() == sizeLimit) { break; } } // full search for (char c = 'A'; c <= 'Z'; c++) { if (!abandon && persons.size() < sizeLimit) { Condition startsWith = conditions.startsWith("cn", String.valueOf(c)); Collection<LdapContact> galContacts = null; try { log.info("now do GAL search"); galContacts = userFactory.galFind(startsWith, sizeLimit); } catch (NotAuthorizedException ex) { log.error("not auth", ex); } catch (BadRequestException ex) { log.error("bad req", ex); } if (galContacts != null) { LogUtils.debug(log, "doSearch: results:", contacts.size()); for (LdapContact person : galContacts) { persons.add(person); if (persons.size() == sizeLimit) { break; } } } } if (persons.size() == sizeLimit) { break; } } } else { // append only personal contacts log.info("do personcal contact search only"); Condition filter = ldapFilter.getContactSearchFilter(); LogUtils.debug(log, "not full search:", filter); // if ldapfilter is not a full search and filter is null, // ignored all attribute filters => return empty results if (ldapFilter.isFullSearch() || filter != null) { Set<LdapContact> contacts = contactFind(filter, returningAttributes, sizeLimit); for (LdapContact person : contacts) { persons.add(person); if (persons.size() == sizeLimit) { log.debug("EXceeded size limit1"); break; } } LogUtils.trace(log, "local contacts result size: ", persons.size()); if (!abandon && persons.size() < sizeLimit) { List<LdapContact> galContacts = null; try { galContacts = ldapFilter.findInGAL(user, returningAttributes, sizeLimit - persons.size()); } catch (NotAuthorizedException ex) { log.error("not auth", ex); } catch (BadRequestException ex) { log.error("bad req", ex); } if (galContacts != null) { LogUtils.trace(log, "gal contacts result size: ", galContacts.size()); for (LdapContact person : galContacts) { if (persons.size() >= sizeLimit) { log.debug("EXceeded size limit2"); break; } LogUtils.trace(log, "add contact to results: ", person.getName()); persons.add(person); } } } } } LogUtils.debug( log, "LOG_LDAP_REQ_SEARCH_FOUND_RESULTS", currentMessageId, persons.size()); try { sendPersons(currentMessageId, ", " + dn, persons, returningAttributes); } catch (NotAuthorizedException ex) { log.error("not auth", ex); } catch (BadRequestException ex) { log.error("bad req", ex); } LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_END", currentMessageId); } else { LogUtils.debug( log, "LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn); } } else if (dn != null && dn.length() > 0 && !Ldap.OD_CONFIG_CONTEXT.equals(dn) && !Ldap.OD_GROUP_CONTEXT.equals(dn)) { LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_INVALID_DN (2)", currentMessageId, dn); log.debug( "DN is not equal to: " + Ldap.OD_CONFIG_CONTEXT + " or " + Ldap.OD_GROUP_CONTEXT + " or any other valid pattern. Is: " + dn); } else { log.warn( "Search criteria didnt match any of the expected patterns. Perhaps the user name is missing a context? DN=" + dn + ", expected something like: " + Ldap.OD_USER_CONTEXT); } // iCal: do not send LDAP_SIZE_LIMIT_EXCEEDED on apple-computer search by cn with sizelimit 1 if (size > 1 && size == sizeLimit) { LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_SIZE_LIMIT_EXCEEDED", currentMessageId); responseHandler.sendClient( currentMessageId, Ldap.LDAP_REP_RESULT, Ldap.LDAP_SIZE_LIMIT_EXCEEDED, ""); } else { log.debug("No search results"); LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_SUCCESS", currentMessageId); responseHandler.sendClient(currentMessageId, Ldap.LDAP_REP_RESULT, Ldap.LDAP_SUCCESS, ""); } } catch (SocketException e) { log.warn("closed connection", e); } catch (IOException e) { log.error("", e); try { responseHandler.sendErr(currentMessageId, Ldap.LDAP_REP_RESULT, e); } catch (IOException e2) { LogUtils.debug(log, "LOG_EXCEPTION_SENDING_ERROR_TO_CLIENT", e2); } } finally { log.debug("search complete"); searchManager.searchComplete(uuid, currentMessageId); } }