Example usage for com.google.common.collect MinMaxPriorityQueue offer

List of usage examples for com.google.common.collect MinMaxPriorityQueue offer

Introduction

In this page you can find the example usage for com.google.common.collect MinMaxPriorityQueue offer.

Prototype

@Override
public boolean offer(E element) 

Source Link

Document

Adds the given element to this queue.

Usage

From source file:org.apache.druid.query.groupby.orderby.TopNSequence.java

public TopNSequence(final Sequence<T> input, final Ordering<T> ordering, final int limit) {
    super(new IteratorMaker<T, Iterator<T>>() {
        @Override//from   w  w w.  j a  v  a  2  s  . c o  m
        public Iterator<T> make() {
            if (limit <= 0) {
                return Collections.emptyIterator();
            }

            // Materialize the topN values
            final MinMaxPriorityQueue<T> queue = MinMaxPriorityQueue.orderedBy(ordering).maximumSize(limit)
                    .create();

            input.accumulate(queue, new Accumulator<MinMaxPriorityQueue<T>, T>() {
                @Override
                public MinMaxPriorityQueue<T> accumulate(MinMaxPriorityQueue<T> theQueue, T row) {
                    theQueue.offer(row);
                    return theQueue;
                }
            });

            // Now return them when asked
            return new Iterator<T>() {
                @Override
                public boolean hasNext() {
                    return !queue.isEmpty();
                }

                @Override
                public T next() {
                    return queue.poll();
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public void cleanup(Iterator<T> rowIterator) {
            // Nothing to do
        }
    });
}

From source file:io.druid.query.groupby.orderby.TopNSequence.java

public TopNSequence(final Sequence<T> input, final Ordering<T> ordering, final int limit) {
    super(new IteratorMaker<T, Iterator<T>>() {
        @Override//from w  w w.j a va 2s . c om
        public Iterator<T> make() {
            if (limit <= 0) {
                return Iterators.emptyIterator();
            }

            // Materialize the topN values
            final MinMaxPriorityQueue<T> queue = MinMaxPriorityQueue.orderedBy(ordering).maximumSize(limit)
                    .create();

            input.accumulate(queue, new Accumulator<MinMaxPriorityQueue<T>, T>() {
                @Override
                public MinMaxPriorityQueue<T> accumulate(MinMaxPriorityQueue<T> theQueue, T row) {
                    theQueue.offer(row);
                    return theQueue;
                }
            });

            // Now return them when asked
            return new Iterator<T>() {
                @Override
                public boolean hasNext() {
                    return !queue.isEmpty();
                }

                @Override
                public T next() {
                    return queue.poll();
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public void cleanup(Iterator<T> rowIterator) {
            // Nothing to do
        }
    });
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.ObservationCache.java

@SuppressWarnings("unchecked")
public <T> T getValueForObservation(Observation observation, EObservationCacheKey key) {
    final NycRawLocationRecord record = observation.getRecord();
    final MinMaxPriorityQueue<ObservationContents> contentsCache = _contentsByVehicleId
            .getUnchecked(record.getVehicleId());
    /*/*from w ww .  j  a v  a  2s.c om*/
     * Synchronize for integration tests, which can do crazy things.
     */
    final ObservationContents contents;
    synchronized (contentsCache) {
        contents = Iterables.find(contentsCache, new ObsSearch(observation), null);

        if (contents == null) {
            /*
             *  Add this new observation content, which, if it's truly a new obs, 
             *  will be added later
             */
            if (!contentsCache.offer(new ObservationContents(observation))) {
                return null;
            }
            return null;
        }
    }

    return (T) contents.getValueForValueType(key);
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.ObservationCache.java

public void putValueForObservation(Observation observation, EObservationCacheKey key, Object value) {
    final NycRawLocationRecord record = observation.getRecord();
    final MinMaxPriorityQueue<ObservationContents> contentsCache = _contentsByVehicleId
            .getUnchecked(record.getVehicleId());

    /*//w w  w  .  j a  va  2s .  c  o m
     * Synchronize for integration tests, which can do crazy things.
     */
    ObservationContents contents;
    synchronized (contentsCache) {
        contents = Iterables.find(contentsCache, new ObsSearch(observation), null);

        if (contents == null) {
            contents = new ObservationContents(observation);
            /*
             * If we attempt to add observations that are behind our current, 
             * then just return
             */
            if (!contentsCache.offer(contents)) {
                return;
            }
        }
    }

    contents.putValueForValueType(key, value);
}