Java Code Examples for com.google.common.base.Predicate#apply()

The following examples show how to use com.google.common.base.Predicate#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return The number of times the the predicate is true for the node
 * or any of its children.
 */
static int getCount(
    Node n, Predicate<Node> pred, Predicate<Node> traverseChildrenPred) {
  int total = 0;

  if (pred.apply(n)) {
    total++;
  }

  if (traverseChildrenPred.apply(n)) {
    for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
      total += getCount(c, pred, traverseChildrenPred);
    }
  }

  return total;
}
 
Example 2
Source File: Cardumen_0014_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return The number of times the the predicate is true for the node
 * or any of its children.
 */
static int getCount(
    Node n, Predicate<Node> pred, Predicate<Node> traverseChildrenPred) {
  int total = 0;

  if (pred.apply(n)) {
    total++;
  }

  if (traverseChildrenPred.apply(n)) {
    for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
      total += getCount(c, pred, traverseChildrenPred);
    }
  }

  return total;
}
 
Example 3
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the predicate is true for the node or any of its children.
 */
static boolean has(Node node,
                   Predicate<Node> pred,
                   Predicate<Node> traverseChildrenPred) {
  if (pred.apply(node)) {
    return true;
  }

  if (!traverseChildrenPred.apply(node)) {
    return false;
  }

  for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
    if (has(c, pred, traverseChildrenPred)) {
      return true;
    }
  }

  return false;
}
 
Example 4
Source File: ZkStateStore.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Get store names in the state store
 *
 * @param predicate only returns names matching predicate
 * @return (possibly empty) list of store names from the given store
 * @throws IOException
 */
public List<String> getStoreNames(Predicate<String> predicate)
    throws IOException {
  List<String> names = Lists.newArrayList();
  String path = formPath("");

  List<String> children = propStore.getChildNames(path, 0);

  if (children != null) {
    for (String c : children) {
      if (predicate.apply(c)) {
        names.add(c);
      }
    }
  }

  return names;
}
 
Example 5
Source File: Graphs.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates a mutable copy of {@code graph}, using all of its elements that satisfy
 * {@code nodePredicate} and {@code edgePredicate}.
 */
public static <N, E> MutableNetwork<N, E> copyOf(
    Network<N, E> graph,
    Predicate<? super N> nodePredicate,
    Predicate<? super E> edgePredicate) {
  checkNotNull(graph, "graph");
  checkNotNull(nodePredicate, "nodePredicate");
  checkNotNull(edgePredicate, "edgePredicate");
  MutableNetwork<N, E> copy = NetworkBuilder.from(graph)
      .expectedNodeCount(graph.nodes().size()).expectedEdgeCount(graph.edges().size()).build();
  mergeNodesFrom(graph, copy, nodePredicate);

  // We can't just call mergeEdgesFrom(graph, copy, edgePredicate) because addEdge() can add
  // the edge's incident nodes if they are not present.
  for (E edge : graph.edges()) {
    if (edgePredicate.apply(edge)) {
      Set<N> incidentNodes = graph.incidentNodes(edge);
      if (copy.nodes().containsAll(incidentNodes)) {
        addEdge(copy, edge, incidentNodes);
      }
    }
  }

  return copy;
}
 
Example 6
Source File: Cardumen_0014_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the predicate is true for the node or any of its children.
 */
static boolean has(Node node,
                   Predicate<Node> pred,
                   Predicate<Node> traverseChildrenPred) {
  if (pred.apply(node)) {
    return true;
  }

  if (!traverseChildrenPred.apply(node)) {
    return false;
  }

  for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
    if (has(c, pred, traverseChildrenPred)) {
      return true;
    }
  }

  return false;
}
 
Example 7
Source File: Iterators.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns a view of {@code unfiltered} containing all elements that satisfy
 * the input predicate {@code retainIfTrue}.
 */
public static <T> UnmodifiableIterator<T> filter(
    final Iterator<T> unfiltered, final Predicate<? super T> retainIfTrue) {
  checkNotNull(unfiltered);
  checkNotNull(retainIfTrue);
  return new AbstractIterator<T>() {
    @Override
    protected T computeNext() {
      while (unfiltered.hasNext()) {
        T element = unfiltered.next();
        if (retainIfTrue.apply(element)) {
          return element;
        }
      }
      return endOfData();
    }
  };
}
 
Example 8
Source File: jKali_003_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Apply the supplied predicate against
 * all possible result Nodes of the expression.
 */
static boolean allResultsMatch(Node n, Predicate<Node> p) {
  switch (n.getType()) {
    case Token.ASSIGN:
    case Token.COMMA:
      return allResultsMatch(n.getLastChild(), p);
    case Token.AND:
    case Token.OR:
      return allResultsMatch(n.getFirstChild(), p)
          && allResultsMatch(n.getLastChild(), p);
    case Token.HOOK:
      return allResultsMatch(n.getFirstChild().getNext(), p)
          && allResultsMatch(n.getLastChild(), p);
    default:
      return p.apply(n);
  }
}
 
Example 9
Source File: AbstractSemanticRegionsFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
	Preconditions.checkNotNull(kw1);
	Preconditions.checkNotNull(kw2);
	Preconditions.checkArgument(!kw1.equals(kw2));
	Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
	Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
	List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
	Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
	LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
	for (ISemanticRegion region : all) {
		if (p1.apply(region))
			stack.push(region);
		else {
			AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
			while (!stack.isEmpty()) {
				ISemanticRegion candidate = stack.pop();
				if (region.getSemanticElement() == candidate.getSemanticElement()) {
					AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
					if (regionRule == candidateRule) {
						result.add(Pair.of(candidate, region));
						break;
					}
				}
			}
		}
	}
	return result.build();
}
 
Example 10
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A pre-order traversal, calling Vistor.visit for each child matching
 * the predicate.
 */
static void visitPreOrder(Node node,
                   Visitor vistor,
                   Predicate<Node> traverseChildrenPred) {
  vistor.visit(node);

  if (traverseChildrenPred.apply(node)) {
    for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
      visitPreOrder(c, vistor, traverseChildrenPred);
    }
  }
}
 
Example 11
Source File: SemanticRegionInIterableFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ISemanticRegion findFirst(Predicate<ISemanticRegion> predicate) {
	for (ISemanticRegion region : regions)
		if (predicate.apply(region))
			return region;
	return null;
}
 
Example 12
Source File: Cardumen_0014_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A pre-order traversal, calling Visitor.visit for each child matching
 * the predicate.
 */
static void visitPreOrder(Node node,
                   Visitor visitor,
                   Predicate<Node> traverseChildrenPred) {
  visitor.visit(node);

  if (traverseChildrenPred.apply(node)) {
    for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
      visitPreOrder(c, visitor, traverseChildrenPred);
    }
  }
}
 
Example 13
Source File: Cardumen_0087_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A pre-order traversal, calling Visitor.visit for each child matching
 * the predicate.
 */
static void visitPreOrder(Node node,
                   Visitor visitor,
                   Predicate<Node> traverseChildrenPred) {
  visitor.visit(node);

  if (traverseChildrenPred.apply(node)) {
    for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
      visitPreOrder(c, visitor, traverseChildrenPred);
    }
  }
}
 
Example 14
Source File: TreeTableContainer.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void applyPreservingExpansionState(ModelObject rootObject, Predicate<ModelObject> callable) {
  MutableTreeTableNode rootNode = getNode(rootObject);
  List<MutableTreeTableNode> subtree = TreeUtil.collectSubtree(rootNode);
  Collections.reverse(subtree);
  LinkedHashMap<ModelObject, Boolean> states = Maps.newLinkedHashMap();
  for (MutableTreeTableNode node : subtree) {
    int row = myTreeTable.getTree().getRowForPath(TreeUtil.createPath(node));
    states.put((ModelObject)node.getUserObject(), myTreeTable.getTree().isExpanded(row));
  }
  callable.apply(rootObject);
  for (Map.Entry<ModelObject, Boolean> state : states.entrySet()) {
    setExpanded(state.getKey(), state.getValue());
  }
}
 
Example 15
Source File: ArrayUtils.java    From opoopress with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the first element in {@code array} that satisfies the given
 * predicate.
 *
 * @param array
 * @param predicate
 * @param <T>
 * @return
 */
public static <T> T find(T[] array, Predicate<T> predicate){
    if(array == null){
        return null;
    }
    for(T t: array){
        if(predicate.apply(t)){
            return t;
        }
    }
    return null;
}
 
Example 16
Source File: SpyService.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private SpyPlugIn findOne(Collection<SpyPlugIn> collection, Predicate<SpyPlugIn> test) {
   if (collection != null) {
      for (SpyPlugIn item : collection) {
         if (test.apply(item)) {
            return item;
         }
      }
   }
   return null;
}
 
Example 17
Source File: DefaultWidgetTypesRegistry.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> List<IWidgetType<T>> lookupByDomain(String domain, Predicate<IWidgetType<T>> filter) {
	List<IWidgetType<T>> ret = new ArrayList<IWidgetType<T>>();
	for(IWidgetType<?> description : widgetDescriptions)
	{
		if(domain.equals(description.getDomain())
				&& (filter==null || filter.apply((IWidgetType<T>)description))) ret.add((IWidgetType<T>)description);
	}
	return Collections.unmodifiableList(ret);
}
 
Example 18
Source File: ClosureClient.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <In> boolean useGoogleCollectPredicate(Predicate<In> predicate, In value) {
	return predicate.apply(value);
}
 
Example 19
Source File: Cardumen_00200_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 20
Source File: EObjectUtil.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Find a direct or indirect container that satisfies a given predicate. If no such container exists, return null. If the
 * object itself satisfies it, return the object.
 *
 * @param obj
 *          the object
 * @param predicate
 *          the predicate
 * @return The container, as described above.
 */
public static EObject eContainer(final EObject obj, final Predicate<EObject> predicate) {
  for (EObject e = obj; e != null; e = e.eContainer()) {
    if (predicate.apply(e)) {
      return e;
    }
  }
  return null;
}