View Javadoc
1   /*
2    * Copyright (c) 2022-2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.commons.core.resources;
19  
20  import java.io.Serializable;
21  import java.util.Optional;
22  import java.util.UUID;
23  
24  import org.eclipse.microprofile.openapi.annotations.media.Schema;
25  
26  import com.fasterxml.jackson.annotation.JsonIgnore;
27  import com.fasterxml.jackson.annotation.JsonInclude;
28  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
29  
30  import de.kaiserpfalzedv.commons.api.resources.Metadata;
31  import de.kaiserpfalzedv.commons.api.resources.Resource;
32  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
33  import jakarta.validation.constraints.NotNull;
34  import lombok.AllArgsConstructor;
35  import lombok.Builder;
36  import lombok.EqualsAndHashCode;
37  import lombok.Getter;
38  import lombok.NoArgsConstructor;
39  import lombok.ToString;
40  import lombok.experimental.SuperBuilder;
41  import lombok.extern.jackson.Jacksonized;
42  
43  /**
44   * Resource -- A generic resource holding a single data set.
45   *
46   * @param <D> The data set provided by this resource.
47   * @author klenkes74 {@literal <rlichti@kaiserpfalz-edv.de>}
48   * @version 3.0.0  2023-01-07
49   * @version 2.1.0  2022-01-16
50   * @since 2.0.0  2021-05-24
51   */
52  @SuppressFBWarnings(value = "EI_EXPOSE_REF2", justification = "Use of lombok provided builder.")
53  @Jacksonized
54  @SuperBuilder(toBuilder = true)
55  @AllArgsConstructor
56  @NoArgsConstructor
57  @Getter
58  @ToString(onlyExplicitlyIncluded = true, callSuper = true)
59  @EqualsAndHashCode(onlyExplicitlyIncluded = true)
60  @JsonInclude(JsonInclude.Include.NON_ABSENT)
61  @JsonPropertyOrder({"metadata", "spec", "status"})
62  public class ResourceImpl<D extends Serializable> implements Resource<D> {
63      /** serial class version */
64      private static final long serialVersionUID = 0L;
65  
66      @Schema(
67              name = "metadata",
68              description = "Technical data to the resource.",
69              required = true
70      )
71      @NotNull
72      @ToString.Include
73      @EqualsAndHashCode.Include
74      protected Metadata metadata;
75  
76  
77      @Schema(
78              name = "spec",
79              description = "The resource data itself.",
80              required = true
81      )
82      @Builder.Default
83      protected D spec = null;
84  
85  
86      @Schema(
87              name = "status",
88              description = "The status of the resource (containing the history).",
89              nullable = true
90      )
91      @Builder.Default
92      @SuppressFBWarnings(value = {"EI_EXPOSE_REP","EI_EXPOSE_REP2"}, justification = "lombok provided @Getter are created")
93      protected StatusImpl status = null;
94  
95  
96      @Override
97      @JsonIgnore
98      public PointerImpl toPointer() {
99          return PointerImpl.builder()
100                 .kind(this.getKind())
101                 .apiVersion(this.getApiVersion())
102                 .nameSpace(this.getNameSpace())
103                 .nameSpace(this.getName())
104                 .build();
105     }
106 
107     @JsonIgnore
108     @Override
109     public String getKind() {
110         return this.getMetadata().getKind();
111     }
112 
113     @JsonIgnore
114     @Override
115     public String getApiVersion() {
116         return this.getMetadata().getApiVersion();
117     }
118 
119     @JsonIgnore
120     @Override
121     public String getNameSpace() {
122         return this.getMetadata().getNameSpace();
123     }
124 
125     @JsonIgnore
126     @Override
127     public String getName() {
128         return this.getMetadata().getName();
129     }
130 
131     @Override
132     @JsonIgnore
133     public UUID getUid() {
134         return this.metadata.getUid();
135     }
136 
137     @Override
138     @JsonIgnore
139     public Integer getGeneration() {
140         return this.metadata.getGeneration();
141     }
142 
143 
144     @JsonIgnore
145     public Optional<D> getData() {
146         return Optional.ofNullable(this.spec);
147     }
148 
149     @JsonIgnore
150     public Optional<StatusImpl> getState() {
151         return Optional.ofNullable(this.status);
152     }
153 
154     @Override
155     @JsonIgnore
156     public String getSelfLink() {
157         return this.getMetadata().getSelfLink();
158     }
159 
160 
161     @Override
162     synchronized public ResourceImpl<D> increaseGeneration() {
163         return this.toBuilder()
164                 .metadata(this.getMetadata().increaseGeneration())
165                 .build();
166     }
167 
168 
169     @SuppressWarnings("MethodDoesntCallSuperMethod")
170     @SuppressFBWarnings(value = "CN_IDIOM_NO_SUPER_CALL", justification = "Using the lombok builder.")
171     @Override
172     public ResourceImpl<D> clone() {
173         return this.toBuilder().build();
174     }
175 }