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

The following examples show how to use com.google.common.base.Function#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: AnnotatedSubsystem.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
protected final void handleRequest(MessageReceivedEvent event, SubsystemContext<M> context) {
   PlatformMessage request = event.getMessage();
   String type = request.getMessageType();
   Function<SubsystemEventAndContext, MessageBody> handler = requestHandlers.get(type);
   if(handler == null) {
      context.sendResponse(request, Errors.invalidRequest(type));
   }
   else {
      MessageBody response;
      try {
         response = handler.apply(new SubsystemEventAndContext(context, event));
      }
      catch(Exception e) {
         context.logger().warn("Error handling request {}", request, e);
         response = Errors.fromException(e);
      }
      if(response != null) {
         context.sendResponse(request, response);
      }
   }
}
 
Example 2
Source File: Main.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static Predicate<Entry> or(List<String> queries, Function<String, Predicate<Entry>> transform) {
   Preconditions.checkNotNull(transform);
   if(queries == null || queries.isEmpty()) {
      return Predicates.alwaysTrue();
   }
   List<Predicate<Entry>> predicates = new ArrayList<>(queries.size());
   for(String query: queries) {
      Predicate<Entry> p = transform.apply(query);
      if(p != null) {
         predicates.add(p);
      }
   }
   if(predicates.isEmpty()) {
      return Predicates.alwaysTrue();
   }
   else if(predicates.size() == 1) {
      return predicates.get(0);
   }
   else {
      return Predicates.or(predicates);
   }
}
 
Example 3
Source File: MyLists.java    From molicode with Apache License 2.0 6 votes vote down vote up
/**
 * 内部转换 List
 *
 * @param fromList   来源list
 * @param function   转换工具
 * @param filterNull 是否过滤null值
 * @param <F>        入参对象
 * @param <T>        出参对象
 * @return 返回转换后的列表
 */
private static <F, T> List<T> transformInner(List<F> fromList,
                                             Function<? super F, ? extends T> function,
                                             boolean filterNull) {
    if (CollectionUtils.isEmpty(fromList)) {
        return new ArrayList<T>(0);
    }

    List<T> resultList = new ArrayList<T>(fromList.size());
    for (F f : fromList) {
        T data = function.apply(f);
        if (filterNull && data == null) {
            continue;
        }
        resultList.add(data);
    }
    return resultList;
}
 
Example 4
Source File: GraphViewerUtils.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static <V extends VisualVertex, E extends VisualEdge<V>> Function<V, Rectangle> 
	createVertexToBoundsTransformer(VisualizationServer<V, E> viewer) {
//@formatter:on

	RenderContext<V, E> context = viewer.getRenderContext();
	Function<? super V, Shape> shapeTransformer = context.getVertexShapeTransformer();
	Layout<V, E> layout = viewer.getGraphLayout();
	Function<V, Rectangle> transformer = v -> {

		Shape s = shapeTransformer.apply(v);
		Rectangle bounds = s.getBounds();
		Point2D p = layout.apply(v);

		// Note: we use the raw x/y of the layout; the view code will center the vertices
		bounds.setLocation(new Point((int) p.getX(), (int) p.getY()));
		return bounds;
	};
	return transformer;
}
 
Example 5
Source File: AbstractVisualVertexRenderer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the render context to create a compact shape for the given vertex
 * 
 * @param rc the render context
 * @param layout the layout
 * @param vertex the vertex
 * @return the vertex shape
 * @see VertexShapeProvider#getCompactShape()
 */
protected Shape getCompactShape(RenderContext<V, E> rc, Layout<V, E> layout, V vertex) {

	Function<? super V, Shape> vertexShaper = rc.getVertexShapeTransformer();
	Shape shape = null;
	if (vertexShaper instanceof VisualGraphVertexShapeTransformer) {
		@SuppressWarnings("unchecked")
		VisualGraphVertexShapeTransformer<V> vgShaper =
			(VisualGraphVertexShapeTransformer<V>) vertexShaper;

		// use the viewable shape here, as it is visually pleasing
		shape = vgShaper.transformToCompactShape(vertex);
	}
	else {
		shape = vertexShaper.apply(vertex);
	}

	return transformFromLayoutToView(rc, layout, vertex, shape);
}
 
Example 6
Source File: VisualVertexRenderer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected void paintScaledVertex(RenderContext<V, E> rc, V vertex, GraphicsDecorator g,
		Shape shape) {
	Function<? super V, Paint> fillXform = rc.getVertexFillPaintTransformer();
	Paint fillPaint = fillXform.apply(vertex);
	if (fillPaint == null) {
		return;
	}

	Paint oldPaint = g.getPaint();
	g.setPaint(fillPaint);
	g.fill(shape);

	// an outline
	g.setColor(Color.BLACK);
	g.draw(shape);

	g.setPaint(oldPaint);
}
 
Example 7
Source File: VisualEdgeRenderer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the render context to create a compact shape for the given vertex
 * 
 * @param rc the render context
 * @param layout the layout
 * @param vertex the vertex
 * @return the vertex shape
 * @see VertexShapeProvider#getCompactShape()
 */
protected Shape getCompactShape(RenderContext<V, E> rc, Layout<V, E> layout, V vertex) {

	Function<? super V, Shape> vertexShaper = rc.getVertexShapeTransformer();
	Shape shape = null;
	if (vertexShaper instanceof VisualGraphVertexShapeTransformer) {
		@SuppressWarnings("unchecked")
		VisualGraphVertexShapeTransformer<V> vgShaper =
			(VisualGraphVertexShapeTransformer<V>) vertexShaper;

		// use the viewable shape here, as it is visually pleasing
		shape = vgShaper.transformToCompactShape(vertex);
	}
	else {
		shape = vertexShaper.apply(vertex);
	}

	return transformFromLayoutToView(rc, layout, vertex, shape);
}
 
Example 8
Source File: IrisFunctions.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <I> boolean anyTrue(List<I> inputList, Function<I, Boolean> test) {
   if (inputList == null || inputList.isEmpty()) {
      return false;
   }
   for (I input : inputList) {
      if (test.apply(input)) {
         return true;
      }
   }
   return false;
}
 
Example 9
Source File: PlatformDispatcherFactory.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private <R> Consumer<PlatformMessage> wrapConsumer(Object ths, Method m, Predicate<Address> p, ArgumentResolverFactory<PlatformMessage, ?> resolver) {
   ArgumentResolverFactory<PlatformMessage, ?> factory =
         this.resolver == null ?
               resolver :
               Resolvers.chain(resolver, (ArgumentResolverFactory) this.resolver);
   Function<PlatformMessage, ?> h = 
         Methods
            .buildInvokerFactory(factory)
            .build()
            .wrapWithThis(m, ths);
   return (message) -> { if(p.apply(message.getSource())) { h.apply(message); } };
}
 
Example 10
Source File: GraphViewerUtils.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private static <V> Shape getVertexShapeForEdge(V v, Function<? super V, Shape> vertexShaper) {
	if (vertexShaper instanceof VisualGraphVertexShapeTransformer) {
		if (v instanceof VisualVertex) {
			VisualVertex vv = (VisualVertex) v;

			// Note: it is a bit odd that we 'know' to use the compact shape here for 
			// 		 hit detection, but this is how the edge is painted, so we want the 
			//       view to match the mouse.
			return ((VisualGraphVertexShapeTransformer) vertexShaper).transformToCompactShape(
				vv);
		}
	}
	return vertexShaper.apply(v);
}
 
Example 11
Source File: NotifyServiceImpl.java    From qconfig with MIT License 5 votes vote down vote up
private void doNotify(List<String> serverUrls, String uri, String type, Function<String, Request> requestBuilder) {
    List<ListenableFuture<Response>> futures = Lists.newArrayListWithCapacity(serverUrls.size());
    for (String oneServer : serverUrls) {
        String url = "http://" + oneServer + "/" + uri;
        Request request = requestBuilder.apply(url);
        ListenableFuture<Response> future = HttpListenableFuture.wrap(httpClient.executeRequest(request));
        futures.add(future);
    }

    dealResult(futures, serverUrls, type);
}
 
Example 12
Source File: IrisCollections.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <K, V> Map<K, V> toUnmodifiableMap(V[] elements, Function<V, K> keyFunction) {
   if(elements == null || elements.length == 0) {
      return Collections.emptyMap();
   }
   Map<K, V> map = new HashMap<>(elements.length);
   for(V element: elements) {
      K key = keyFunction.apply(element);
      if(key != null) {
         map.put(key, element);
      }
   }
   return Collections.unmodifiableMap(map);
}
 
Example 13
Source File: VisualGraphViewUpdater.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void ensureVertexVisible(V vertex, Rectangle area) {

		RenderContext<V, E> renderContext = primaryViewer.getRenderContext();
		Function<? super V, Shape> transformer = renderContext.getVertexShapeTransformer();
		Shape shape = transformer.apply(vertex);
		Rectangle bounds = shape.getBounds();
		ensureVertexAreaVisible(vertex, bounds, null);
	}
 
Example 14
Source File: SortUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static <T extends Comparable, E extends Comparable> Iterator<T> extractAndSort(Iterator<T> input, Function<T, E> extractor) {
    TreeMultimap<E, T> reorgnized = TreeMultimap.create();
    while (input.hasNext()) {
        T t = input.next();
        E e = extractor.apply(t);
        reorgnized.put(e, t);
    }
    return reorgnized.values().iterator();
}
 
Example 15
Source File: MyLists.java    From molicode with Apache License 2.0 5 votes vote down vote up
/**
 * 将列表转化为指定map对象
 *
 * @param list          列表
 * @param functionKey   key转化函数
 * @param functionValue
 * @param <S>           列表元素类型
 * @param <T>           mapping key
 * @return map 对象
 */
public static <S, T, U> Map<S, T> transformToMap(List<U> list, Function<U, S> functionKey, Function<U, T> functionValue) {
    if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
        return Maps.newHashMapWithExpectedSize(0);
    }

    Map<S, T> map = Maps.newHashMap();
    for (U element : list) {
        if (functionValue.apply(element) != null) {
            map.put(functionKey.apply(element), functionValue.apply(element));
        }
    }
    return map;
}
 
Example 16
Source File: JdbcTemplateDelegated.java    From qconfig with MIT License 5 votes vote down vote up
public <T> int batchUpdate(String sql, List<T> entities, Function<T, Object[]> function) {
    int result = 0;
    List<Object[]> paramList = Lists.newLinkedList();
    for (T entity : entities) {
        Object[] params = function.apply(entity);
        paramList.add(params);
    }
    for (JdbcTemplate jdbcTemplate : jdbcTemplates.values()) {
        int[] resultArray = jdbcTemplate.batchUpdate(sql, paramList);
        for (int count : resultArray) {
            result += count;
        }
    }
    return result;
}
 
Example 17
Source File: ProcessUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private static void processOutput(InputStream inputStream, Function<String, Void> function) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
        String line = null;
        while ((line = reader.readLine()) != null) {
            function.apply(line);
        }
    }
}
 
Example 18
Source File: IrisCollections.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <K, V> Map<K, V> toUnmodifiableMap(Collection<V> elements, Function<V, K> keyFunction) {
 if(elements == null || elements.isEmpty()) {
 	return Collections.emptyMap();
 }
 Map<K, V> map = new HashMap<>(elements.size());
 for(V element: elements) {
 	K key = keyFunction.apply(element);
 	if(key != null) {
 		map.put(key, element);
 	}
 }
 return Collections.unmodifiableMap(map);
}
 
Example 19
Source File: List827.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
public static <T> void forEach(List<T> list, Function<T, Void> function) {
    for (T t : list) {
        function.apply(t);
    }
}
 
Example 20
Source File: RegexDfaByte.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public <T> State<T> transform(Function<V,T> transformer) {
   return new State<T>(type, transformer.apply(value));
}