java.util.function.ToLongFunction Java Examples

The following examples show how to use java.util.function.ToLongFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: hadoop-ozone   Author: apache   File: ChunkUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static void writeData(ChunkBuffer data, String filename,
    long offset, long len, VolumeIOStats volumeIOStats,
    ToLongFunction<ChunkBuffer> writer) throws StorageContainerException {

  validateBufferSize(len, data.remaining());

  final long startTime = Time.monotonicNow();
  final long bytesWritten;
  try {
    bytesWritten = writer.applyAsLong(data);
  } catch (UncheckedIOException e) {
    throw wrapInStorageContainerException(e.getCause());
  }

  final long endTime = Time.monotonicNow();
  long elapsed = endTime - startTime;
  volumeIOStats.incWriteTime(elapsed);
  volumeIOStats.incWriteOpCount();
  volumeIOStats.incWriteBytes(bytesWritten);

  LOG.debug("Written {} bytes at offset {} to {} in {} ms",
      bytesWritten, offset, filename, elapsed);

  validateWriteSize(len, bytesWritten);
}
 
Example #2
Source Project: morpheus-core   Author: zavtech   File: XDataFrameContent.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Maps the specified column to longs using the mapper function provided
 * @param frame     the frame reference
 * @param colKey    the column key to apply mapper function to
 * @param mapper    the mapper function to apply
 * @return          the newly created content, with update column
 */
@SuppressWarnings("unchecked")
final XDataFrameContent<R,C> mapToLongs(XDataFrame<R,C> frame, C colKey, ToLongFunction<DataFrameValue<R,C>> mapper) {
    if (!isColumnStore()) {
        throw new DataFrameException("Cannot apply columns of a transposed DataFrame");
    } else {
        final int rowCount = rowKeys.size();
        final boolean parallel  = frame.isParallel();
        final int colIndex = colKeys.getIndexForKey(colKey);
        return new XDataFrameContent<>(rowKeys, colKeys, true, Mapper.apply(data, parallel, (index, array) -> {
            if (index != colIndex) {
                return array;
            } else {
                final int colOrdinal = colKeys.getOrdinalForKey(colKey);
                final Array<?> targetValues = Array.of(Long.class, array.length());
                final Cursor cursor = new Cursor(frame, rowKeys.isEmpty() ? -1 : 0, colOrdinal);
                for (int i = 0; i < rowCount; ++i) {
                    cursor.atRowOrdinal(i);
                    final long value = mapper.applyAsLong(cursor);
                    targetValues.setLong(cursor.rowIndex, value);
                }
                return targetValues;
            }
        }));
    }
}
 
Example #3
Source Project: incubator-ratis   Author: apache   File: LeaderState.java    License: Apache License 2.0 6 votes vote down vote up
private long[] getSorted(List<RaftPeerId> followerIDs, boolean includeSelf,
    ToLongFunction<FollowerInfo> getFollowerIndex, LongSupplier getLogIndex) {
  final int length = includeSelf ? followerIDs.size() + 1 : followerIDs.size();
  if (length == 0) {
    throw new IllegalArgumentException("followers.size() == "
        + followerIDs.size() + " and includeSelf == " + includeSelf);
  }

  final long[] indices = new long[length];
  List<FollowerInfo> followerInfos = getFollowerInfos(followerIDs);
  for (int i = 0; i < followerInfos.size(); i++) {
    indices[i] = getFollowerIndex.applyAsLong(followerInfos.get(i));
  }

  if (includeSelf) {
    // note that we also need to wait for the local disk I/O
    indices[length - 1] = getLogIndex.getAsLong();
  }

  Arrays.sort(indices);
  return indices;
}
 
Example #4
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ReferencePipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
    Objects.requireNonNull(mapper);
    return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedReference<P_OUT, Long>(sink) {
                @Override
                public void accept(P_OUT u) {
                    downstream.accept(mapper.applyAsLong(u));
                }
            };
        }
    };
}
 
Example #5
Source Project: jdk8u60   Author: chenghanpeng   File: ReferencePipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
    Objects.requireNonNull(mapper);
    return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedReference<P_OUT, Long>(sink) {
                @Override
                public void accept(P_OUT u) {
                    downstream.accept(mapper.applyAsLong(u));
                }
            };
        }
    };
}
 
Example #6
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: ReferencePipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
    Objects.requireNonNull(mapper);
    return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedReference<P_OUT, Long>(sink) {
                @Override
                public void accept(P_OUT u) {
                    downstream.accept(mapper.applyAsLong(u));
                }
            };
        }
    };
}
 
Example #7
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ReferencePipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
    Objects.requireNonNull(mapper);
    return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedReference<P_OUT, Long>(sink) {
                @Override
                public void accept(P_OUT u) {
                    downstream.accept(mapper.applyAsLong(u));
                }
            };
        }
    };
}
 
Example #8
Source Project: morpheus-core   Author: zavtech   File: XDataFrame.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DataFrame<R, C> mapToLongs(ToLongFunction<DataFrameValue<R, C>> mapper) {
    final Array<R> rowKeys = rows().keyArray();
    final Array<C> colKeys = cols().keyArray();
    final XDataFrame<R,C> result = (XDataFrame<R,C>)DataFrame.ofLongs(rowKeys, colKeys);
    this.forEachValue(v -> {
        final long value = mapper.applyAsLong(v);
        final int rowOrdinal = v.rowOrdinal();
        final int colOrdinal = v.colOrdinal();
        result.content().setLong(rowOrdinal, colOrdinal, value);
    });
    return result;
}
 
Example #9
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: BasicTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testLongComparator() {
    Thing[] things = new Thing[longValues.length];
    for (int i=0; i<longValues.length; i++)
        things[i] = new Thing(0, longValues[i], 0.0, null);
    Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
        @Override
        public long applyAsLong(Thing thing) {
            return thing.getLongField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example #10
Source Project: morpheus-core   Author: zavtech   File: ArrayBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final Array<T> applyLongs(ToLongFunction<ArrayValue<T>> function) {
    final int length = length();
    if (length > 0) {
        final ApplyValues action = new ApplyValues(0, length - 1, function);
        if (isParallel()) {
            ForkJoinPool.commonPool().invoke(action);
        } else {
            action.compute();
        }
    }
    return this;
}
 
Example #11
Source Project: TencentKona-8   Author: Tencent   File: BasicTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testLongComparator() {
    Thing[] things = new Thing[longValues.length];
    for (int i=0; i<longValues.length; i++)
        things[i] = new Thing(0, longValues[i], 0.0, null);
    Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
        @Override
        public long applyAsLong(Thing thing) {
            return thing.getLongField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example #12
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: BasicTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testLongComparator() {
    Thing[] things = new Thing[longValues.length];
    for (int i=0; i<longValues.length; i++)
        things[i] = new Thing(0, longValues[i], 0.0, null);
    Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
        @Override
        public long applyAsLong(Thing thing) {
            return thing.getLongField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example #13
Source Project: jdk8u60   Author: chenghanpeng   File: BasicTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testLongComparator() {
    Thing[] things = new Thing[longValues.length];
    for (int i=0; i<longValues.length; i++)
        things[i] = new Thing(0, longValues[i], 0.0, null);
    Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
        @Override
        public long applyAsLong(Thing thing) {
            return thing.getLongField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example #14
Source Project: morpheus-core   Author: zavtech   File: ArrayBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final Array<Long> mapToLongs(ToLongFunction<ArrayValue<T>> mapper) {
    final Array<Long> result = Array.of(Long.class, length());
    final MapValues<Long> action = new MapValues<>(0, length() - 1, mapper, result);
    if (isParallel()) {
        ForkJoinPool.commonPool().invoke(action);
        return result;
    } else {
        action.compute();
        return result;
    }
}
 
Example #15
Source Project: NNAnalytics   Author: paypal   File: AbstractQueryEngine.java    License: Apache License 2.0 5 votes vote down vote up
private Map<String, Long> genericAvgingHistogram(
    Stream<INode> inodes,
    Function<INode, String> namingFunction,
    ToLongFunction<INode> dataFunction) {
  Map<String, Double> collect =
      inodes.collect(
          Collectors.groupingBy(
              namingFunction,
              Collectors.averagingDouble(n -> (double) dataFunction.applyAsLong(n))));
  HashMap<String, Long> histogram = new HashMap<>(collect.size());
  for (Entry<String, Double> entry : collect.entrySet()) {
    histogram.put(entry.getKey(), entry.getValue().longValue());
  }
  return histogram;
}
 
Example #16
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: BasicTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testLongComparator() {
    Thing[] things = new Thing[longValues.length];
    for (int i=0; i<longValues.length; i++)
        things[i] = new Thing(0, longValues[i], 0.0, null);
    Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
        @Override
        public long applyAsLong(Thing thing) {
            return thing.getLongField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example #17
Source Project: micrometer   Author: micrometer-metrics   File: DropwizardMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
    DropwizardFunctionTimer<T> ft = new DropwizardFunctionTimer<>(id, clock, obj, countFunction, totalTimeFunction,
            totalTimeFunctionUnit, getBaseTimeUnit());
    registry.register(hierarchicalName(id), ft.getDropwizardMeter());
    return ft;
}
 
Example #18
Source Project: micrometer   Author: micrometer-metrics   File: CumulativeFunctionTimer.java    License: Apache License 2.0 5 votes vote down vote up
public CumulativeFunctionTimer(Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction,
                               TimeUnit totalTimeFunctionUnit, TimeUnit baseTimeUnit) {
    this.id = id;
    this.ref = new WeakReference<>(obj);
    this.countFunction = countFunction;
    this.totalTimeFunction = totalTimeFunction;
    this.totalTimeFunctionUnit = totalTimeFunctionUnit;
    this.baseTimeUnit = baseTimeUnit;
}
 
Example #19
Source Project: micrometer   Author: micrometer-metrics   File: StepFunctionTimer.java    License: Apache License 2.0 5 votes vote down vote up
public StepFunctionTimer(Id id, Clock clock, long stepMillis, T obj, ToLongFunction<T> countFunction,
                         ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit, TimeUnit baseTimeUnit) {
    this.id = id;
    this.clock = clock;
    this.ref = new WeakReference<>(obj);
    this.countFunction = countFunction;
    this.totalTimeFunction = totalTimeFunction;
    this.totalTimeFunctionUnit = totalTimeFunctionUnit;
    this.baseTimeUnit = baseTimeUnit;
    this.countTotal = new StepTuple2<>(clock, stepMillis, 0L, 0.0,
            count::sumThenReset, total::sumThenReset);
}
 
Example #20
Source Project: galeb   Author: galeb   File: JmxReporterService.java    License: Apache License 2.0 5 votes vote down vote up
private long extractDelta(final AtomicLong last, final ToLongFunction<Undertow.ListenerInfo> longFunction) {
    long start = System.nanoTime();
    double localLast = last.get() * 1.0;
    double current = undertow.getListenerInfo().stream().mapToLong(longFunction).sum() * 1.0;
    long end = System.nanoTime();
    last.set((long) current);
    return Math.round((current * ((double) end / (double) start)) - localLast);
}
 
Example #21
Source Project: micrometer   Author: micrometer-metrics   File: FunctionTimer.java    License: Apache License 2.0 5 votes vote down vote up
private Builder(String name, @Nullable T obj,
                ToLongFunction<T> countFunction,
                ToDoubleFunction<T> totalTimeFunction,
                TimeUnit totalTimeFunctionUnit) {
    this.name = name;
    this.obj = obj;
    this.countFunction = countFunction;
    this.totalTimeFunction = totalTimeFunction;
    this.totalTimeFunctionUnit = totalTimeFunctionUnit;
}
 
Example #22
Source Project: rheem   Author: rheem-ecosystem   File: DefaultCardinalityEstimator.java    License: Apache License 2.0 5 votes vote down vote up
public DefaultCardinalityEstimator(double certaintyProb,
                                   int numInputs,
                                   boolean isAllowMoreInputs,
                                   ToLongFunction<long[]> singlePointEstimator) {
    this(certaintyProb,
            numInputs,
            isAllowMoreInputs,
            (inputCards, rheemContext) -> singlePointEstimator.applyAsLong(inputCards));
}
 
Example #23
Source Project: morpheus-core   Author: zavtech   File: XDataFrameVector.java    License: Apache License 2.0 5 votes vote down vote up
@Override()
public final Z applyLongs(ToLongFunction<DataFrameValue<R,C>> mapper) {
    return forEachValue(value -> {
        final long result = mapper.applyAsLong(value);
        value.setLong(result);
    });
}
 
Example #24
Source Project: micrometer   Author: micrometer-metrics   File: SimpleMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
    switch (config.mode()) {
        case CUMULATIVE:
            return new CumulativeFunctionTimer<>(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnit, getBaseTimeUnit());

        case STEP:
        default:
            return new StepFunctionTimer<>(id, clock, config.step().toMillis(), obj, countFunction, totalTimeFunction, totalTimeFunctionUnit, getBaseTimeUnit());
    }
}
 
Example #25
Source Project: presto   Author: prestosql   File: StreamDataOutput.java    License: Apache License 2.0 4 votes vote down vote up
public StreamDataOutput(ToLongFunction<SliceOutput> writer, Stream stream)
{
    this.writer = requireNonNull(writer, "writer is null");
    this.stream = requireNonNull(stream, "stream is null");
}
 
Example #26
Source Project: chvote-protocol-poc   Author: republique-et-canton-de-geneve   File: Simulation.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
private LongSummaryStatistics computeStats(List<DefaultVotingClient> votingClients, ToLongFunction<DefaultVotingClient.Stats> getVoteEncodingTime) {
    return votingClients.stream().map(DefaultVotingClient::getStats).mapToLong(getVoteEncodingTime).summaryStatistics();
}
 
Example #27
Source Project: micrometer   Author: micrometer-metrics   File: WavefrontMeterRegistry.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
    return new CumulativeFunctionTimer<>(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnit, getBaseTimeUnit());
}
 
Example #28
Source Project: qpid-broker-j   Author: apache   File: BrokerAttributeInjector.java    License: Apache License 2.0 4 votes vote down vote up
private static long getLongValue(Broker broker, ToLongFunction<Broker> supplier)
{
    return supplier.applyAsLong(broker);
}
 
Example #29
Source Project: NNAnalytics   Author: paypal   File: QueryEngine.java    License: Apache License 2.0 4 votes vote down vote up
Map<String, Long> genericSumOrFindHistogram(
Stream<INode> inodes,
Function<INode, String> namingFunction,
ToLongFunction<INode> dataFunction,
String find);
 
Example #30
Source Project: NNAnalytics   Author: paypal   File: Helper.java    License: Apache License 2.0 4 votes vote down vote up
public static <T> ToLongFunction<T> convertToLongFunction(Function<T, Long> function) {
  return function::apply;
}