InMemoryInvoiceFinderFromMap.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.Nullable;
  30. import java.util.List;
  31. import java.util.Map;
  32. import it.tidalwave.util.Id;
  33. import it.tidalwave.util.spi.FinderWithIdMapSupport;
  34. import it.tidalwave.accounting.model.Invoice;
  35. import it.tidalwave.accounting.model.InvoiceRegistry;
  36. import it.tidalwave.accounting.model.Project;
  37. import it.tidalwave.accounting.model.spi.InvoiceSpi;
  38. import it.tidalwave.accounting.model.types.Money;
  39. import lombok.RequiredArgsConstructor;
  40. import static java.util.stream.Collectors.toList;

  41. /***********************************************************************************************************************
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **********************************************************************************************************************/
  46. @RequiredArgsConstructor
  47. public class InMemoryInvoiceFinderFromMap
  48.         extends FinderWithIdMapSupport<Invoice, InMemoryInvoice, InvoiceRegistry.Finder>
  49.         implements InvoiceRegistry.Finder
  50.   {
  51.     private static final long serialVersionUID = 1L;
  52.    
  53.     @Nullable
  54.     private final Project project;

  55.     public InMemoryInvoiceFinderFromMap (@Nonnull final Map<Id, InMemoryInvoice> invoiceMapById)
  56.       {
  57.         super(invoiceMapById);  
  58.         this.project = null;
  59.       }
  60.    
  61.     public InMemoryInvoiceFinderFromMap (@Nonnull final InMemoryInvoiceFinderFromMap other,
  62.                                          @Nonnull final Object override)
  63.       {
  64.         super(other, override);
  65.         final var source = getSource(InMemoryInvoiceFinderFromMap.class, other, override);
  66.         this.project = source.project;
  67.       }

  68.     @Override @Nonnull
  69.     public InvoiceRegistry.Finder withProject (@Nonnull final Project project)
  70.       {
  71.         return clonedWith(new InMemoryInvoiceFinderFromMap(project));
  72.       }

  73.     @Override @Nonnull
  74.     protected List<Invoice> computeResults()
  75.       {
  76.         var stream = super.computeResults().stream();

  77.         if (project != null)
  78.           {
  79.             stream = stream.filter(invoice -> ((InvoiceSpi)invoice).getProject().equals(project));
  80.           }
  81.        
  82.         return stream.collect(toList());
  83.       }

  84.     @Override @Nonnull
  85.     public Money getEarnings()
  86.       {
  87.         return streamImpl().map(InMemoryInvoice::getEarnings).reduce(Money.ZERO, Money::add);
  88.       }
  89.   }