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.util.Collection;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.StringJoiner;
25  
26  import org.eclipse.microprofile.openapi.annotations.media.Schema;
27  
28  import com.fasterxml.jackson.annotation.JsonIgnore;
29  import com.fasterxml.jackson.annotation.JsonInclude;
30  
31  import de.kaiserpfalzedv.commons.api.resources.DefaultResourceSpec;
32  import de.kaiserpfalzedv.commons.api.resources.Pointer;
33  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
34  import lombok.AllArgsConstructor;
35  import lombok.Builder;
36  import lombok.EqualsAndHashCode;
37  import lombok.NoArgsConstructor;
38  import lombok.ToString;
39  import lombok.experimental.SuperBuilder;
40  import lombok.extern.jackson.Jacksonized;
41  
42  /**
43   * The basic data for every resource.
44   *
45   * @author klenkes74 {@literal <rlichti@kaiserpfalz-edv.de>}
46   * @since 2.0.0  2021-05-24
47   */
48  @SuppressFBWarnings(value = "EI_EXPOSE_REF2", justification = "Use of lombok provided builder.")
49  @Jacksonized
50  @SuperBuilder(toBuilder = true)
51  @AllArgsConstructor
52  @NoArgsConstructor
53  @ToString(onlyExplicitlyIncluded = true)
54  @EqualsAndHashCode(onlyExplicitlyIncluded = true)
55  @JsonInclude(JsonInclude.Include.NON_ABSENT)
56  @Schema(name = "DefaultResourceSpec", description = "A standardized resource.")
57  public class DefaultResourceSpecImpl implements DefaultResourceSpec {
58      /** serial version of this class. */
59      private static final long serialVersionUID = 0L;
60  
61      @SuppressWarnings("FieldMayBeFinal")
62      @SuppressFBWarnings(value = "EI_EXPOSE_REF2", justification = "Use of lombok provided builder.")
63      @Schema(name = "properties", description = "A map of plugin properties for spec.")
64      @Builder.Default
65      @ToString.Include
66      @EqualsAndHashCode.Include
67      private Map<String, String> properties = new HashMap<>();
68  
69      @Override
70      public Map<String, String> getProperties() {
71          return Collections.unmodifiableMap(this.properties);
72      }
73  
74      @Override
75      @JsonIgnore
76      public Pointer convertStringToResourcePointer(final String property) {
77          final String[] data = property.split("/", 4);
78          if (data.length != 5) {
79              throw new IllegalStateException("Invalid property for resource pointers: " + property);
80          }
81  
82          return PointerImpl.builder()
83                  .kind(data[0])
84                  .apiVersion(data[1])
85  
86                  .nameSpace(data[2])
87                  .name(data[3])
88  
89                  .build();
90      }
91  
92      @Override
93      @JsonIgnore
94      public void saveResourcePointers(final String key, final Collection<Pointer> pointers) {
95          if (pointers != null) {
96              final StringJoiner data = new StringJoiner(",");
97  
98              pointers.forEach(p -> data.add(this.convertResourcePointerToString(p)));
99  
100             this.properties.put(key, data.toString());
101         }
102     }
103 
104     @SuppressWarnings("MethodDoesntCallSuperMethod")
105     @SuppressFBWarnings(value = "CN_IDIOM_NO_SUPER_CALL", justification = "We are using the lombok builder here.")
106     @Override
107     public DefaultResourceSpecImpl clone() {
108         return this.toBuilder().build();
109     }
110 }