java.util.function.ToIntFunction#applyAsInt ( )源码实例Demo

下面列出了java.util.function.ToIntFunction#applyAsInt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: presto   文件: NodePartitioningManager.java
private ToIntFunction<Split> getSplitToBucket(Session session, PartitioningHandle partitioningHandle)
{
    ConnectorNodePartitioningProvider partitioningProvider = partitioningProviders.get(partitioningHandle.getConnectorId().get());
    checkArgument(partitioningProvider != null, "No partitioning provider for connector %s", partitioningHandle.getConnectorId().get());

    ToIntFunction<ConnectorSplit> splitBucketFunction = partitioningProvider.getSplitBucketFunction(
            partitioningHandle.getTransactionHandle().orElse(null),
            session.toConnectorSession(),
            partitioningHandle.getConnectorHandle());
    checkArgument(splitBucketFunction != null, "No partitioning %s", partitioningHandle);

    return split -> {
        int bucket;
        if (split.getConnectorSplit() instanceof EmptySplit) {
            bucket = split.getLifespan().isTaskWide() ? 0 : split.getLifespan().getId();
        }
        else {
            bucket = splitBucketFunction.applyAsInt(split.getConnectorSplit());
        }
        if (!split.getLifespan().isTaskWide()) {
            checkArgument(split.getLifespan().getId() == bucket);
        }
        return bucket;
    };
}
 
源代码2 项目: freecol   文件: Tile.java
/**
 * Sort possible goods types according to potential.
 *
 * @param unitType The {@code UnitType} to do the work.
 * @param owner the {@code Player} owning the unit.
 * @return A list of goods, highest potential production first.
 */
public List<AbstractGoods> getSortedPotential(UnitType unitType,
                                              Player owner) {
    // Defend against calls while partially read.
    if (getType() == null) return Collections.<AbstractGoods>emptyList();
    
    final ToIntFunction<GoodsType> productionMapper = cacheInt(gt ->
        getPotentialProduction(gt, unitType));
    final Predicate<GoodsType> productionPred = gt ->
        productionMapper.applyAsInt(gt) > 0;
    final Function<GoodsType, AbstractGoods> goodsMapper = gt ->
        new AbstractGoods(gt, productionMapper.applyAsInt(gt));
    final Comparator<AbstractGoods> goodsComp
        = ((owner == null || owner.getMarket() == null)
            ? AbstractGoods.descendingAmountComparator
            : owner.getMarket().getSalePriceComparator());
    // It is necessary to consider all farmed goods, since the
    // tile might have a resource that produces goods not produced
    // by the tile type.
    return transform(getSpecification().getFarmedGoodsTypeList(),
                     productionPred, goodsMapper, goodsComp);
}
 
private static int testDriftServer(ServerMethodInvoker methodInvoker, List<ToIntFunction<HostAndPort>> clients)
{
    DriftNettyServerConfig config = new DriftNettyServerConfig()
            .setSslEnabled(true)
            .setTrustCertificate(ClientTestUtils.getCertificateChainFile())
            .setKey(ClientTestUtils.getPrivateKeyFile());
    TestingPooledByteBufAllocator testingAllocator = new TestingPooledByteBufAllocator();
    ServerTransport serverTransport = new DriftNettyServerTransportFactory(config, testingAllocator).createServerTransport(methodInvoker);
    try {
        serverTransport.start();

        HostAndPort address = HostAndPort.fromParts("localhost", ((DriftNettyServerTransport) serverTransport).getPort());

        int sum = 0;
        for (ToIntFunction<HostAndPort> client : clients) {
            sum += client.applyAsInt(address);
        }
        return sum;
    }
    finally {
        serverTransport.shutdown();
        testingAllocator.close();
    }
}
 
源代码4 项目: drift   文件: TestDriftNettyMethodInvoker.java
private static int testMethodInvoker(ServerMethodInvoker methodInvoker, List<ToIntFunction<HostAndPort>> clients)
{
    TestingPooledByteBufAllocator testingAllocator = new TestingPooledByteBufAllocator();
    ServerTransport serverTransport = new DriftNettyServerTransportFactory(new DriftNettyServerConfig(), testingAllocator).createServerTransport(methodInvoker);
    try {
        serverTransport.start();

        HostAndPort address = HostAndPort.fromParts("localhost", ((DriftNettyServerTransport) serverTransport).getPort());

        int sum = 0;
        for (ToIntFunction<HostAndPort> client : clients) {
            sum += client.applyAsInt(address);
        }
        return sum;
    }
    finally {
        serverTransport.shutdown();
        testingAllocator.close();
    }
}
 
源代码5 项目: customstuff4   文件: CollectionHelper.java
/**
 * Gets a random element from the given collection where each element has a weight provided by weightFunction.
 * A higher weight makes an element more likely to be selected.
 *
 * @return A random element or null if the collection was empty.
 */
@Nonnull
public static <T> Optional<T> randomElement(Collection<T> collection, ToIntFunction<T> weightFunction)
{
    int totalWeight = collection.stream().mapToInt(weightFunction).sum();
    int randomWeight = RandomUtils.nextInt(1, totalWeight + 1);

    for (T t : collection)
    {
        randomWeight -= weightFunction.applyAsInt(t);
        if (randomWeight <= 0)
            return Optional.of(t);
    }

    return Optional.empty();
}
 
源代码6 项目: drift   文件: TestDriftNettyServerTransport.java
private static int testServerMethodInvoker(ServerMethodInvoker methodInvoker, boolean assumeClientsSupportOutOfOrderResponses, List<ToIntFunction<HostAndPort>> clients)
{
    DriftNettyServerConfig config = new DriftNettyServerConfig()
            .setAssumeClientsSupportOutOfOrderResponses(assumeClientsSupportOutOfOrderResponses);
    TestingPooledByteBufAllocator testingAllocator = new TestingPooledByteBufAllocator();
    ServerTransport serverTransport = new DriftNettyServerTransportFactory(config, testingAllocator).createServerTransport(methodInvoker);
    try {
        serverTransport.start();

        HostAndPort address = HostAndPort.fromParts("localhost", ((DriftNettyServerTransport) serverTransport).getPort());

        int sum = 0;
        for (ToIntFunction<HostAndPort> client : clients) {
            sum += client.applyAsInt(address);
        }
        return sum;
    }
    finally {
        serverTransport.shutdown();
        testingAllocator.close();
    }
}
 
源代码7 项目: drift   文件: TestApacheThriftMethodInvoker.java
private static int testProcessor(TProcessor processor, List<ToIntFunction<HostAndPort>> clients)
        throws Exception
{
    try (TServerSocket serverTransport = new TServerSocket(0)) {
        TProtocolFactory protocolFactory = new Factory();
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        TServer server = new TSimpleServer(new Args(serverTransport)
                .protocolFactory(protocolFactory)
                .transportFactory(transportFactory)
                .processor(processor));

        Thread serverThread = new Thread(server::serve);
        try {
            serverThread.start();

            int localPort = serverTransport.getServerSocket().getLocalPort();
            HostAndPort address = HostAndPort.fromParts("localhost", localPort);

            int sum = 0;
            for (ToIntFunction<HostAndPort> client : clients) {
                sum += client.applyAsInt(address);
            }
            return sum;
        }
        finally {
            server.stop();
            serverThread.interrupt();
        }
    }
}
 
源代码8 项目: EnchantmentCracker   文件: Enchantments.java
private static <T> T weightedRandom(Random rand, List<T> list, ToIntFunction<T> weightExtractor) {
	int weight = list.stream().mapToInt(weightExtractor).sum();
	if (weight <= 0) {
		return null;
	}
	weight = rand.nextInt(weight);
	for (T t : list) {
		weight -= weightExtractor.applyAsInt(t);
		if (weight < 0) {
			return t;
		}
	}
	return null;
}
 
源代码9 项目: drift   文件: TestDriftNettyMethodInvoker.java
private static int testProcessor(TProcessor processor, List<ToIntFunction<HostAndPort>> clients)
        throws Exception
{
    try (TServerSocket serverTransport = new TServerSocket(0)) {
        TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        TServer server = new TSimpleServer(new Args(serverTransport)
                .protocolFactory(protocolFactory)
                .transportFactory(transportFactory)
                .processor(processor));

        Thread serverThread = new Thread(server::serve);
        try {
            serverThread.start();

            int localPort = serverTransport.getServerSocket().getLocalPort();
            HostAndPort address = HostAndPort.fromParts("localhost", localPort);

            int sum = 0;
            for (ToIntFunction<HostAndPort> client : clients) {
                sum += client.applyAsInt(address);
            }
            return sum;
        }
        finally {
            server.stop();
            serverThread.interrupt();
        }
    }
}
 
源代码10 项目: openjdk-jdk8u-backup   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Double>
averagingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new long[2],
            (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; },
            (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
            a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
}
 
源代码11 项目: jdk1.8-source-analysis   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码12 项目: jdk1.8-source-analysis   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Double>
averagingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new long[2],
            (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; },
            (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
            a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
}
 
源代码13 项目: openjdk-jdk8u-backup   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码14 项目: Bytecoder   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码15 项目: TencentKona-8   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码16 项目: jdk8u60   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码17 项目: jdk8u60   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Double>
averagingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new long[2],
            (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; },
            (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
            a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
}
 
源代码18 项目: JDKSourceCode1.8   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
源代码19 项目: Bytecoder   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be averaged
 * @return a {@code Collector} that produces the arithmetic mean of a
 * derived property
 */
public static <T> Collector<T, ?, Double>
averagingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new long[2],
            (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; },
            (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
            a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
}
 
源代码20 项目: openjdk-jdk8u   文件: Collectors.java
/**
 * Returns a {@code Collector} that produces the sum of a integer-valued
 * function applied to the input elements.  If no elements are present,
 * the result is 0.
 *
 * @param <T> the type of the input elements
 * @param mapper a function extracting the property to be summed
 * @return a {@code Collector} that produces the sum of a derived property
 */
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
    return new CollectorImpl<>(
            () -> new int[1],
            (a, t) -> { a[0] += mapper.applyAsInt(t); },
            (a, b) -> { a[0] += b[0]; return a; },
            a -> a[0], CH_NOID);
}
 
 同类方法