RFC: draft API for JEP 269 Convenience Collection Factories

Stephen Colebourne scolebourne at joda.org
Thu Oct 15 14:28:00 UTC 2015


I've been working on a Java 8 wrapper class around double[] in my day
job, and added the following factory method:

  /**
   * Obtains an instance with entries filled using a function.
   * <p>
   * The function is passed the array index and returns the value for
that index.
   *
   * @param size  the number of elements
   * @param valueFunction  the function used to populate the value
   * @return an array initialized using the function
   */
  public static DoubleMatrix1D of(int size, IntToDoubleFunction valueFunction) {
    if (size == 0) {
      return EMPTY;
    }
    double[] array = new double[size];
    for (int i = 0; i < array.length; i++) {
      array[i] = valueFunction.applyAsDouble(i);
    }
    return new DoubleMatrix1D(array);
  }

View on GitHub here:
https://github.com/OpenGamma/Strata/commit/63e73652194a3dd94e37fbc407f4933c10abadda#diff-2a9787868cf0654b4a6a07e75c1a6286R199

It occurs to me that it would be a *very* good idea to add a similar
method to List.

public static <T> List of(int size, IntFunction<T> valueFunction) { ... }

Stephen



On 14 October 2015 at 11:39, Stephen Colebourne <scolebourne at joda.org> wrote:
> On 14 October 2015 at 10:38, Paul Sandoz <paul.sandoz at oracle.com> wrote:
>>> On 14 Oct 2015, at 06:18, Stuart Marks <stuart.marks at oracle.com> wrote:
>>> I'm not entirely sure what to take from this. If it were clearly exponential, we could say with confidence that above a certain threshold there would be vanishingly little benefit adding more arguments. But since the curve seems to flatten out, maybe this is pushing us to add more pairs than we had originally thought. The current draft API has 8 pairs; that seems to leave a few percent of cases on the table. Obviously we can't get to 100%, but is 97% good enough?
>
> I'd say 5 is definitely too little, without an easy builder fallback
> (as Guava provides). I'd say the right number is between 8 and 10.
>
> Stephen



More information about the core-libs-dev mailing list