Example usage for com.google.common.collect Lists reverse

List of usage examples for com.google.common.collect Lists reverse

Introduction

In this page you can find the example usage for com.google.common.collect Lists reverse.

Prototype

@CheckReturnValue
public static <T> List<T> reverse(List<T> list) 

Source Link

Document

Returns a reversed view of the specified list.

Usage

From source file:multitypetree.distributions.StructuredCoalescentTreeDensity.java

/**
 * Determines the sequence of migration, coalescence and sampling events
 * which make up the coloured tree.// w  ww  .j  ava  2s.c  om
 */
public void updateEventSequence() {

    // Clean up previous list:
    eventList.clear();
    lineageCountList.clear();
    Node rootNode = mtTree.getRoot();

    // Initialise map of active nodes to active change indices:
    Map<Node, Integer> changeIdx = new HashMap<>();
    changeIdx.put(rootNode, -1);

    // Initialise lineage count per colour array:
    Integer[] lineageCount = new Integer[migrationModel.getNTypes()];
    for (int c = 0; c < migrationModel.getNTypes(); c++)
        if (c == ((MultiTypeNode) rootNode).getNodeType())
            lineageCount[c] = 1;
        else
            lineageCount[c] = 0;

    // Calculate event sequence:
    while (!changeIdx.isEmpty()) {

        SCEvent nextEvent = new SCEvent();
        nextEvent.time = Double.NEGATIVE_INFINITY;
        nextEvent.node = rootNode; // Initial assignment not significant

        // Determine next event
        for (Node node : changeIdx.keySet())
            if (changeIdx.get(node) < 0) {
                if (node.isLeaf()) {
                    // Next event is a sample
                    if (node.getHeight() > nextEvent.time) {
                        nextEvent.time = node.getHeight();
                        nextEvent.kind = SCEventKind.SAMPLE;
                        nextEvent.type = ((MultiTypeNode) node).getNodeType();
                        nextEvent.node = node;
                    }
                } else {
                    // Next event is a coalescence
                    if (node.getHeight() > nextEvent.time) {
                        nextEvent.time = node.getHeight();
                        nextEvent.kind = SCEventKind.COALESCE;
                        nextEvent.type = ((MultiTypeNode) node).getNodeType();
                        nextEvent.node = node;
                    }
                }
            } else {
                // Next event is a migration
                double thisChangeTime = ((MultiTypeNode) node).getChangeTime(changeIdx.get(node));
                if (thisChangeTime > nextEvent.time) {
                    nextEvent.time = thisChangeTime;
                    nextEvent.kind = SCEventKind.MIGRATE;
                    nextEvent.destType = ((MultiTypeNode) node).getChangeType(changeIdx.get(node));
                    if (changeIdx.get(node) > 0)
                        nextEvent.type = ((MultiTypeNode) node).getChangeType(changeIdx.get(node) - 1);
                    else
                        nextEvent.type = ((MultiTypeNode) node).getNodeType();
                    nextEvent.node = node;
                }
            }

        // Update active node list (changeIdx) and lineage count appropriately:
        switch (nextEvent.kind) {
        case COALESCE:
            Node leftChild = nextEvent.node.getLeft();
            Node rightChild = nextEvent.node.getRight();

            changeIdx.remove(nextEvent.node);
            changeIdx.put(leftChild, ((MultiTypeNode) leftChild).getChangeCount() - 1);
            changeIdx.put(rightChild, ((MultiTypeNode) rightChild).getChangeCount() - 1);
            lineageCount[nextEvent.type]++;
            break;

        case SAMPLE:
            changeIdx.remove(nextEvent.node);
            lineageCount[nextEvent.type]--;
            break;

        case MIGRATE:
            lineageCount[nextEvent.destType]--;
            lineageCount[nextEvent.type]++;
            int oldIdx = changeIdx.get(nextEvent.node);
            changeIdx.put(nextEvent.node, oldIdx - 1);
            break;
        }

        // Add event to list:
        eventList.add(nextEvent);
        lineageCountList.add(Arrays.copyOf(lineageCount, lineageCount.length));
    }

    // Reverse event and lineage count lists (order them from tips to root):
    eventList = Lists.reverse(eventList);
    lineageCountList = Lists.reverse(lineageCountList);

}

From source file:com.streamsets.pipeline.lib.xml.StreamingXmlParser.java

public String getXpathPrefix() {
    return "/" + StringUtils.join(Lists.reverse(elementNameStack), "/");
}

From source file:fredboat.audio.player.AbstractPlayer.java

public List<AudioTrackContext> getTracksInHistory(int start, int end) {
    start = Math.max(start, 0);//w  ww  .  j a  va  2s .  c  om
    end = Math.max(end, start);
    List<AudioTrackContext> historyList = new ArrayList<>(historyQueue);

    if (historyList.size() >= end) {
        return Lists.reverse(new ArrayList<>(historyQueue)).subList(start, end);
    } else {
        return new ArrayList<>();
    }
}

From source file:org.apache.druid.indexing.firehose.IngestSegmentFirehoseFactory.java

@VisibleForTesting
static List<String> getUniqueMetrics(List<TimelineObjectHolder<String, DataSegment>> timelineSegments) {
    final BiMap<String, Integer> uniqueMetrics = HashBiMap.create();

    // Here, we try to retain the order of metrics as they were specified. Metrics are extracted from the recent
    // segments to olders.

    // timelineSegments are sorted in order of interval
    int index = 0;
    for (TimelineObjectHolder<String, DataSegment> timelineHolder : Lists.reverse(timelineSegments)) {
        for (PartitionChunk<DataSegment> chunk : timelineHolder.getObject()) {
            for (String metric : chunk.getObject().getMetrics()) {
                if (!uniqueMetrics.containsKey(metric)) {
                    uniqueMetrics.put(metric, index++);
                }//ww  w  .  ja  v  a  2s. c om
            }
        }
    }

    final BiMap<Integer, String> orderedMetrics = uniqueMetrics.inverse();
    return IntStream.range(0, orderedMetrics.size()).mapToObj(orderedMetrics::get).collect(Collectors.toList());
}

From source file:org.apache.calcite.sql.advise.SqlAdvisor.java

private static boolean isSelectListItem(SqlNode root, final SqlParserPos pos) {
    List<SqlNode> nodes = SqlUtil.getAncestry(root, new PredicateImpl<SqlNode>() {
        public boolean test(SqlNode input) {
            return input instanceof SqlIdentifier
                    && Util.last(((SqlIdentifier) input).names).equals(UPPER_HINT_TOKEN);
        }/*from w  ww.  ja  v  a  2  s.co  m*/
    }, new PredicateImpl<SqlNode>() {
        public boolean test(SqlNode input) {
            return input.getParserPosition().startsAt(pos);
        }
    });
    assert nodes.get(0) == root;
    nodes = Lists.reverse(nodes);
    return nodes.size() > 2 && nodes.get(2) instanceof SqlSelect
            && nodes.get(1) == ((SqlSelect) nodes.get(2)).getSelectList();
}

From source file:org.atteo.moonshine.services.ServicesImplementation.java

@Override
public void stop() {
    for (LifeCycleListener listener : listeners) {
        listener.stopping();/*from   www. ja  v a  2  s . c o m*/
    }
    for (ServiceWrapper service : Lists.reverse(services)) {
        service.stop();
    }
}

From source file:com.searchcode.app.util.LoggerWrapper.java

public synchronized List<String> getSearchLogs() {
    List<String> values = new ArrayList<>();
    try {/* w ww.  j  a  v a2s  . c  om*/
        values = new ArrayList(this.searchLog);
        values = Lists.reverse(values);
    } catch (ArrayIndexOutOfBoundsException ignored) {
    }

    return values;
}

From source file:co.cask.cdap.logging.meta.FileMetaDataReader.java

@VisibleForTesting
List<LogLocation> getFilesInRange(List<LogLocation> files, long startTimeInMs) {
    // return if its empty
    if (files.isEmpty()) {
        return files;
    }/*w  ww . j ava2 s.c o  m*/
    // sort the list
    Collections.sort(files, LOG_LOCATION_COMPARATOR);

    // iterate the list from the end
    // we continue when the start timestamp of the log file is higher than the startTimeInMs
    // when we reach a file where start time is lower than the startTimeInMs we return the list from this index.
    // as the files with same startTimeInMs is sorted by creation timestamp,
    // we will return the file with most recent creation time - which is the expected behavior.
    // if we reach the beginning of the list, we return the entire list.
    List<LogLocation> filteredList = new ArrayList<>();
    for (LogLocation logLocation : Lists.reverse(files)) {
        long eventTimestamp = logLocation.getEventTimeMs();
        filteredList.add(0, logLocation);
        if (eventTimestamp < startTimeInMs) {
            break;
        }
    }
    return filteredList;
}

From source file:com.synformation.boundary.ClosedBoundary.java

private List<Node> reverseNodeList(final List<Node> nodeList) {
    List<Node> reversedListView = Lists.reverse(nodeList);
    List<Node> newReversedList = new LinkedList<>();
    newReversedList.addAll(reversedListView);
    return newReversedList;
}

From source file:gobblin.util.PullFileLoader.java

/**
 * Load at most one *.properties files from path and each ancestor of path up to and including {@link #rootDirectory}.
 * Higher directories will serve as fallback for lower directories, and sysProps will serve as fallback for all of them.
 * @throws IOException/*from   w w  w.j av a  2s  .c  o  m*/
 */
private Config loadAncestorGlobalConfigs(Path path, Config sysProps) throws IOException {
    Config config = sysProps;

    if (!PathUtils.isAncestor(this.rootDirectory, path)) {
        log.warn(String.format(
                "Loaded path %s is not a descendant of root path %s. Cannot load global properties.", path,
                this.rootDirectory));
    } else {

        List<Path> ancestorPaths = Lists.newArrayList();
        while (PathUtils.isAncestor(this.rootDirectory, path)) {
            ancestorPaths.add(path);
            path = path.getParent();
        }

        List<Path> reversedAncestors = Lists.reverse(ancestorPaths);
        for (Path ancestor : reversedAncestors) {
            config = findAndLoadGlobalConfigInDirectory(ancestor, config);
        }
    }
    return config;
}