下面列出了怎么用java.util.function.DoubleSupplier的API类实例代码及写法,或者点击链接到github查看源代码。
public synchronized TaskHandle addTask(
TaskId taskId,
DoubleSupplier utilizationSupplier,
int initialSplitConcurrency,
Duration splitConcurrencyAdjustFrequency,
OptionalInt maxDriversPerTask)
{
requireNonNull(taskId, "taskId is null");
requireNonNull(utilizationSupplier, "utilizationSupplier is null");
checkArgument(maxDriversPerTask.isEmpty() || maxDriversPerTask.getAsInt() <= maximumNumberOfDriversPerTask,
"maxDriversPerTask cannot be greater than the configured value");
log.debug("Task scheduled " + taskId);
TaskHandle taskHandle = new TaskHandle(taskId, waitingSplits, utilizationSupplier, initialSplitConcurrency, splitConcurrencyAdjustFrequency, maxDriversPerTask);
tasks.add(taskHandle);
return taskHandle;
}
public TaskHandle(
TaskId taskId,
MultilevelSplitQueue splitQueue,
DoubleSupplier utilizationSupplier,
int initialSplitConcurrency,
Duration splitConcurrencyAdjustFrequency,
OptionalInt maxDriversPerTask)
{
this.taskId = requireNonNull(taskId, "taskId is null");
this.splitQueue = requireNonNull(splitQueue, "splitQueue is null");
this.utilizationSupplier = requireNonNull(utilizationSupplier, "utilizationSupplier is null");
this.maxDriversPerTask = requireNonNull(maxDriversPerTask, "maxDriversPerTask is null");
this.concurrencyController = new SplitConcurrencyController(
initialSplitConcurrency,
requireNonNull(splitConcurrencyAdjustFrequency, "splitConcurrencyAdjustFrequency is null"));
}
/**
* Creates a new view configuration
*
* @param viewportSupplier A supplier that supplies the viewport,
* as 4 float elements, [x, y, width, height]
* @param aspectRatioSupplier An optional supplier for the aspect ratio.
* If this is <code>null</code>, then the aspect ratio of the
* camera will be used
* @param externalViewMatrixSupplier The optional external supplier of
* a view matrix.
* @param externalProjectionMatrixSupplier The optional external supplier
* of a projection matrix.
*/
ViewConfiguration(
Supplier<float[]> viewportSupplier,
DoubleSupplier aspectRatioSupplier,
Supplier<float[]> externalViewMatrixSupplier,
Supplier<float[]> externalProjectionMatrixSupplier)
{
this.viewportSupplier = Objects.requireNonNull(
viewportSupplier, "The viewportSupplier may not be null");
this.currentCameraModelSupplier = new SettableSupplier<CameraModel>();
this.aspectRatioSupplier = aspectRatioSupplier;
this.viewMatrixSupplier =
createViewMatrixSupplier(externalViewMatrixSupplier);
this.projectionMatrixSupplier =
createProjectionMatrixSupplier(externalProjectionMatrixSupplier);
}
public Rotate(
final DoubleSupplier speed,
final AffineTransform3D globalTransform,
final AffineTransform3D displayTransform,
final AffineTransform3D globalToViewerTransform,
final Consumer<AffineTransform3D> submitTransform,
final Object lock)
{
super();
this.speed = speed;
this.globalTransform = globalTransform;
this.displayTransform = displayTransform;
this.globalToViewerTransform = globalToViewerTransform;
this.submitTransform = submitTransform;
this.lock = lock;
}
public static EventHandler<KeyEvent> keyPressedHandler(
final PainteraGateway gateway,
final Node target,
Consumer<Exception> exceptionHandler,
Predicate<KeyEvent> check,
final String menuText,
final PainteraBaseView viewer,
final Supplier<String> projectDirectory,
final DoubleSupplier x,
final DoubleSupplier y)
{
return event -> {
if (check.test(event))
{
event.consume();
OpenDialogMenu m = gateway.openDialogMenu();
Optional<ContextMenu> cm = m.getContextMenu(menuText, viewer, projectDirectory, exceptionHandler);
Bounds bounds = target.localToScreen(target.getBoundsInLocal());
cm.ifPresent(menu -> menu.show(target, x.getAsDouble() + bounds.getMinX(), y.getAsDouble() + bounds.getMinY()));
}
};
}
/** Set up the instance for the benchmark.
*/
@Setup(Level.Iteration)
public void setup() {
vectors = new ArrayList<>(size);
final double[] values = new double[dimension];
final DoubleSupplier doubleSupplier = createDoubleSupplier(getType(),
RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP));
for (int i = 0; i < size; ++i) {
for (int j = 0; j < dimension; ++j) {
values[j] = doubleSupplier.getAsDouble();
}
vectors.add(vectorFactory.apply(values));
}
}
/** Create a supplier that produces doubles of the given type.
* @param type type of doubles to produce
* @param rng random provider
* @return a supplier that produces doubles of the given type
*/
private DoubleSupplier createDoubleSupplier(final String type, final UniformRandomProvider rng) {
switch (type) {
case RANDOM:
return () -> createRandomDouble(rng);
case NORMALIZABLE:
final ZigguratNormalizedGaussianSampler sampler = ZigguratNormalizedGaussianSampler.of(rng);
return () -> {
double n = sampler.sample();
return n == 0 ? 0.1 : n; // do not return exactly zero
};
case EDGE:
return () -> EDGE_NUMBERS[rng.nextInt(EDGE_NUMBERS.length)];
default:
throw new IllegalStateException("Invalid input type: " + type);
}
}
@Override
protected double[] createNumbers(SplittableRandom rng) {
DoubleSupplier generator;
if ("pi".equals(type)) {
generator = () -> rng.nextDouble() * 2 * Math.PI - Math.PI;
} else if ("pi/2".equals(type)) {
generator = () -> rng.nextDouble() * Math.PI - Math.PI / 2;
} else if ("random".equals(type)) {
generator = () -> createRandomNumber(rng);
} else if ("edge".equals(type)) {
generator = () -> createEdgeNumber(rng);
} else {
throw new IllegalStateException("Unknown number type: " + type);
}
return DoubleStream.generate(generator).limit(getSize()).toArray();
}
@Override
public void createGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {
final String metricName = convertToPrometheusName(category, name);
if (isCategoryEnabled(category)) {
final Collector collector = new CurrentValueCollector(metricName, help, valueSupplier);
addCollectorUnchecked(category, collector);
}
}
public double getGaugeValue(final String name) {
final DoubleSupplier gauge = gauges.get(name);
if (gauge == null) {
throw new IllegalArgumentException("Unknown gauge: " + name);
}
return gauge.getAsDouble();
}
public ModularUI.Builder createUITemplate(DoubleSupplier progressSupplier, IItemHandlerModifiable importItems, IItemHandlerModifiable exportItems, FluidTankList importFluids, FluidTankList exportFluids) {
ModularUI.Builder builder = ModularUI.defaultBuilder();
builder.widget(new ProgressWidget(progressSupplier, 77, 22, 20, 20, progressBarTexture, moveType));
addInventorySlotGroup(builder, importItems, importFluids, false);
addInventorySlotGroup(builder, exportItems, exportFluids, true);
return builder;
}
@Override
public Builder createUITemplate(DoubleSupplier progressSupplier, IItemHandlerModifiable importItems, IItemHandlerModifiable exportItems, FluidTankList importFluids, FluidTankList exportFluids) {
ModularUI.Builder builder = ModularUI.defaultBuilder();
builder.widget(new ProgressWidget(progressSupplier, 77, 22, 20, 20, progressBarTexture, moveType));
addInventorySlotGroup(builder, importItems, importFluids, false);
BooleanWrapper booleanWrapper = new BooleanWrapper();
ServerWidgetGroup itemOutputGroup = createItemOutputWidgetGroup(exportItems, new ServerWidgetGroup(() -> !booleanWrapper.getCurrentMode()));
ServerWidgetGroup fluidOutputGroup = createFluidOutputWidgetGroup(exportFluids, new ServerWidgetGroup(booleanWrapper::getCurrentMode));
builder.widget(itemOutputGroup).widget(fluidOutputGroup);
ToggleButtonWidget buttonWidget = new ToggleButtonWidget(176 - 7 - 20, 60, 20, 20,
GuiTextures.BUTTON_SWITCH_VIEW, booleanWrapper::getCurrentMode, booleanWrapper::setCurrentMode)
.setTooltipText("gregtech.gui.toggle_view");
builder.widget(buttonWidget);
return builder;
}
public ProgressWidget(DoubleSupplier progressSupplier, int x, int y, int width, int height, TextureArea fullImage, MoveType moveType) {
super(new Position(x, y), new Size(width, height));
this.progressSupplier = progressSupplier;
this.emptyBarArea = fullImage.getSubArea(0.0, 0.0, 1.0, 0.5);
this.filledBarArea = fullImage.getSubArea(0.0, 0.5, 1.0, 0.5);
this.moveType = moveType;
}
@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
DoubleSupplier supplier = () -> 0.2 + (unlockProgress / (MAX_UNLOCK_PROGRESS * 1.0)) * 0.8;
ModularUI.Builder builder = ModularUI.defaultBuilder()
.widget(new ProgressWidget(supplier, 5, 5, 166, 74,
GuiTextures.PROGRESS_BAR_UNLOCK,
MoveType.VERTICAL_INVERTED))
.bindPlayerInventory(entityPlayer.inventory);
ServerWidgetGroup lockedGroup = new ServerWidgetGroup(() -> !isSafeUnlocked && unlockProgress < 0);
lockedGroup.addWidget(new LabelWidget(5, 20, "gregtech.machine.locked_safe.malfunctioning"));
lockedGroup.addWidget(new LabelWidget(5, 30, "gregtech.machine.locked_safe.requirements"));
lockedGroup.addWidget(new SlotWidget(unlockInventory, 0, 70, 40, false, true).setBackgroundTexture(GuiTextures.SLOT));
lockedGroup.addWidget(new SlotWidget(unlockInventory, 1, 70 + 18, 40, false, true).setBackgroundTexture(GuiTextures.SLOT));
lockedGroup.addWidget(new SlotWidget(unlockComponents, 0, 70, 58, false, false));
lockedGroup.addWidget(new SlotWidget(unlockComponents, 1, 70 + 18, 58, false, false));
ServerWidgetGroup unlockedGroup = new ServerWidgetGroup(() -> isSafeUnlocked);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
unlockedGroup.addWidget(new SlotWidget(safeLootInventory, col + row * 9,
8 + col * 18, 15 + row * 18, true, false));
}
}
return builder.widget(unlockedGroup)
.widget(lockedGroup)
.build(getHolder(), entityPlayer);
}
public TranslateAlongNormal(
final DoubleSupplier translationSpeed,
final GlobalTransformManager manager,
final AffineTransform3D worldToSharedViewerSpace,
final Object lock)
{
this.translationSpeed = translationSpeed;
this.manager = manager;
this.worldToSharedViewerSpace = worldToSharedViewerSpace;
this.lock = lock;
manager.addListener(global::set);
}
public Zoom(
final DoubleSupplier speed,
final GlobalTransformManager manager,
final AffineTransform3D concatenated,
final Object lock)
{
this.speed = speed;
this.manager = manager;
this.concatenated = concatenated;
this.lock = lock;
this.manager.addListener(global::set);
}
@Override
public void createGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {
final StubGauge guage = new StubGauge(category, name, help, valueSupplier);
final Map<String, StubGauge> gaugesInCategory =
gauges.computeIfAbsent(category, key -> new ConcurrentHashMap<>());
if (gaugesInCategory.putIfAbsent(name, guage) != null) {
throw new IllegalArgumentException("Attempting to create two gauges with the same name");
}
}
protected StubGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier supplier) {
super(category, name, help);
this.supplier = supplier;
}
@Test
public void testToTree_randomSpheres() {
// arrange
UniformRandomProvider rand = RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP, 1L);
DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-10);
double min = 1e-1;
double max = 1e2;
DoubleSupplier randDouble = () -> (rand.nextDouble() * (max - min)) + min;
int count = 10;
for (int i = 0; i < count; ++i) {
Vector3D center = Vector3D.of(
randDouble.getAsDouble(),
randDouble.getAsDouble(),
randDouble.getAsDouble());
double radius = randDouble.getAsDouble();
Sphere sphere = Sphere.from(center, radius, precision);
for (int s = 0; s < 7; ++s) {
// act
RegionBSPTree3D tree = sphere.toTree(s);
// assert
Assert.assertEquals((int)(8 * Math.pow(4, s)), tree.getBoundaries().size());
Assert.assertTrue(tree.isFinite());
Assert.assertFalse(tree.isEmpty());
Assert.assertTrue(tree.getSize() < sphere.getSize());
}
}
}
private static Double average(Table table, CalculateEntry calculateEntry) throws Exception {
DoubleSupplier ds = () -> {
return 0d;
};
Double result = table.stream().mapToDouble(row -> row.getAsDouble(calculateEntry.getColumn())).average()
.orElseGet(ds);
return result;
}
protected Double average(Table table) throws Exception {
DoubleSupplier ds = () -> {
return 0d;
};
Double result = table.stream().mapToDouble(row -> row.getAsDouble(this.column)).average().orElseGet(ds);
return result;
}
/**
* Function that makes sure functional counter values survive pg_stat_reset calls.
*/
Double resettableFunctionalCounter(String functionalCounterKey, DoubleSupplier function) {
Double result = function.getAsDouble();
Double previousResult = previousValueCacheMap.getOrDefault(functionalCounterKey, 0D);
Double beforeResetValue = beforeResetValuesCacheMap.getOrDefault(functionalCounterKey, 0D);
Double correctedValue = result + beforeResetValue;
if (correctedValue < previousResult) {
beforeResetValuesCacheMap.put(functionalCounterKey, previousResult);
correctedValue = previousResult + result;
}
previousValueCacheMap.put(functionalCounterKey, correctedValue);
return correctedValue;
}
public CurrentValueCollector(
final String metricName, final String help, final DoubleSupplier valueSupplier) {
this.metricName = metricName;
this.help = help;
this.valueSupplier = valueSupplier;
}
@Override
public void createGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {}
OfDouble(long size, DoubleSupplier s) {
super(size);
this.s = s;
}
OfDouble(long size, DoubleSupplier s) {
super(size);
this.s = s;
}
OfDouble(long size, DoubleSupplier s) {
super(size);
this.s = s;
}
OfDouble(long size, DoubleSupplier s) {
super(size);
this.s = s;
}
public Builder progressBar(DoubleSupplier progressSupplier, int x, int y, int width, int height, TextureArea texture, MoveType moveType) {
return widget(new ProgressWidget(progressSupplier, x, y, width, height, texture, moveType));
}
OfDouble(long size, DoubleSupplier s) {
super(size);
this.s = s;
}