public static List<BlockingQueue<Object>> blockingQueues() { return ImmutableList.<BlockingQueue<Object>>of( new LinkedBlockingQueue<Object>(), new LinkedBlockingQueue<Object>(10), new SynchronousQueue<Object>(), new ArrayBlockingQueue<Object>(10), new LinkedBlockingDeque<Object>(), new LinkedBlockingDeque<Object>(10), new PriorityBlockingQueue<Object>(10, Ordering.arbitrary())); }
public void testArbitrary_withoutCollisions() { List<Object> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(new Object()); } Ordering<Object> arbitrary = Ordering.arbitrary(); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); assertEquals("Ordering.arbitrary()", arbitrary.toString()); }
@GwtIncompatible // too slow public void testCombinationsExhaustively_startingFromArbitrary() { Ordering<Object> arbitrary = Ordering.arbitrary(); Object[] array = {1, "foo", new Object()}; // There's no way to tell what the order should be except empirically Arrays.sort(array, arbitrary); testExhaustively(arbitrary, array); }
/** * 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; }
/** * Return list of settings by component uuid, sorted from project to lowest module */ private Multimap<String, Setting> loadComponentSettings(DbSession dbSession, Set<String> keys, 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 = dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, keys, componentIds); List<PropertyDto> propertySets = dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, getPropertySetKeys(properties), componentIds); Multimap<String, Setting> settingsByUuid = TreeMultimap.create(Ordering.explicit(componentUuids), Ordering.arbitrary()); for (PropertyDto propertyDto : properties) { Long componentId = propertyDto.getResourceId(); String componentUuid = uuidsById.get(componentId); String propertyKey = propertyDto.getKey(); settingsByUuid.put(componentUuid, Setting.createFromDto(propertyDto, getPropertySets(propertyKey, propertySets, componentId), propertyDefinitions.get(propertyKey))); } return settingsByUuid; }
@SuppressWarnings("unchecked") private static <T> Ordering<T> arbitrary() { return (Ordering<T>)Ordering.arbitrary(); }
/** * @param executor the {@link ScheduledExecutorService} to provide the thread for operations to * provide available message IDs */ public MessageIDFactory(ScheduledExecutorService executor) { this.executor = executor; this.allocations = TreeMultimap.create(Ordering.arbitrary(), Ordering.natural()); this.lock = new ReentrantReadWriteLock(); this.random = new Random(System.currentTimeMillis()); }
@Override public int compareTo(RoleAssignments other) { return Ordering.arbitrary().compare(this, other); } }
/** Use an arbitrary, but fixed, ordering. */ @Override public int compare(DockerHostLocation l1, DockerHostLocation l2) { return Ordering.arbitrary().compare(l1, l2); }
@Override public int compareTo(FieldMember o) { return super.compare(o).compare(value, o.value, Ordering.arbitrary()).result(); }
/** * Return a {@link Comparator} that sorts elements by their natural order if * they are {@link Comparable} and sorts them in an arbitrary, but per * JVM-lifecycle consistent, order if they are not. * * @return the comparator */ @SuppressWarnings("unchecked") public static <T> Comparator<T> naturalOrArbitrary() { return (o1, o2) -> { if(o1 instanceof Comparable) { return ((Comparable<T>) o1).compareTo(o2); } else { return Ordering.arbitrary().compare(o1, o2); } }; }
/** * Return a {@link Comparator} that always returns {@code 0} when two * objects are equal, but otherwise returns an arbitrary but consistent * ordering for objects during the course of the JVM lifecycle. * * @return the comparator */ public static <T> Comparator<T> equalOrArbitrary() { return (o1, o2) -> { if(o1 == o2 || o1.equals(o2)) { return 0; } else { return Ordering.arbitrary().compare(o1, o2); } }; }
@Override public int compare(Expression expression1, Expression expression2) { // Current cost heuristic: // 1) Prefer fewer input symbols // 2) Prefer smaller expression trees // 3) Ordering.arbitrary() - creates a stable consistent ordering (extremely useful for unit testing) // TODO: be more precise in determining the cost of an expression return ComparisonChain.start() .compare(DependencyExtractor.extractAll(expression1).size(), DependencyExtractor.extractAll(expression2).size()) .compare(SubExpressionExtractor.extract(expression1).size(), SubExpressionExtractor.extract(expression2).size()) .compare(expression1, expression2, Ordering.arbitrary()) .result(); } });
@Override public int compareTo(ConfigRecord<?> that) { return ComparisonChain.start() .compare(String.valueOf(this.key.getTypeLiteral().getType()), String.valueOf(that.key.getTypeLiteral().getType())) .compare(String.valueOf(this.key.getAnnotationType()), String.valueOf(that.key.getAnnotationType())) .compare(this.key, that.key, Ordering.arbitrary()) .result(); } }
@Override public int compareTo(ConfigRecord<?> that) { return ComparisonChain.start() .compare(String.valueOf(this.key.getTypeLiteral().getType()), String.valueOf(that.key.getTypeLiteral().getType())) .compare(String.valueOf(this.key.getAnnotationType()), String.valueOf(that.key.getAnnotationType())) .compare(this.key, that.key, Ordering.arbitrary()) .result(); } }
@Override public int compareTo(ConfigRecord<?> that) { return ComparisonChain.start() .compare(String.valueOf(this.key.getTypeLiteral().getType()), String.valueOf(that.key.getTypeLiteral().getType())) .compare(String.valueOf(this.key.getAnnotationType()), String.valueOf(that.key.getAnnotationType())) .compare(this.key, that.key, Ordering.arbitrary()) .result(); } }
public static List<BlockingQueue<Object>> blockingQueues() { return ImmutableList.<BlockingQueue<Object>>of( new LinkedBlockingQueue<Object>(), new LinkedBlockingQueue<Object>(10), new SynchronousQueue<Object>(), new ArrayBlockingQueue<Object>(10), new LinkedBlockingDeque<Object>(), new LinkedBlockingDeque<Object>(10), new PriorityBlockingQueue<Object>(10, Ordering.arbitrary())); }
public void testArbitrary_withoutCollisions() { List<Object> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(new Object()); } Ordering<Object> arbitrary = Ordering.arbitrary(); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); assertEquals("Ordering.arbitrary()", arbitrary.toString()); }
@GwtIncompatible // too slow public void testCombinationsExhaustively_startingFromArbitrary() { Ordering<Object> arbitrary = Ordering.arbitrary(); Object[] array = {1, "foo", new Object()}; // There's no way to tell what the order should be except empirically Arrays.sort(array, arbitrary); testExhaustively(arbitrary, array); }
static TreeMultimap<String,IndexStream> nextAll(String key, Collection<IndexStream> streams) { TreeMultimap<String,IndexStream> newChildren = TreeMultimap.create(Ordering.<String> natural(), Ordering.arbitrary()); for (IndexStream itr : streams) { if (!isDay(key) && isDay(key(itr.peek()))) { newChildren.put(itr.peek().first(), itr); } else { itr.next(); if (itr.hasNext()) { newChildren.put(itr.peek().first(), itr); } else { return TreeMultimap.create(Ordering.<String> natural(), Ordering.arbitrary()); } } } return newChildren; }