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.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.spi.JobEventGroupSpi;
38 import it.tidalwave.accounting.model.spi.JobEventSpi;
39 import it.tidalwave.accounting.model.types.Money;
40 import lombok.EqualsAndHashCode;
41 import lombok.ToString;
42
43 /***************************************************************************************************************************************************************
44 *
45 * @author Fabrizio Giudici
46 *
47 **************************************************************************************************************************************************************/
48 @Immutable @EqualsAndHashCode(callSuper = true) @ToString(exclude = "events", callSuper = true)
49 public class InMemoryJobEventGroup extends InMemoryJobEvent implements JobEventGroupSpi
50 {
51 @Nonnull
52 private final List<InMemoryJobEvent> events; // FIXME: immutable
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 @Override @Nonnull
67 public JobEvent.Builder toBuilder()
68 {
69 return new Builder(id, null, null, null, name,
70 description, null, null, new ArrayList<>(findChildren().results()));
71 }
72
73 /***********************************************************************************************************************************************************
74 * {@inheritDoc}
75 **********************************************************************************************************************************************************/
76 @Override @Nonnull
77 public LocalDateTime getDateTime()
78 {
79 // return findChildren().sorted(comparing(JobEvent::getDateTime)).findFirst().get().getDateTime();
80 final BinaryOperator<LocalDateTime> min = (a, b) -> a.isAfter(b) ? b : a;
81 return findChildren().stream().map(jobEvent -> (JobEventSpi)jobEvent)
82 .map(JobEventSpi::getDateTime)
83 .reduce(min)
84 .orElseThrow();
85 }
86
87 /***********************************************************************************************************************************************************
88 * {@inheritDoc}
89 **********************************************************************************************************************************************************/
90 @Override @Nonnull
91 public Money getEarnings()
92 {
93 return findChildren().getEarnings();
94 }
95
96 /***********************************************************************************************************************************************************
97 * {@inheritDoc}
98 **********************************************************************************************************************************************************/
99 @Override @Nonnull
100 public Duration getDuration()
101 {
102 return findChildren().getDuration();
103 }
104
105 /***********************************************************************************************************************************************************
106 * {@inheritDoc}
107 **********************************************************************************************************************************************************/
108 @Override @Nonnull
109 public ProjectRegistry.JobEventFinder findChildren()
110 {
111 return new InMemoryJobEventFinderFromList(events);
112 }
113 }