Example usage for java.util.function BiFunction apply

List of usage examples for java.util.function BiFunction apply

Introduction

In this page you can find the example usage for java.util.function BiFunction apply.

Prototype

R apply(T t, U u);

Source Link

Document

Applies this function to the given arguments.

Usage

From source file:com.hortonworks.streamline.streams.security.service.SecurityCatalogResource.java

private void checkAclOp(AclEntry aclEntry, SecurityContext securityContext,
        BiFunction<AclEntry, SecurityContext, Boolean> fn) {
    if (!fn.apply(aclEntry, securityContext)) {
        throw new WebserviceAuthorizationException("Principal: " + securityContext.getUserPrincipal()
                + " is neither a security admin " + "nor have necessary ACLs for the requested operation");
    }/*from  w w w  .  j  ava 2  s .  c  o  m*/
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Creates a stream that combines two other streams, continuing until either stream ends.
 * <p>/* w w  w  .  j av  a2 s.  c  o m*/
 * The combiner function is called once for each pair of objects found in the input streams.
 * @param <A> the type of the first stream
 * @param <B> the type of the second stream
 * @param <R> the type of the resulting stream
 * @param stream1 the first stream
 * @param stream2 the first stream
 * @param zipper the function used to combine the pair of objects
 * @return a stream of pairs, one from each stream
 */
private static <A, B, R> Stream<R> zip(Stream<A> stream1, Stream<B> stream2, BiFunction<A, B, R> zipper) {
    // this is private for now, to see if it is really needed on the API
    // it suffers from generics problems at the call site with common zipper functions
    // as such, it is less useful than it might seem
    Spliterator<A> split1 = stream1.spliterator();
    Spliterator<B> split2 = stream2.spliterator();
    // merged stream lacks some characteristics
    int characteristics = split1.characteristics() & split2.characteristics()
            & ~(Spliterator.DISTINCT | Spliterator.SORTED);
    long size = Math.min(split1.getExactSizeIfKnown(), split2.getExactSizeIfKnown());

    Iterator<A> it1 = Spliterators.iterator(split1);
    Iterator<B> it2 = Spliterators.iterator(split2);
    Iterator<R> it = new Iterator<R>() {
        @Override
        public boolean hasNext() {
            return it1.hasNext() && it2.hasNext();
        }

        @Override
        public R next() {
            return zipper.apply(it1.next(), it2.next());
        }
    };
    Spliterator<R> split = Spliterators.spliterator(it, size, characteristics);
    return StreamSupport.stream(split, false);
}

From source file:org.sonar.api.server.ws.Request.java

@Beta
public <T> Param<T> getParam(String key, BiFunction<Request, String, T> retrieveAndValidate) {
    String param = this.param(key);
    if (param != null) {
        return GenericParam.present(retrieveAndValidate.apply(this, key));
    }// w  w  w  .ja v  a2s .com
    return AbsentParam.absent();
}

From source file:org.trustedanalytics.cloud.cc.CcClientTest.java

private void check_get_for_object(BiFunction<UUID, Role, Collection<User>> fun, String apiUrl) {
    UUID guid = UUID.randomUUID();

    when(template.getForObject(anyString(), any(), any(UUID.class))).thenReturn(mock(CcOrgUsersList.class));
    fun.apply(guid, Role.DEVELOPERS);
    verify(template).getForObject(eq(apiUrl), any(), any(UUID.class));

}

From source file:org.onosproject.store.cluster.messaging.impl.NettyMessagingManager.java

@Override
public void registerHandler(String type, BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler) {
    checkPermission(CLUSTER_WRITE);//www  .  j a va  2s.c  om
    handlers.put(type, message -> {
        handler.apply(message.sender(), message.payload()).whenComplete((result, error) -> {
            Status status = error == null ? Status.OK : Status.ERROR_HANDLER_EXCEPTION;
            sendReply(message, status, Optional.ofNullable(result));
        });
    });
}

From source file:com.intuit.wasabi.api.pagination.comparators.PaginationComparator.java

/**
 * Compares two objects by one of their properties.
 * If a {@link NullPointerException} occurs while trying to access a property of one of the two objects,
 * the property is handles as {@code null}. For example if {@code myHouse.getSecondFloor().getWindow()} would
 * throw a {@link NullPointerException} because it only has a ground level, then this will be caught and the
 * window to be compared will be treated as {@code null}.
 * <p>//from  w  w  w. j  a v  a  2  s.  c  om
 * Both extracted properties are first compared according to their {@code null} value (see
 * {@link #compareNull(Object, Object, boolean)}). If both are non-null values, then they comparison function
 * is applied. This way the comparison function does not need to care about {@code null} values.
 * <p>
 * If {@code descending} is set, the results are multiplied by {@code -1} at the end (however, {@code null}
 * values are always considered to be bigger, so that they appear after relevant values).
 *
 * @param left              left object
 * @param right             right object
 * @param propertyExtractor will be applied to both objects to extract the properties to be compared
 * @param comparisonFunc    compares the two extracted properties if both are not null
 * @param descending        if true, the order of the values is changed
 * @param <V>               type of the extracted property.
 * @return -1, 0, 1
 */
/*test*/ <V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor,
        BiFunction<V, V, Integer> comparisonFunc, boolean descending) {
    V property1 = null;
    V property2 = null;

    try {
        property1 = propertyExtractor.apply(left);
    } catch (NullPointerException ignored) {
    }
    try {
        property2 = propertyExtractor.apply(right);
    } catch (NullPointerException ignored) {
    }

    int result;
    if ((result = compareNull(property1, property2, descending)) == 2) {
        result = comparisonFunc.apply(property1, property2);
    }
    return result * (descending ? -1 : 1);
}

From source file:org.springframework.cloud.sleuth.instrument.web.client.TraceWebClientAutoConfiguration.java

private Publisher<Void> handle(
        BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>> handler,
        HttpClientRequest req, NettyOutbound nettyOutbound) {
    if (handler != null) {
        return handler.apply(req, nettyOutbound);
    }/*www  .  j  a v a  2 s.  c  om*/
    return nettyOutbound;
}

From source file:at.gridtec.lambda4j.function.bi.ThrowableBiFunction.java

/**
 * Returns a composed {@link BiFunction2} that first applies this function to its input, and then applies the {@code
 * recover} operation if a {@link Throwable} is thrown from this one. The {@code recover} operation is represented
 * by a curried operation which is called with throwable information and same arguments of this function.
 *
 * @param recover The operation to apply if this function throws a {@code Throwable}
 * @return A composed {@link BiFunction2} that first applies this function to its input, and then applies the {@code
 * recover} operation if a {@code Throwable} is thrown from this one.
 * @throws NullPointerException If given argument or the returned enclosing function is {@code null}
 * @implSpec The implementation checks that the returned enclosing function from {@code recover} operation is not
 * {@code null}. If it is, then a {@link NullPointerException} with appropriate message is thrown.
 * @implNote If thrown {@code Throwable} is of type {@link Error}, it is thrown as-is and thus not passed to {@code
 * recover} operation.//from  ww w  .  jav a  2  s.co  m
 */
@Nonnull
default BiFunction2<T, U, R> recover(
        @Nonnull final Function<? super Throwable, ? extends BiFunction<? super T, ? super U, ? extends R>> recover) {
    Objects.requireNonNull(recover);
    return (t, u) -> {
        try {
            return this.applyThrows(t, u);
        } catch (Error e) {
            throw e;
        } catch (Throwable throwable) {
            final BiFunction<? super T, ? super U, ? extends R> function = recover.apply(throwable);
            Objects.requireNonNull(function,
                    () -> "recover returned null for " + throwable.getClass() + ": " + throwable.getMessage());
            return function.apply(t, u);
        }
    };
}

From source file:org.openhab.binding.yamahareceiver.internal.protocol.xml.DeviceDescriptorXML.java

private <T, V> Map<T, V> buildFeatureLookup(Node descNode, String funcValue, Function<String, T> converter,
        BiFunction<T, Element, V> factory) {
    Map<T, V> groupedElements = new HashMap<>();

    if (descNode != null) {
        Stream<Element> elements = getChildElements(descNode)
                .filter(x -> "Menu".equals(x.getTagName()) && funcValue.equals(x.getAttribute("Func")));

        elements.forEach(e -> {/*from w  ww .  ja v a  2  s.co  m*/
            String tag = e.getAttribute("YNC_Tag");

            if (StringUtils.isNotEmpty(tag)) {
                T key = converter.apply(tag);
                if (key != null) {
                    V value = factory.apply(key, e);

                    // a VNC_Tag value might appear more than once (e.g. Zone B has Main_Zone tag)
                    groupedElements.putIfAbsent(key, value);
                }
            }
        });
    }

    return groupedElements;
}

From source file:com.netflix.spinnaker.clouddriver.cloudfoundry.client.ServiceInstances.java

@Nullable
private <T> Resource<T> getServiceInstance(BiFunction<Integer, List<String>, Page<T>> func,
        CloudFoundrySpace space, @Nullable String serviceInstanceName) {
    if (isBlank(serviceInstanceName)) {
        throw new CloudFoundryApiException("Please specify a name for the service being sought");
    }//from  w  w  w  . j  ava 2s . c  om

    List<Resource<T>> serviceInstances = collectPageResources("service instances by space and name",
            pg -> func.apply(pg, getServiceQueryParams(Collections.singletonList(serviceInstanceName), space)));

    if (serviceInstances.isEmpty()) {
        return null;
    }

    if (serviceInstances.size() > 1) {
        throw new CloudFoundryApiException(serviceInstances.size() + " service instances found with name '"
                + serviceInstanceName + "' in space '" + space.getName() + "', but expected only 1");
    }

    return serviceInstances.get(0);
}