InMemoryCustomer.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.util.Map;
  31. import it.tidalwave.util.As;
  32. import it.tidalwave.util.Id;
  33. import it.tidalwave.util.spi.FinderWithIdMapSupport;
  34. import it.tidalwave.accounting.model.Accounting;
  35. import it.tidalwave.accounting.model.Project;
  36. import it.tidalwave.accounting.model.ProjectRegistry;
  37. import it.tidalwave.accounting.model.types.Address;
  38. import it.tidalwave.accounting.model.spi.CustomerSpi;
  39. import lombok.EqualsAndHashCode;
  40. import lombok.experimental.Delegate;
  41. import lombok.Getter;
  42. import lombok.Setter;
  43. import lombok.ToString;
  44. import static java.util.stream.Collectors.toMap;

  45. /***********************************************************************************************************************
  46.  *
  47.  * This class models a customer.
  48.  *
  49.  * @author  Fabrizio Giudici
  50.  *
  51.  **********************************************************************************************************************/
  52. @Immutable @Getter @EqualsAndHashCode @ToString(exclude = {"accounting", "as"}) // FIXME: remove the @Getter
  53. public class InMemoryCustomer implements CustomerSpi
  54.   {
  55.     private static final long serialVersionUID = 1L;
  56.    
  57.     static class InMemoryProjectFinder extends FinderWithIdMapSupport<Project, InMemoryProject, ProjectRegistry.ProjectFinder>
  58.                                 implements ProjectRegistry.ProjectFinder
  59.       {
  60.         private static final long serialVersionUID = 1L;
  61.    
  62.         public InMemoryProjectFinder (@Nonnull final InMemoryProjectFinder other, @Nonnull final Object override)
  63.           {
  64.             super(other, override);  
  65.           }
  66.        
  67.         InMemoryProjectFinder (@Nonnull final Map<Id, InMemoryProject> projectMapById)
  68.           {
  69.             super(projectMapById);  
  70.           }
  71.       }
  72.    
  73.     @Delegate
  74.     private final As as = As.forObject(this);

  75.     @Getter @Nonnull
  76.     private final Id id;

  77.     @Nonnull
  78.     private final String name;

  79.     @Nonnull
  80.     private final Address billingAddress;

  81.     @Nonnull
  82.     private final String vatNumber;
  83.    
  84.     @Setter @Nonnull // FIXME: drop the setter!
  85.     private Accounting accounting;

  86.     /*******************************************************************************************************************
  87.      *
  88.      *
  89.      *
  90.      ******************************************************************************************************************/
  91.     public /* FIXME protected */ InMemoryCustomer (@Nonnull final Builder builder)
  92.       {
  93.         this.id = builder.getId();
  94.         this.name = builder.getName();
  95.         this.billingAddress = builder.getBillingAddress();
  96.         this.vatNumber = builder.getVatNumber();
  97.       }

  98.     /*******************************************************************************************************************
  99.      *
  100.      *
  101.      *
  102.      ******************************************************************************************************************/
  103.     @Override @Nonnull
  104.     public ProjectRegistry.ProjectFinder findProjects()
  105.       {
  106.         return new InMemoryProjectFinder(accounting.getProjectRegistry().findProjects()
  107.                                                    .stream()
  108.                                                    .filter(project -> project.getCustomer().getId().equals(getId()))
  109.                                                    .collect(toMap(Project::getId, project -> (InMemoryProject)project)));
  110.       }
  111.    
  112.     /*******************************************************************************************************************
  113.      *
  114.      * {@inheritDoc}
  115.      *
  116.      ******************************************************************************************************************/
  117.     @Override @Nonnull
  118.     public Builder toBuilder()
  119.       {
  120.         return new Builder(id, name, billingAddress, vatNumber, InMemoryCustomer.Builder.Callback.DEFAULT);                
  121.       }
  122.   }