Java Code Examples for java.util.function.LongFunction#apply()

The following examples show how to use java.util.function.LongFunction#apply() . 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 File: BytesSSZReader.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
private <T> List<T> readList(LongFunction<Bytes> bytesSupplier, Function<Bytes, T> converter) {
  ensureBytes(4, () -> "SSZ encoded data is not a list");
  int originalIndex = this.index;
  List<T> elements;
  try {
    // use a long to simulate reading unsigned
    long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
    elements = new ArrayList<>();
    while (listSize > 0) {
      Bytes bytes = bytesSupplier.apply(listSize);
      elements.add(converter.apply(bytes));
      listSize -= bytes.size();
      listSize -= 4;
      if (listSize < 0) {
        throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
      }
    }
  } catch (Exception e) {
    this.index = originalIndex;
    throw e;
  }
  return elements;
}
 
Example 2
Source File: LongPipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 3
Source File: SlidingWindow.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * A new request arrives, create it with {@link #nextSeqNum}
 * and then try sending it to the server.
 *
 * @param requestConstructor use seqNum to create a new request.
 * @return the new request.
 */
public synchronized REQUEST submitNewRequest(
    LongFunction<REQUEST> requestConstructor, Consumer<REQUEST> sendMethod) {
  if (!requests.isEmpty()) {
    Preconditions.assertTrue(nextSeqNum == requests.lastSeqNum() + 1,
        () -> "nextSeqNum=" + nextSeqNum + " but " + this);
  }

  final long seqNum = nextSeqNum++;
  final REQUEST r = requestConstructor.apply(seqNum);

  if (exception != null) {
    alreadyClosed(r, exception);
    return r;
  }

  requests.putNewRequest(r);

  final boolean submitted = sendOrDelayRequest(r, sendMethod);
  LOG.debug("{}: submitting a new request {} in {}? {}",
      requests.getName(), r, this, submitted? "submitted": "delayed");
  return r;
}
 
Example 4
Source File: LongPipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 5
Source File: LongPipeline.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 6
Source File: BytesSSZReader.java    From cava with Apache License 2.0 6 votes vote down vote up
private <T> List<T> readList(LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
  ensureBytes(4, () -> "SSZ encoded data is not a list");
  int originalIndex = this.index;
  List<T> elements;
  try {
    // use a long to simulate reading unsigned
    long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
    elements = new ArrayList<>();
    while (listSize > 0) {
      byte[] bytes = bytesSupplier.apply(listSize);
      elements.add(converter.apply(bytes));
      listSize -= bytes.length;
      listSize -= 4;
      if (listSize < 0) {
        throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
      }
    }
  } catch (Exception e) {
    this.index = originalIndex;
    throw e;
  }
  return elements;
}
 
Example 7
Source File: LongPipeline.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 8
Source File: OrcBatchReader.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <T> void readNonNullLongColumn(Object[] vals, int fieldIdx, LongColumnVector vector,
												int childCount, LongFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		T repeatingValue = reader.apply(vector.vector[0]);
		fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount);
	} else {
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				vals[i] = reader.apply(vector.vector[i]);
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
			}
		}
	}
}
 
Example 9
Source File: LongPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 10
Source File: LongPipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 11
Source File: LongPipeline.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example 12
Source File: BytesSSZReader.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
private <T> List<T> readList(long listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
  int originalIndex = this.index;
  List<T> elements;
  try {
    elements = new ArrayList<>();
    while (listSize > 0) {
      byte[] bytes = bytesSupplier.apply(listSize);
      elements.add(converter.apply(bytes));
      // When lists have lengths passed in, the listSize argument is the number of
      // elements in the list, instead of the number of bytes in the list, so
      // we only subtract one each time an element is processed in this case.
      listSize -= 1;
      if (listSize < 0) {
        throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
      }
    }
  } catch (Exception e) {
    this.index = originalIndex;
    throw e;
  }
  return elements;
}
 
Example 13
Source File: BytesSSZReader.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
private List<Bytes> readList(LongFunction<Bytes> bytesSupplier) {
  ensureBytes(4, () -> "SSZ encoded data is not a list");
  int originalIndex = this.index;
  List<Bytes> elements;
  try {
    // use a long to simulate reading unsigned
    long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
    elements = new ArrayList<>();
    while (listSize > 0) {
      Bytes bytes = bytesSupplier.apply(listSize);
      elements.add(bytes);
      listSize -= bytes.size();
      listSize -= 4;
      if (listSize < 0) {
        throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
      }
    }
  } catch (Exception e) {
    this.index = originalIndex;
    throw e;
  }
  return elements;
}
 
Example 14
Source File: OrcBatchReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <T> void readLongColumn(Object[] vals, int fieldIdx, LongColumnVector vector,
										int childCount, LongFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		if (vector.isNull[0]) {
			// fill vals with null values
			fillColumnWithRepeatingValue(vals, fieldIdx, null, childCount);
		} else {
			// read repeating non-null value by forwarding call.
			readNonNullLongColumn(vals, fieldIdx, vector, childCount, reader);
		}
	} else {
		boolean[] isNullVector = vector.isNull;
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					vals[i] = null;
				} else {
					vals[i] = reader.apply(vector.vector[i]);
				}
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					rows[i].setField(fieldIdx, null);
				} else {
					rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
				}
			}
		}
	}
}
 
Example 15
Source File: Function2.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an LONG function that wraps to function provided
 * @param function  the function to wrap
 * @param <O>       the output type
 * @return          the newly created function wrapper
 */
public static <O> Function2<Long,O> fromLong(LongFunction<O> function) {
    return new Function2<Long,O>(FunctionStyle.LONG) {
        @Override
        public final O apply(long input) {
            return function.apply(input);
        }
    };
}
 
Example 16
Source File: Printer.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an LONG Printer that wraps the function provided
 * @param function  the function to wrap
 * @return          the newly created function Printer
 */
public static Printer<Long> forLong(LongFunction<String> function) {
    return new Printer<Long>(FunctionStyle.LONG, DEFAULT_NULL) {
        @Override
        public final String apply(long input) {
            return function.apply(input);
        }
    };
}
 
Example 17
Source File: LongPipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                LongConsumer downstreamAsLong = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsLong);
                            }
                            else {
                                Spliterator.OfLong s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsLong));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 18
Source File: LongPipeline.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                LongConsumer downstreamAsLong = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsLong);
                            }
                            else {
                                Spliterator.OfLong s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsLong));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 19
Source File: LongPipeline.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                LongConsumer downstreamAsLong = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsLong);
                            }
                            else {
                                var s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsLong));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 20
Source File: LongPipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
            return new Sink.ChainedLong<Long>(sink) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                LongConsumer downstreamAsLong = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(long t) {
                    try (LongStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsLong);
                            }
                            else {
                                Spliterator.OfLong s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsLong));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}