1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * blueHour: open source accounting
5 * http://tidalwave.it/projects/bluehour
6 *
7 * Copyright (C) 2013 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/bluehour-src
22 * git clone https://github.com/tidalwave-it/bluehour-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.accounting.model.impl;
27
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30 import java.util.List;
31 import java.util.Map;
32 import it.tidalwave.accounting.model.Invoice;
33 import it.tidalwave.accounting.model.InvoiceRegistry;
34 import it.tidalwave.accounting.model.Project;
35 import it.tidalwave.accounting.model.spi.InvoiceSpi;
36 import it.tidalwave.accounting.model.types.Money;
37 import it.tidalwave.util.Id;
38 import it.tidalwave.util.spi.FinderWithIdMapSupport;
39 import lombok.RequiredArgsConstructor;
40 import static java.util.stream.Collectors.*;
41
42 /***************************************************************************************************************************************************************
43 *
44 * @author Fabrizio Giudici
45 *
46 **************************************************************************************************************************************************************/
47 @RequiredArgsConstructor
48 public class InMemoryInvoiceFinderFromMap
49 extends FinderWithIdMapSupport<Invoice, InMemoryInvoice, InvoiceRegistry.Finder>
50 implements InvoiceRegistry.Finder
51 {
52 private static final long serialVersionUID = 1L;
53
54 @Nullable
55 private final Project project;
56
57 public InMemoryInvoiceFinderFromMap (@Nonnull final Map<Id, InMemoryInvoice> invoiceMapById)
58 {
59 super(invoiceMapById);
60 this.project = null;
61 }
62
63 public InMemoryInvoiceFinderFromMap (@Nonnull final InMemoryInvoiceFinderFromMap other,
64 @Nonnull final Object override)
65 {
66 super(other, override);
67 final var source = getSource(InMemoryInvoiceFinderFromMap.class, other, override);
68 this.project = source.project;
69 }
70
71 @Override @Nonnull
72 public InvoiceRegistry.Finder withProject (@Nonnull final Project project)
73 {
74 return clonedWith(new InMemoryInvoiceFinderFromMap(project));
75 }
76
77 @Override @Nonnull
78 protected List<Invoice> computeResults()
79 {
80 var stream = super.computeResults().stream();
81
82 if (project != null)
83 {
84 stream = stream.filter(invoice -> ((InvoiceSpi)invoice).getProject().equals(project));
85 }
86
87 return stream.collect(toList());
88 }
89
90 @Override @Nonnull
91 public Money getEarnings()
92 {
93 return streamImpl().map(InMemoryInvoice::getEarnings).reduce(Money.ZERO, Money::add);
94 }
95 }