1+ /*
2+ * Copyright (c) 2025 Karlsruhe Institute of Technology.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117package edu .kit .datamanager .pit .domain ;
218
319import com .fasterxml .jackson .annotation .JsonIgnore ;
4-
520import edu .kit .datamanager .entities .EtagSupport ;
621import edu .kit .datamanager .pit .pidsystem .impl .local .PidDatabaseObject ;
722
8- import java .util .ArrayList ;
9- import java .util .Collection ;
10- import java .util .HashMap ;
11- import java .util .List ;
12- import java .util .Map ;
23+ import java .util .*;
1324import java .util .stream .Collectors ;
14- import java .util .Set ;
1525
1626/**
1727 * The internal representation for a PID record, offering methods to manipulate
2131 * communication or representation for the outside. In contrast, this is the
2232 * internal representation offering methods for manipulation.
2333 */
24- public class PIDRecord implements EtagSupport {
34+ public class PIDRecord implements EtagSupport , Cloneable {
2535
2636 private String pid = "" ;
2737
@@ -30,11 +40,12 @@ public class PIDRecord implements EtagSupport {
3040 /**
3141 * Creates an empty record without PID.
3242 */
33- public PIDRecord () {}
43+ public PIDRecord () {
44+ }
3445
3546 /**
3647 * Creates a record with the same content as the given representation.
37- *
48+ *
3849 * @param dbo the given record representation.
3950 */
4051 public PIDRecord (PidDatabaseObject dbo ) {
@@ -53,7 +64,7 @@ public PIDRecord(SimplePidRecord rec) {
5364
5465 /**
5566 * Convenience setter / builder method.
56- *
67+ *
5768 * @param pid the pid to set in this object.
5869 * @return this object (builder method).
5970 */
@@ -74,6 +85,15 @@ public Map<String, List<PIDRecordEntry>> getEntries() {
7485 return entries ;
7586 }
7687
88+ /**
89+ * Sets the entries of this record.
90+ *
91+ * @param entries the entries to set.
92+ */
93+ public void setEntries (Map <String , List <PIDRecordEntry >> entries ) {
94+ this .entries = entries ;
95+ }
96+
7797 @ JsonIgnore
7898 public Set <SimplePair > getSimpleEntries () {
7999 return this .entries
@@ -85,20 +105,16 @@ public Set<SimplePair> getSimpleEntries() {
85105 .collect (Collectors .toSet ());
86106 }
87107
88- public void setEntries (Map <String , List <PIDRecordEntry >> entries ) {
89- this .entries = entries ;
90- }
91-
92108 public void addEntry (String propertyIdentifier , String propertyValue ) {
93109 this .addEntry (propertyIdentifier , "" , propertyValue );
94110 }
95111
96112 /**
97113 * Adds a new key-name-value triplet.
98- *
114+ *
99115 * @param propertyIdentifier the key/type PID.
100- * @param propertyName the human-readable name for the given key/type.
101- * @param propertyValue the value to this key/type.
116+ * @param propertyName the human-readable name for the given key/type.
117+ * @param propertyValue the value to this key/type.
102118 */
103119 public void addEntry (String propertyIdentifier , String propertyName , String propertyValue ) {
104120 if (propertyIdentifier .isEmpty ()) {
@@ -110,22 +126,22 @@ public void addEntry(String propertyIdentifier, String propertyName, String prop
110126 entry .setValue (propertyValue );
111127
112128 this .entries
113- .computeIfAbsent (propertyIdentifier , key -> new ArrayList <>())
114- .add (entry );
129+ .computeIfAbsent (propertyIdentifier , key -> new ArrayList <>())
130+ .add (entry );
115131 }
116132
117133 /**
118134 * Sets the name for a given key/type in all available pairs.
119- *
135+ *
120136 * @param propertyIdentifier the key/type.
121- * @param name the new name.
137+ * @param name the new name.
122138 */
123139 @ JsonIgnore
124140 public void setPropertyName (String propertyIdentifier , String name ) {
125141 List <PIDRecordEntry > propertyEntries = this .entries .get (propertyIdentifier );
126142 if (propertyEntries == null ) {
127143 throw new IllegalArgumentException (
128- "Property identifier not listed in this record: " + propertyIdentifier );
144+ "Property identifier not listed in this record: " + propertyIdentifier );
129145 }
130146 for (PIDRecordEntry entry : propertyEntries ) {
131147 entry .setName (name );
@@ -135,7 +151,7 @@ public void setPropertyName(String propertyIdentifier, String name) {
135151 /**
136152 * Check if there is a pair or triplet containing the given property (key/type)
137153 * is availeble in this record.
138- *
154+ *
139155 * @param propertyIdentifier the key/type to search for.
140156 * @return true, if the property/key/type is present.
141157 */
@@ -158,7 +174,7 @@ public void removeAllValuesOf(String attribute) {
158174
159175 /**
160176 * Get all properties contained in this record.
161- *
177+ *
162178 * @return al contained properties.
163179 */
164180 @ JsonIgnore
@@ -180,7 +196,7 @@ public String getPropertyValue(String propertyIdentifier) {
180196
181197 /**
182198 * Get all values of a given property.
183- *
199+ *
184200 * @param propertyIdentifier the given property identifier.
185201 * @return all values of the given property.
186202 */
@@ -194,7 +210,7 @@ public String[] getPropertyValues(String propertyIdentifier) {
194210 for (PIDRecordEntry e : entry ) {
195211 values .add (e .getValue ());
196212 }
197- return values .toArray (new String [] {});
213+ return values .toArray (new String []{});
198214 }
199215
200216 @ Override
@@ -215,9 +231,15 @@ public int hashCode() {
215231 */
216232 @ Override
217233 public boolean equals (Object obj ) {
218- if (this == obj ) {return true ;}
219- if (obj == null ) {return false ;}
220- if (getClass () != obj .getClass ()) {return false ;}
234+ if (this == obj ) {
235+ return true ;
236+ }
237+ if (obj == null ) {
238+ return false ;
239+ }
240+ if (getClass () != obj .getClass ()) {
241+ return false ;
242+ }
221243
222244 PIDRecord other = (PIDRecord ) obj ;
223245 boolean isThisPidEmpty = pid == null || pid .isBlank ();
@@ -240,13 +262,32 @@ public String toString() {
240262
241263 /**
242264 * Calculates an etag for a record.
243- *
265+ *
244266 * @return an etag, which is independent of any order or duplicates in the
245- * entries.
267+ * entries.
246268 */
247269 @ JsonIgnore
248270 @ Override
249271 public String getEtag () {
250272 return Integer .toString (this .hashCode ());
251273 }
274+
275+ @ Override
276+ public PIDRecord clone () {
277+ try {
278+ PIDRecord clone = (PIDRecord ) super .clone ();
279+ clone .pid = this .pid ;
280+ clone .entries = new HashMap <>();
281+ for (Map .Entry <String , List <PIDRecordEntry >> entry : this .entries .entrySet ()) {
282+ List <PIDRecordEntry > entryList = new ArrayList <>();
283+ for (PIDRecordEntry e : entry .getValue ()) {
284+ entryList .add (e .clone ());
285+ }
286+ clone .entries .put (entry .getKey (), entryList );
287+ }
288+ return clone ;
289+ } catch (CloneNotSupportedException e ) {
290+ throw new AssertionError ();
291+ }
292+ }
252293}
0 commit comments