InMemoryCustomer.java

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

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

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

  76.     @Nonnull
  77.     private final String name;

  78.     @Nonnull
  79.     private final Address billingAddress;

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

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

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