InMemoryJobEventGroup.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * blueHour
  6.  * http://bluehour.tidalwave.it - git clone git@bitbucket.org:tidalwave/bluehour-src.git
  7.  * %%
  8.  * Copyright (C) 2013 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.accounting.model.impl;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.concurrent.Immutable;
  30. import java.time.Duration;
  31. import java.time.LocalDateTime;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.function.BinaryOperator;
  35. import it.tidalwave.accounting.model.JobEvent;
  36. import it.tidalwave.accounting.model.ProjectRegistry;
  37. import it.tidalwave.accounting.model.types.Money;
  38. import it.tidalwave.accounting.model.spi.JobEventGroupSpi;
  39. import it.tidalwave.accounting.model.spi.JobEventSpi;
  40. import lombok.EqualsAndHashCode;
  41. import lombok.ToString;

  42. /***********************************************************************************************************************
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @Immutable @EqualsAndHashCode(callSuper = true) @ToString(exclude = "events", callSuper = true)
  48. public class InMemoryJobEventGroup extends InMemoryJobEvent implements JobEventGroupSpi
  49.   {
  50.     @Nonnull
  51.     private final List<InMemoryJobEvent> events; // FIXME: immutable

  52.     /*******************************************************************************************************************
  53.      *
  54.      *
  55.      *
  56.      ******************************************************************************************************************/
  57.     public /* FIXME protected */ InMemoryJobEventGroup (@Nonnull final Builder builder)
  58.       {
  59.         super(builder);
  60.         this.events = (List<InMemoryJobEvent>)builder.getEvents();
  61.       }

  62.     /*******************************************************************************************************************
  63.      *
  64.      * {@inheritDoc}
  65.      *
  66.      ******************************************************************************************************************/
  67.     @Override @Nonnull
  68.     public JobEvent.Builder toBuilder()
  69.       {
  70.         return new Builder(id, null, null, null, name,
  71.                            description, null, null, new ArrayList<>(findChildren().results()));
  72.       }
  73.    
  74.     /*******************************************************************************************************************
  75.      *
  76.      * {@inheritDoc}
  77.      *
  78.      ******************************************************************************************************************/
  79.     @Override @Nonnull
  80.     public LocalDateTime getDateTime()
  81.       {
  82. //        return findChildren().sorted(comparing(JobEvent::getDateTime)).findFirst().get().getDateTime();  
  83.         final BinaryOperator<LocalDateTime> min = (a, b) -> a.isAfter(b) ? b : a;
  84.         return findChildren().stream().map(jobEvent -> (JobEventSpi)jobEvent)
  85.                                       .map(JobEventSpi::getDateTime)
  86.                                       .reduce(min).get();
  87.       }
  88.    
  89.     /*******************************************************************************************************************
  90.      *
  91.      * {@inheritDoc}
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Override @Nonnull
  95.     public Money getEarnings()
  96.       {
  97.         return findChildren().getEarnings();
  98.       }
  99.    
  100.     /*******************************************************************************************************************
  101.      *
  102.      * {@inheritDoc}
  103.      *
  104.      ******************************************************************************************************************/
  105.     @Override @Nonnull
  106.     public Duration getDuration()
  107.       {
  108.         return findChildren().getDuration();
  109.       }
  110.    
  111.     /*******************************************************************************************************************
  112.      *
  113.      * {@inheritDoc}
  114.      *
  115.      ******************************************************************************************************************/
  116.     @Override @Nonnull
  117.     public ProjectRegistry.JobEventFinder findChildren()
  118.       {
  119.         return new InMemoryJobEventFinderFromList(events);  
  120.       }
  121.   }