下面列出了com.google.common.base.Function#apply ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
/**
* 内部转换 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;
}
/**
* 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);
}
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);
}
/**
* 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);
}
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;
}
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);
}
}
}
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);
}
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);
}
}
}
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;
}
/**
* 将列表转化为指定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;
}
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();
}
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);
}
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);
}
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;
}
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);
}
@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);
}
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); } };
}
public static <T> void forEach(List<T> list, Function<T, Void> function) {
for (T t : list) {
function.apply(t);
}
}
public <T> State<T> transform(Function<V,T> transformer) {
return new State<T>(type, transformer.apply(value));
}