本文整理匯總了Java中java.util.function.ToIntFunction.applyAsInt方法的典型用法代碼示例。如果您正苦於以下問題:Java ToIntFunction.applyAsInt方法的具體用法?Java ToIntFunction.applyAsInt怎麽用?Java ToIntFunction.applyAsInt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.function.ToIntFunction
的用法示例。
在下文中一共展示了ToIntFunction.applyAsInt方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSortedPotential
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* 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);
}
示例2: getOrdinal
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
private int getOrdinal(ResourcePoolEntry resource) {
String path = resource.path();
Integer value = orderedPaths.get(stripModule(path));
if (value != null) {
return value;
}
for (ToIntFunction<String> function : filters) {
int ordinal = function.applyAsInt(path);
if (ordinal != Integer.MAX_VALUE) {
return ordinal;
}
}
return Integer.MAX_VALUE;
}
示例3: testProcessor
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
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();
}
}
}
示例4: testProcessor
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
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();
}
}
}
示例5: goodsToMake
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* Chooses a type of goods for some of the natives in a settlement
* to manufacture.
* Simple rule: choose the refined goods that is the greatest shortage
* for which there is a surplus of the raw material.
*
* @return A {@code GoodsType} to manufacture, or null if
* none suitable.
*/
private GoodsType goodsToMake() {
final ToIntFunction<GoodsType> deficit = cacheInt(gt ->
getWantedGoodsAmount(gt) - getGoodsCount(gt));
final Predicate<GoodsType> goodsPred = gt ->
gt.isRawMaterial()
&& gt.getOutputType() != null
&& !gt.getOutputType().isBreedable()
&& gt.getOutputType().isStorable()
&& deficit.applyAsInt(gt) < 0
&& deficit.applyAsInt(gt.getOutputType()) > 0;
final Comparator<GoodsType> comp = Comparator.comparingInt(deficit);
return maximize(getSpecification().getGoodsTypeList(), goodsPred, comp);
}
示例6: toInt
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* Creates an INTEGER mapper that wraps to function provided
* @param function the function to wrap
* @param <I> the input type
* @return the newly created mapper
*/
public static <I,O> Function1<I,Integer> toInt(ToIntFunction<I> function) {
return new Function1<I,Integer>(FunctionStyle.INTEGER) {
@Override
public final int applyAsInt(I value) {
return function.applyAsInt(value);
}
};
}
示例7: getFullness
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* Get the "fullness" of this team, relative to some capacity returned by
* the given function. The return value is always in the range 0 to 1.
*/
public Fraction getFullness(ToIntFunction<? super Team> maxFunction) {
final int max = maxFunction.applyAsInt(this);
return max == 0 ? Fraction.ONE
: Fraction.getReducedFraction(getSize(), max);
}
示例8: summingInt
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* 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);
}
示例9: averagingInt
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* 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);
}
示例10: averagingInt
import java.util.function.ToIntFunction; //導入方法依賴的package包/類
/**
* 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);
}