Example usage for com.google.common.collect Iterables indexOf

List of usage examples for com.google.common.collect Iterables indexOf

Introduction

In this page you can find the example usage for com.google.common.collect Iterables indexOf.

Prototype

public static <T> int indexOf(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns the index in iterable of the first element that satisfies the provided predicate , or -1 if the Iterable has no such elements.

Usage

From source file:net.grappendorf.doitlater.TaskListActivity.java

@SuppressWarnings("unchecked")
private void deleteTaskItem(final String taskId) {
    int taskIndex = Iterables.indexOf(tasks, new Predicate<Task>() {
        @Override/*from w w  w  .  j a v a  2s  . c o m*/
        public boolean apply(@Nullable Task task) {
            return task != null && task.getId().equals(taskId);
        }
    });
    if (taskIndex >= 0) {
        tasks.remove(taskIndex);
        ((ArrayAdapter<Task>) getListAdapter()).notifyDataSetChanged();
    }
}

From source file:net.grappendorf.doitlater.TaskListActivity.java

@SuppressWarnings("unchecked")
private void completeTaskItem(final Task task) {
    int taskIndex = Iterables.indexOf(tasks, new Predicate<Task>() {
        @Override//ww w.  j  av a  2s.c  o  m
        public boolean apply(@Nullable Task aTask) {
            return aTask != null && aTask.getId().equals(task.getId());
        }
    });
    if (taskIndex >= 0) {
        tasks.set(taskIndex, task);
        ((ArrayAdapter<Task>) getListAdapter()).notifyDataSetChanged();
    }
}

From source file:com.b2international.index.es.EsDocumentSearcher.java

private Iterable<SortBy> getSortFields(SortBy sortBy) {
    final List<SortBy> items = newArrayList();

    // Unpack the top level multi-sort if present
    if (sortBy instanceof MultiSortBy) {
        items.addAll(((MultiSortBy) sortBy).getItems());
    } else {//from   w w w. j a v  a 2 s.c  o  m
        items.add(sortBy);
    }

    final int idSortIdx = Iterables.indexOf(items, item -> {
        return (item instanceof SortByField) && DocumentMapping._ID.equals(((SortByField) item).getField());
    });

    if (idSortIdx >= 0 && items.size() > 1) {
        // Remove all sort conditions after _id, as these values should be unique
        for (int i = items.size() - 1; i > idSortIdx; i--) {
            items.remove(i);
        }
    } else {
        // Add _id as a tie-breaker to the end
        items.add(SortBy.DOC_ID);
    }

    return Iterables.filter(items, SortBy.class);
}

From source file:org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.java

private int finalizerTaskPosition(TaskInfo finalizer, final List<TaskInfoInVisitingSegment> nodeQueue) {
    if (nodeQueue.size() == 0) {
        return 0;
    }//  w  w  w  .j  av a  2  s. c om

    ArrayList<TaskInfo> dependsOnTasks = new ArrayList<TaskInfo>();
    dependsOnTasks.addAll(finalizer.getDependencySuccessors());
    dependsOnTasks.addAll(finalizer.getMustSuccessors());
    dependsOnTasks.addAll(finalizer.getShouldSuccessors());
    List<Integer> dependsOnTaskIndexes = CollectionUtils.collect(dependsOnTasks,
            new Transformer<Integer, TaskInfo>() {
                public Integer transform(final TaskInfo dependsOnTask) {
                    return Iterables.indexOf(nodeQueue, new Predicate<TaskInfoInVisitingSegment>() {
                        public boolean apply(TaskInfoInVisitingSegment taskInfoInVisitingSegment) {
                            return taskInfoInVisitingSegment.taskInfo.equals(dependsOnTask);
                        }
                    });
                }
            });
    return Collections.max(dependsOnTaskIndexes) + 1;
}

From source file:org.gradle.execution.plan.DefaultExecutionPlan.java

/**
 * Given a finalizer task, determine where in the current node queue that it should be inserted.
 * The finalizer should be inserted after any of it's preceding tasks.
 *//*from   w  w  w  .j  a  v  a  2s.  com*/
private int finalizerTaskPosition(Node finalizer, final List<NodeInVisitingSegment> nodeQueue) {
    if (nodeQueue.size() == 0) {
        return 0;
    }

    Set<Node> precedingTasks = getAllPrecedingNodes(finalizer);
    Set<Integer> precedingTaskIndices = CollectionUtils.collect(precedingTasks,
            new Transformer<Integer, Node>() {
                @Override
                public Integer transform(final Node dependsOnTask) {
                    return Iterables.indexOf(nodeQueue, new Predicate<NodeInVisitingSegment>() {
                        @Override
                        @SuppressWarnings("NullableProblems")
                        public boolean apply(NodeInVisitingSegment nodeInVisitingSegment) {
                            return nodeInVisitingSegment.node.equals(dependsOnTask);
                        }
                    });
                }
            });
    return Collections.max(precedingTaskIndices) + 1;
}

From source file:org.apache.hadoop.hdfs.server.datanode.DatanodeJspHelper.java

private static String generateLinksForAdjacentBlock(final int direction, String authority, int datanodePort,
        long startOffset, int chunkSizeToView, long blockSize, long blockId, Long genStamp, final DFSClient dfs,
        final String filename, final Configuration conf, final String scheme, final String tokenString,
        final int namenodeInfoPort, final String nnAddr)
        throws AccessControlException, FileNotFoundException, UnresolvedLinkException, IOException {

    boolean found = false;
    if ((direction == NEXT_BLOCK && startOffset + chunkSizeToView < blockSize)
            || (direction == PREV_BLOCK && startOffset != 0)) {
        // we are in the same block
        found = true;/*w  w w .java 2 s.c o m*/

        if (direction == NEXT_BLOCK) {
            startOffset = startOffset + chunkSizeToView;
        } else {
            startOffset = Math.max(0, startOffset - chunkSizeToView);
        }
    } else {
        List<LocatedBlock> blocks = dfs.getNamenode().getBlockLocations(filename, 0, Long.MAX_VALUE)
                .getLocatedBlocks();

        final long curBlockId = blockId;
        int curBlockIdx = Iterables.indexOf(blocks, new Predicate<LocatedBlock>() {
            @Override
            public boolean apply(LocatedBlock b) {
                return b.getBlock().getBlockId() == curBlockId;
            }
        });
        found = curBlockIdx != -1 && ((direction == NEXT_BLOCK && curBlockIdx < blocks.size() - 1)
                || (direction == PREV_BLOCK && curBlockIdx > 0));

        if (found) {
            LocatedBlock nextBlock = blocks.get(curBlockIdx + direction);

            blockId = nextBlock.getBlock().getBlockId();
            genStamp = nextBlock.getBlock().getGenerationStamp();
            startOffset = 0;
            blockSize = nextBlock.getBlock().getNumBytes();
            DatanodeInfo d = JspHelper.bestNode(nextBlock, conf);
            datanodePort = d.getXferPort();
            authority = JspHelper.Url.authority(scheme, d);
        }
    }

    if (found) {
        return "///" + authority + "/browseBlock.jsp?blockId=" + blockId + "&blockSize=" + blockSize
                + "&startOffset=" + startOffset + "&genstamp=" + genStamp + "&filename="
                + URLEncoder.encode(filename, "UTF-8") + "&chunkSizeToView=" + chunkSizeToView
                + "&datanodePort=" + datanodePort + "&namenodeInfoPort=" + namenodeInfoPort
                + JspHelper.getDelegationTokenUrlParam(tokenString)
                + JspHelper.getUrlParam(JspHelper.NAMENODE_ADDRESS, nnAddr);
    } else {
        return null;
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopWindow.java

@Override
public int indexOf(Component component) {
    return Iterables.indexOf(ownComponents, c -> c == component);
}

From source file:r.base.Types.java

@Primitive
public static SEXP inherits(SEXP exp, StringVector what, boolean which) {
    if (!which) {
        return new LogicalVector(inherits(exp, what));
    }/*  w w w  .ja  va 2  s  . com*/
    StringVector classes = getClass(exp);
    int result[] = new int[what.length()];

    for (int i = 0; i != what.length(); ++i) {
        result[i] = Iterables.indexOf(classes, Predicates.equalTo(what.getElement(i))) + 1;
    }
    return new IntVector(result);
}