Advertisement
Guest User

Contribution

a guest
May 27th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. /**
  2.  * Fields representing all the values users can set when creating a new Contribution resource.
  3.  * @param <T> A class type representing the DTO with information about the movie.
  4.  */
  5. @Getter
  6. @Setter
  7. @JsonDeserialize(builder = ContributionNewRequest.Builder.class)
  8. @ApiModel(description = "An object representing a new contribution")
  9. public class ContributionNewRequest<T extends MovieInfoDTO> extends CommonRequest {
  10.  
  11.     private static final long serialVersionUID = -5646714920444796543L;
  12.  
  13.     @ApiModelProperty(notes = "Elements to be added <A new element>")
  14.     @Valid
  15.     private List<T> elementsToAdd = new ArrayList<>();
  16.  
  17.     @ApiModelProperty(notes = "Elements to be updated <The ID of the updated element, A new element>")
  18.     @Valid
  19.     private Map<Long, T> elementsToUpdate = new HashMap<>();
  20.  
  21.     @ApiModelProperty(notes = "Element IDs to be deleted <The ID of the element to be deleted>")
  22.     private Set<Long> idsToDelete = new HashSet<>();
  23.  
  24.     @ApiModelProperty(notes = "Sources of information(elements)", required = true)
  25.     @NotEmpty
  26.     private Set<String> sources = new HashSet<>();
  27.  
  28.     @ApiModelProperty(notes = "Comment from the user")
  29.     private String comment;
  30.  
  31.     /**
  32.      * Constructor only accessible via builder build() method.
  33.      *
  34.      * @param builder The builder to get data from
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     private ContributionNewRequest(final Builder builder) {
  38.         this.elementsToAdd = builder.bElementsToAdd;
  39.         this.elementsToUpdate = builder.bElementsToUpdate;
  40.         this.idsToDelete = builder.bIdsToDelete;
  41.         this.sources = builder.bSources;
  42.         this.comment = builder.bComment;
  43.     }
  44.  
  45.     /**
  46.      * A builder to create ContributionNewRequest.
  47.      */
  48.     public static class Builder<T> {
  49.  
  50.         private final List<T> bElementsToAdd = new ArrayList<>();
  51.         private final Map<Long, T> bElementsToUpdate = new HashMap<>();
  52.         private final Set<Long> bIdsToDelete = new HashSet<>();
  53.         private final Set<String> bSources;
  54.         private String bComment;
  55.  
  56.         /**
  57.          * Constructor which has required fields.
  58.          *
  59.          * @param sources Sources of information(elements)
  60.          */
  61.         @JsonCreator
  62.         public Builder(
  63.                 @JsonProperty("sources") final Set<String> sources
  64.         ) {
  65.             this.bSources = sources;
  66.         }
  67.  
  68.         /**
  69.          * Set elements to add on this contribution.
  70.          *
  71.          * @param elementsToAdd Elements to add
  72.          * @return The builder
  73.          */
  74.         public Builder withElementsToAdd(@Nullable final List<T> elementsToAdd) {
  75.             this.bElementsToAdd.clear();
  76.             if(elementsToAdd != null) {
  77.                 this.bElementsToAdd.addAll(elementsToAdd);
  78.             }
  79.             return this;
  80.         }
  81.  
  82.         /**
  83.          * Set elements to update on this contribution.
  84.          *
  85.          * @param elementsToUpdate Elements to update
  86.          * @return The builder
  87.          */
  88.         public Builder withElementsToUpdate(@Nullable final Map<Long, T> elementsToUpdate) {
  89.             this.bElementsToUpdate.clear();
  90.             if(elementsToUpdate != null) {
  91.                 this.bElementsToUpdate.putAll(elementsToUpdate);
  92.             }
  93.             return this;
  94.         }
  95.  
  96.         /**
  97.          * Set ids to delete on this contribution.
  98.          *
  99.          * @param idsToDelete Ids to delete
  100.          * @return The builder
  101.          */
  102.         public Builder withIdsToDelete(@Nullable final Set<Long> idsToDelete) {
  103.             this.bIdsToDelete.clear();
  104.             if(idsToDelete != null) {
  105.                 this.bIdsToDelete.addAll(idsToDelete);
  106.             }
  107.             return this;
  108.         }
  109.  
  110.         /**
  111.          * Set the user's comment on this contribution.
  112.          *
  113.          * @param comment The user's comment
  114.          * @return The builder
  115.          */
  116.         public Builder withComment(@Nullable final String comment) {
  117.             this.bComment = comment;
  118.             return this;
  119.         }
  120.  
  121.         /**
  122.          * Build the ContributionNewRequest.
  123.          *
  124.          * @return Create the final read-only ContributionNewRequest instance
  125.          */
  126.         public ContributionNewRequest build() {
  127.             return new ContributionNewRequest(this);
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement