private static Ordering<String> createExplicitComparator(String[] elements) { // Collapse equal elements, which Ordering.explicit() doesn't support, while // maintaining the ordering by first occurrence. Set<String> elementsPlus = Sets.newLinkedHashSet(); elementsPlus.add(BEFORE_FIRST); elementsPlus.add(BEFORE_FIRST_2); elementsPlus.addAll(Arrays.asList(elements)); elementsPlus.add(AFTER_LAST); elementsPlus.add(AFTER_LAST_2); return Ordering.explicit(Lists.newArrayList(elementsPlus)); }
@SuppressWarnings("unchecked") @Override public Ordering<Element> enclosedBy(Element element) { return (Ordering<Element>) Ordering.explicit(element.getEnclosedElements()); } };
public AllAtOnceExecutionSchedule(Collection<SqlStageExecution> stages) { requireNonNull(stages, "stages is null"); List<PlanFragmentId> preferredScheduleOrder = getPreferredScheduleOrder(stages.stream() .map(SqlStageExecution::getFragment) .collect(toImmutableList())); Ordering<SqlStageExecution> ordering = Ordering.explicit(preferredScheduleOrder) .onResultOf(PlanFragment::getId) .onResultOf(SqlStageExecution::getFragment); schedulingStages = new LinkedHashSet<>(ordering.sortedCopy(stages)); }
public void testExplicit_withDuplicates() { try { Ordering.explicit(1, 2, 3, 4, 2); fail(); } catch (IllegalArgumentException expected) { } }
/** * Returns an ordering that compares objects according to the order in which they are given to * this method. Only objects present in the argument list (according to {@link Object#equals}) may * be compared. This comparator imposes a "partial ordering" over the type {@code T}. Null values * in the argument list are not supported. * * <p>The returned comparator throws a {@link ClassCastException} when it receives an input * parameter that isn't among the provided values. * * <p>The generated comparator is serializable if all the provided values are serializable. * * @param leastValue the value which the returned comparator should consider the "least" of all * values * @param remainingValuesInOrder the rest of the values that the returned comparator will be able * to compare, in the order the comparator should follow * @return the comparator described above * @throws NullPointerException if any of the provided values is null * @throws IllegalArgumentException if any duplicate values (according to {@link * Object#equals(Object)}) are present among the method arguments */ // TODO(kevinb): provide replacement @GwtCompatible(serializable = true) public static <T> Ordering<T> explicit(T leastValue, T... remainingValuesInOrder) { return explicit(Lists.asList(leastValue, remainingValuesInOrder)); }
/** * Returns an ordering that compares objects according to the order in which they are given to * this method. Only objects present in the argument list (according to {@link Object#equals}) may * be compared. This comparator imposes a "partial ordering" over the type {@code T}. Null values * in the argument list are not supported. * * <p>The returned comparator throws a {@link ClassCastException} when it receives an input * parameter that isn't among the provided values. * * <p>The generated comparator is serializable if all the provided values are serializable. * * @param leastValue the value which the returned comparator should consider the "least" of all * values * @param remainingValuesInOrder the rest of the values that the returned comparator will be able * to compare, in the order the comparator should follow * @return the comparator described above * @throws NullPointerException if any of the provided values is null * @throws IllegalArgumentException if any duplicate values (according to {@link * Object#equals(Object)}) are present among the method arguments */ // TODO(kevinb): provide replacement @GwtCompatible(serializable = true) public static <T> Ordering<T> explicit(T leastValue, T... remainingValuesInOrder) { return explicit(Lists.asList(leastValue, remainingValuesInOrder)); }
public void testExplicit_sortingExample() { Comparator<Integer> c = Ordering.explicit(2, 8, 6, 1, 7, 5, 3, 4, 0, 9); List<Integer> list = Arrays.asList(0, 3, 5, 6, 7, 8, 9); Collections.sort(list, c); assertThat(list).containsExactly(8, 6, 7, 5, 3, 0, 9).inOrder(); reserializeAndAssert(c); }
/** * Return list of propertyDto by component uuid, sorted from project to lowest module */ private Multimap<String, PropertyDto> loadComponentSettings(DbSession dbSession, Optional<String> key, ComponentDto component) { List<String> componentUuids = DOT_SPLITTER.splitToList(component.moduleUuidPath()); List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids); Set<Long> componentIds = componentDtos.stream().map(ComponentDto::getId).collect(Collectors.toSet()); Map<Long, String> uuidsById = componentDtos.stream().collect(Collectors.toMap(ComponentDto::getId, ComponentDto::uuid)); List<PropertyDto> properties = key.isPresent() ? dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, Collections.singleton(key.get()), componentIds) : dbClient.propertiesDao().selectPropertiesByComponentIds(dbSession, componentIds); Multimap<String, PropertyDto> propertyDtosByUuid = TreeMultimap.create(Ordering.explicit(componentUuids), Ordering.arbitrary()); for (PropertyDto propertyDto : properties) { Long componentId = propertyDto.getResourceId(); String componentUuid = uuidsById.get(componentId); propertyDtosByUuid.put(componentUuid, propertyDto); } return propertyDtosByUuid; }
public void testExplicit_none() { Comparator<Integer> c = Ordering.explicit(Collections.<Integer>emptyList()); try { c.compare(0, 0); fail(); } catch (IncomparableValueException expected) { assertEquals(0, expected.value); } reserializeAndAssert(c); }
@GwtIncompatible // too slow public void testCombinationsExhaustively_startingFromExplicit() { testExhaustively(Ordering.explicit("a", "b", "c", "d"), "a", "b", "d"); }
public void testExplicit_one() { Comparator<Integer> c = Ordering.explicit(0); assertEquals(0, c.compare(0, 0)); try { c.compare(0, 1); fail(); } catch (IncomparableValueException expected) { assertEquals(1, expected.value); } reserializeAndAssert(c); assertEquals("Ordering.explicit([0])", c.toString()); }
public void testExplicit_two() { Comparator<Integer> c = Ordering.explicit(42, 5); assertEquals(0, c.compare(5, 5)); assertTrue(c.compare(5, 42) > 0); assertTrue(c.compare(42, 5) < 0); try { c.compare(5, 666); fail(); } catch (IncomparableValueException expected) { assertEquals(666, expected.value); } new EqualsTester() .addEqualityGroup(c, Ordering.explicit(42, 5)) .addEqualityGroup(Ordering.explicit(5, 42)) .addEqualityGroup(Ordering.explicit(42)) .testEquals(); reserializeAndAssert(c); }
/** * List of ancestors, ordered from root to parent. The list is empty * if the component is a tree root. Disabled components are excluded by design * as tree represents the more recent analysis. */ public List<ComponentDto> selectAncestors(DbSession dbSession, ComponentDto component) { if (component.isRoot()) { return Collections.emptyList(); } List<String> ancestorUuids = component.getUuidPathAsList(); List<ComponentDto> ancestors = selectByUuids(dbSession, ancestorUuids); return Ordering.explicit(ancestorUuids).onResultOf(ComponentDto::uuid).immutableSortedCopy(ancestors); }
private List<GroupDto> findGroups(DbSession dbSession, PermissionQuery dbQuery, PermissionTemplateDto template) { List<String> orderedNames = dbClient.permissionTemplateDao().selectGroupNamesByQueryAndTemplate(dbSession, dbQuery, template.getId()); List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, template.getOrganizationUuid(), orderedNames); if (orderedNames.contains(DefaultGroups.ANYONE)) { groups.add(0, new GroupDto().setId(0).setName(DefaultGroups.ANYONE)); } return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups); }
private List<UserDto> findUsers(DbSession dbSession, PermissionQuery query) { List<Integer> orderedIds = dbClient.userPermissionDao().selectUserIdsByQuery(dbSession, query); return Ordering.explicit(orderedIds).onResultOf(UserDto::getId).immutableSortedCopy(dbClient.userDao().selectByIds(dbSession, orderedIds)); }
private List<GroupDto> findGroups(DbSession dbSession, OrganizationDto org, PermissionQuery dbQuery) { List<String> orderedNames = dbClient.groupPermissionDao().selectGroupNamesByQuery(dbSession, dbQuery); List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, org.getUuid(), orderedNames); if (orderedNames.contains(DefaultGroups.ANYONE)) { groups.add(0, new GroupDto().setId(0).setName(DefaultGroups.ANYONE).setOrganizationUuid(org.getUuid())); } return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups); }
private List<UserDto> findUsers(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) { List<String> orderedLogins = dbClient.permissionTemplateDao().selectUserLoginsByQueryAndTemplate(dbSession, query, template.getId()); return Ordering.explicit(orderedLogins).onResultOf(UserDto::getLogin).immutableSortedCopy(dbClient.userDao().selectByLogins(dbSession, orderedLogins)); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = getOrganization(dbSession, request.param("organization")); userSession.checkMembership(organization); UserQuery.Builder userQuery = buildUserQuery(request, organization); SearchOptions searchOptions = buildSearchOptions(request); SearchResult<UserDoc> searchResults = userIndex.search(userQuery.build(), searchOptions); List<String> orderedLogins = searchResults.getDocs().stream().map(UserDoc::login).collect(MoreCollectors.toList()); List<UserDto> users = dbClient.userDao().selectByLogins(dbSession, orderedLogins).stream() .sorted(Ordering.explicit(orderedLogins).onResultOf(UserDto::getLogin)) .collect(MoreCollectors.toList()); Multiset<String> groupCountByLogin = null; if (userSession.hasPermission(ADMINISTER, organization)) { groupCountByLogin = dbClient.groupMembershipDao().countGroupByLoginsAndOrganization(dbSession, orderedLogins, organization.getUuid()); } Common.Paging wsPaging = buildWsPaging(request, searchResults); SearchMembersWsResponse wsResponse = buildResponse(users, wsPaging, groupCountByLogin); writeProtobuf(wsResponse, request, response); } }
private SearchResults searchData(DbSession dbSession, SearchProjectsRequest request, @Nullable OrganizationDto organization) { Set<String> favoriteProjectUuids = loadFavoriteProjectUuids(dbSession); List<Criterion> criteria = FilterParser.parse(firstNonNull(request.getFilter(), "")); ProjectMeasuresQuery query = newProjectMeasuresQuery(criteria, hasFavoriteFilter(criteria) ? favoriteProjectUuids : null) .setSort(request.getSort()) .setAsc(request.getAsc()); Optional.ofNullable(organization) .map(OrganizationDto::getUuid) .ifPresent(query::setOrganizationUuid); ProjectMeasuresQueryValidator.validate(query); SearchIdResult<String> esResults = index.search(query, new SearchOptions() .addFacets(request.getFacets()) .setPage(request.getPage(), request.getPageSize())); List<String> projectUuids = esResults.getIds(); Ordering<ComponentDto> ordering = Ordering.explicit(projectUuids).onResultOf(ComponentDto::uuid); List<ComponentDto> projects = ordering.immutableSortedCopy(dbClient.componentDao().selectByUuids(dbSession, projectUuids)); Map<String, SnapshotDto> analysisByProjectUuid = getSnapshots(dbSession, request, projectUuids); return new SearchResults(projects, favoriteProjectUuids, esResults, analysisByProjectUuid, query); }
public void testCompound_instance_generics() { Ordering<Object> objects = Ordering.explicit((Object) 1); Ordering<Number> numbers = Ordering.explicit((Number) 1); Ordering<Integer> integers = Ordering.explicit(1); // Like by like equals like Ordering<Number> a = numbers.compound(numbers); // The compound takes the more specific type of the two, regardless of order Ordering<Number> b = numbers.compound(objects); Ordering<Number> c = objects.compound(numbers); Ordering<Integer> d = numbers.compound(integers); Ordering<Integer> e = integers.compound(numbers); // This works with three levels too (IDEA falsely reports errors as noted // below. Both javac and eclipse handle these cases correctly.) Ordering<Number> f = numbers.compound(objects).compound(objects); // bad IDEA Ordering<Number> g = objects.compound(numbers).compound(objects); Ordering<Number> h = objects.compound(objects).compound(numbers); Ordering<Number> i = numbers.compound(objects.compound(objects)); Ordering<Number> j = objects.compound(numbers.compound(objects)); // bad IDEA Ordering<Number> k = objects.compound(objects.compound(numbers)); // You can also arbitrarily assign a more restricted type - not an intended // feature, exactly, but unavoidable (I think) and harmless Ordering<Integer> l = objects.compound(numbers); // This correctly doesn't work: // Ordering<Object> m = numbers.compound(objects); // Sadly, the following works in javac 1.6, but at least it fails for // eclipse, and is *correctly* highlighted red in IDEA. // Ordering<Object> n = objects.compound(numbers); }