Example usage for com.google.common.collect Ordering nullsFirst

List of usage examples for com.google.common.collect Ordering nullsFirst

Introduction

In this page you can find the example usage for com.google.common.collect Ordering nullsFirst.

Prototype



@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsFirst() 

Source Link

Document

Returns an ordering that treats null as less than all other values and uses this to compare non-null values.

Usage

From source file:org.apache.phoenix.iterate.OrderedResultIterator.java

/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 *//*from ww w  . j av a 2  s . c om*/
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Expression e = col.getExpression();
        Comparator<ImmutableBytesWritable> comparator = e.getSortOrder() == SortOrder.DESC
                && !e.getDataType().isFixedWidth() ? buildDescVarLengthComparator()
                        : new ImmutableBytesWritable.Comparator();
        Ordering<ImmutableBytesWritable> o = Ordering.from(comparator);
        if (!col.isAscending())
            o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}

From source file:com.google.errorprone.refaster.testdata.template.IfFallthroughTemplate.java

@AfterTemplate
int after(T left, T right, Ordering<? super T> ordering) {
    return ordering.nullsFirst().compare(left, right);
}

From source file:io.crate.execution.engine.sort.OrderingByPosition.java

private OrderingByPosition(int position, boolean reverse, @Nullable Boolean nullFirst) {
    this.position = position;

    // note, that we are reverse for the queue so this conditional is by intent
    Ordering<Comparable> ordering;
    nullFirst = nullFirst != null ? !nullFirst : null; // swap because queue is reverse
    if (reverse) {
        ordering = Ordering.natural();/* ww  w  . j a  v a2  s .  co  m*/
        if (nullFirst == null || !nullFirst) {
            ordering = ordering.nullsLast();
        } else {
            ordering = ordering.nullsFirst();
        }
    } else {
        ordering = Ordering.natural().reverse();
        if (nullFirst == null || nullFirst) {
            ordering = ordering.nullsFirst();
        } else {
            ordering = ordering.nullsLast();
        }
    }
    this.ordering = ordering;
}

From source file:com.ibm.common.activitystreams.actions.Parameter.java

public <O extends Comparable<? super O>> Range<O> bounds() {
    O mini = minInclusive();/*  ww  w.j  av  a  2s  .  c  o  m*/
    O mine = minExclusive();
    O maxi = maxInclusive();
    O maxe = maxExclusive();
    Ordering<O> ordering = Ordering.<O>natural();
    O min = ordering.nullsLast().min(mini, mine);
    O max = ordering.nullsFirst().max(maxi, maxe);
    BoundType lower = min == null ? null : min == mini ? BoundType.CLOSED : BoundType.OPEN;
    BoundType upper = max == null ? null : max == maxi ? BoundType.CLOSED : BoundType.OPEN;
    if (lower == null && upper == null)
        return Range.<O>all();
    else if (lower != null && upper == null)
        return lower == BoundType.CLOSED ? Range.atLeast(min) : Range.greaterThan(min);
    else if (lower == null && upper != null)
        return upper == BoundType.CLOSED ? Range.atMost(max) : Range.lessThan(max);
    else {
        return Range.range(min, lower, max, upper);
    }
}

From source file:org.apache.druid.query.ChainedExecutionQueryRunner.java

@Override
public Sequence<T> run(final QueryPlus<T> queryPlus, final Map<String, Object> responseContext) {
    Query<T> query = queryPlus.getQuery();
    final int priority = QueryContexts.getPriority(query);
    final Ordering ordering = query.getResultOrdering();
    final QueryPlus<T> threadSafeQueryPlus = queryPlus.withoutThreadUnsafeState();
    return new BaseSequence<T, Iterator<T>>(new BaseSequence.IteratorMaker<T, Iterator<T>>() {
        @Override/*w w w. j a v a 2  s .co m*/
        public Iterator<T> make() {
            // Make it a List<> to materialize all of the values (so that it will submit everything to the executor)
            ListenableFuture<List<Iterable<T>>> futures = Futures
                    .allAsList(Lists.newArrayList(Iterables.transform(queryables, input -> {
                        if (input == null) {
                            throw new ISE(
                                    "Null queryRunner! Looks to be some segment unmapping action happening");
                        }

                        return exec.submit(new AbstractPrioritizedCallable<Iterable<T>>(priority) {
                            @Override
                            public Iterable<T> call() {
                                try {
                                    Sequence<T> result = input.run(threadSafeQueryPlus, responseContext);
                                    if (result == null) {
                                        throw new ISE("Got a null result! Segments are missing!");
                                    }

                                    List<T> retVal = result.toList();
                                    if (retVal == null) {
                                        throw new ISE("Got a null list of results! WTF?!");
                                    }

                                    return retVal;
                                } catch (QueryInterruptedException e) {
                                    throw Throwables.propagate(e);
                                } catch (Exception e) {
                                    log.error(e, "Exception with one of the sequences!");
                                    throw Throwables.propagate(e);
                                }
                            }
                        });
                    })));

            queryWatcher.registerQuery(query, futures);

            try {
                return new MergeIterable<>(ordering.nullsFirst(),
                        QueryContexts.hasTimeout(query)
                                ? futures.get(QueryContexts.getTimeout(query), TimeUnit.MILLISECONDS)
                                : futures.get()).iterator();
            } catch (InterruptedException e) {
                log.warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId());
                futures.cancel(true);
                throw new QueryInterruptedException(e);
            } catch (CancellationException e) {
                throw new QueryInterruptedException(e);
            } catch (TimeoutException e) {
                log.info("Query timeout, cancelling pending results for query id [%s]", query.getId());
                futures.cancel(true);
                throw new QueryInterruptedException(e);
            } catch (ExecutionException e) {
                throw Throwables.propagate(e.getCause());
            }
        }

        @Override
        public void cleanup(Iterator<T> tIterator) {

        }
    });
}