1515import java .math .BigDecimal ;
1616import java .math .MathContext ;
1717import java .time .Duration ;
18+ import java .time .Instant ;
1819import java .time .ZoneId ;
1920import java .time .ZonedDateTime ;
2021import java .util .ArrayList ;
7172 * @author Mark Herwege - add median methods
7273 * @author Mark Herwege - use item lastChange and lastUpdate methods if not in peristence
7374 * @author Mark Herwege - add Riemann sum methods
75+ * @author Jörg Sautter - use Instant instead of ZonedDateTime in Riemann sum methods
7476 */
7577@ Component (immediate = true )
7678@ NonNullByDefault
@@ -1926,7 +1928,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
19261928
19271929 private static @ Nullable BigDecimal average (ZonedDateTime begin , ZonedDateTime end , Iterator <HistoricItem > it ,
19281930 @ Nullable Unit <?> unit , @ Nullable RiemannType type ) {
1929- BigDecimal sum = riemannSum (begin , end , it , unit , type );
1931+ BigDecimal sum = riemannSum (begin . toInstant () , end . toInstant () , it , unit , type );
19301932 BigDecimal totalDuration = BigDecimal .valueOf (Duration .between (begin , end ).toMillis ());
19311933 if (totalDuration .signum () == 0 ) {
19321934 return null ;
@@ -2195,35 +2197,39 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
21952197 Item baseItem = item instanceof GroupItem groupItem ? groupItem .getBaseItem () : item ;
21962198 Unit <?> unit = (baseItem instanceof NumberItem numberItem )
21972199 && (numberItem .getUnit () instanceof Unit <?> numberItemUnit ) ? numberItemUnit .getSystemUnit () : null ;
2198- BigDecimal sum = riemannSum (beginTime , endTime , it , unit , type ).scaleByPowerOfTen (-3 );
2200+ BigDecimal sum = riemannSum (beginTime . toInstant () , endTime . toInstant () , it , unit , type ).scaleByPowerOfTen (-3 );
21992201 if (unit != null ) {
22002202 return new QuantityType <>(sum , unit .multiply (Units .SECOND ));
22012203 }
22022204 return new DecimalType (sum );
22032205 }
22042206
2205- private static BigDecimal riemannSum (ZonedDateTime begin , ZonedDateTime end , Iterator <HistoricItem > it ,
2206- @ Nullable Unit <?> unit , @ Nullable RiemannType type ) {
2207+ private static BigDecimal riemannSum (Instant begin , Instant end , Iterator <HistoricItem > it , @ Nullable Unit <?> unit ,
2208+ @ Nullable RiemannType type ) {
22072209 RiemannType riemannType = type == null ? RiemannType .LEFT : type ;
22082210
22092211 BigDecimal sum = BigDecimal .ZERO ;
22102212 HistoricItem prevItem = null ;
2211- HistoricItem nextItem = null ;
2213+ HistoricItem nextItem ;
22122214 DecimalType prevState = null ;
2213- DecimalType nextState = null ;
2215+ DecimalType nextState ;
2216+ Instant prevInstant = null ;
2217+ Instant nextInstant ;
22142218 Duration prevDuration = Duration .ZERO ;
2215- Duration nextDuration = Duration . ZERO ;
2219+ Duration nextDuration ;
22162220
22172221 boolean midpointStartBucket = true ; // The start and end buckets for the midpoint calculation should be
22182222 // considered for the full length, this flag is used to find the start
22192223 // bucket
22202224 if ((riemannType == RiemannType .MIDPOINT ) && it .hasNext ()) {
22212225 prevItem = it .next ();
2226+ prevInstant = prevItem .getInstant ();
22222227 prevState = getPersistedValue (prevItem , unit );
22232228 }
22242229
22252230 while (it .hasNext ()) {
22262231 nextItem = it .next ();
2232+ nextInstant = nextItem .getInstant ();
22272233 BigDecimal weight = BigDecimal .ZERO ;
22282234 BigDecimal value = BigDecimal .ZERO ;
22292235 switch (riemannType ) {
@@ -2232,24 +2238,24 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22322238 prevState = getPersistedValue (prevItem , unit );
22332239 if (prevState != null ) {
22342240 value = prevState .toBigDecimal ();
2235- weight = BigDecimal .valueOf (
2236- Duration .between (prevItem .getTimestamp (), nextItem .getTimestamp ()).toMillis ());
2241+ weight = BigDecimal .valueOf (Duration .between (prevInstant , nextInstant ).toMillis ());
22372242 }
22382243 }
22392244 prevItem = nextItem ;
2245+ prevInstant = nextInstant ;
22402246 break ;
22412247 case RIGHT :
22422248 nextState = getPersistedValue (nextItem , unit );
22432249 if (nextState != null ) {
22442250 value = nextState .toBigDecimal ();
22452251 if (prevItem == null ) {
2246- weight = BigDecimal .valueOf (Duration .between (begin , nextItem . getTimestamp () ).toMillis ());
2252+ weight = BigDecimal .valueOf (Duration .between (begin , nextInstant ).toMillis ());
22472253 } else {
2248- weight = BigDecimal .valueOf (
2249- Duration .between (prevItem .getTimestamp (), nextItem .getTimestamp ()).toMillis ());
2254+ weight = BigDecimal .valueOf (Duration .between (prevInstant , nextInstant ).toMillis ());
22502255 }
22512256 }
22522257 prevItem = nextItem ;
2258+ prevInstant = nextInstant ;
22532259 break ;
22542260 case TRAPEZOIDAL :
22552261 if (prevItem != null ) {
@@ -2258,11 +2264,11 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22582264 if (prevState != null && nextState != null ) {
22592265 value = prevState .toBigDecimal ().add (nextState .toBigDecimal ())
22602266 .divide (BigDecimal .valueOf (2 ));
2261- weight = BigDecimal .valueOf (
2262- Duration .between (prevItem .getTimestamp (), nextItem .getTimestamp ()).toMillis ());
2267+ weight = BigDecimal .valueOf (Duration .between (prevInstant , nextInstant ).toMillis ());
22632268 }
22642269 }
22652270 prevItem = nextItem ;
2271+ prevInstant = nextInstant ;
22662272 break ;
22672273 case MIDPOINT :
22682274 if (prevItem != null ) {
@@ -2272,19 +2278,20 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22722278 if (midpointStartBucket && !prevDuration .isZero () && prevState != null ) {
22732279 // Add half of the start bucket with the start value (left approximation)
22742280 sum = sum .add (prevState .toBigDecimal ()
2275- .multiply (BigDecimal .valueOf (prevDuration .dividedBy ( 2 ). toMillis ())));
2281+ .multiply (BigDecimal .valueOf (prevDuration .toMillis () / 2 )));
22762282 midpointStartBucket = false ;
22772283 }
2278- nextDuration = Duration .between (prevItem . getTimestamp (), nextItem . getTimestamp () );
2284+ nextDuration = Duration .between (prevInstant , nextInstant );
22792285 weight = prevDuration .isZero () || nextDuration .isZero () ? BigDecimal .ZERO
2280- : BigDecimal .valueOf (prevDuration .plus (nextDuration ).dividedBy ( 2 ). toMillis ());
2286+ : BigDecimal .valueOf (prevDuration .plus (nextDuration ).toMillis () / 2 );
22812287 if (!nextDuration .isZero ()) {
22822288 prevDuration = nextDuration ;
22832289 }
22842290 prevState = currentState ;
22852291 }
22862292 }
22872293 prevItem = nextItem ;
2294+ prevInstant = nextInstant ;
22882295 break ;
22892296 }
22902297 sum = sum .add (value .multiply (weight ));
@@ -2295,7 +2302,7 @@ private static BigDecimal riemannSum(ZonedDateTime begin, ZonedDateTime end, Ite
22952302 DecimalType dtState = getPersistedValue (prevItem , unit );
22962303 if (dtState != null ) {
22972304 BigDecimal value = dtState .toBigDecimal ();
2298- BigDecimal weight = BigDecimal .valueOf (prevDuration .dividedBy ( 2 ). toMillis ());
2305+ BigDecimal weight = BigDecimal .valueOf (prevDuration .toMillis () / 2 );
22992306 sum = sum .add (value .multiply (weight ));
23002307 }
23012308 }
0 commit comments