Skip to content
This repository has been archived by the owner on May 29, 2018. It is now read-only.

Commit

Permalink
PLAT-6840 Replace noCopy() with withValues()
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartw committed Oct 22, 2014
1 parent 9ae6e55 commit 48b16ab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Expand Up @@ -109,19 +109,6 @@ public static ImmutableLocalDateDoubleTimeSeries of(int[] dates, double[] values
return new ImmutableLocalDateDoubleTimeSeries(timesArray, valuesArray);
}

/**
* Obtains a time-series from matching arrays of dates and values.
* Arrays are *not* copied on construction. If either array is modified the time-series will also be modified.
*
* @param dates the date array, not null
* @param values the value array, not null
* @return the time-series, not null
*/
public static ImmutableLocalDateDoubleTimeSeries noCopy(int[] dates, double[] values) {
validate(dates, values);
return new ImmutableLocalDateDoubleTimeSeries(dates, values);
}

/**
* Obtains a time-series from matching arrays of dates and values.
*
Expand Down Expand Up @@ -397,4 +384,15 @@ public LocalDateDoubleTimeSeriesBuilder toBuilder() {
return builder().putAll(this);
}

/**
* Obtain a new time-series with the same times and new values
*
* @param values the new values, not null
* @return time series, not null
*/
public LocalDateDoubleTimeSeries withValues(double[] values) {
validate(_times, values);
return new ImmutableLocalDateDoubleTimeSeries(_times, values.clone()); // immutable, so can share times
}

}
Expand Up @@ -55,6 +55,8 @@ public interface LocalDateDoubleTimeSeries
@Override // override for covariant return type
LocalDateDoubleTimeSeries lag(int lagCount);

LocalDateDoubleTimeSeries withValues(double[] values);

//-------------------------------------------------------------------------
@Override // override for covariant return type
LocalDateDoubleTimeSeries operate(UnaryOperator operator);
Expand Down
Expand Up @@ -762,10 +762,11 @@ public void test_builder_toString() {
public void test_noCopy() {
int[] times = new int[] {10101, 10102, 10103, 10104};
double[] values = new double[] {0d, 1d, 2d, 3d};
LocalDateDoubleTimeSeries series = ImmutableLocalDateDoubleTimeSeries.noCopy(times, values);
LocalDateDoubleTimeSeries series = ImmutableLocalDateDoubleTimeSeries.of(times, values);
assertEquals(2d, series.getValueAtIndex(2));
values[2] = 5; // modification of backing array will be reflected in time series
assertEquals(5d, series.getValueAtIndex(2));
values[2] = 5;
LocalDateDoubleTimeSeries series2 = series.withValues(values);
assertEquals(5d, series2.getValueAtIndex(2));
}

}

0 comments on commit 48b16ab

Please sign in to comment.