55*/
66package edu .ie3 .datamodel .utils .validation ;
77
8- import edu .ie3 .datamodel .exceptions .InvalidEntityException ;
9- import edu .ie3 .datamodel .exceptions .InvalidGridException ;
10- import edu .ie3 .datamodel .exceptions .UnsafeEntityException ;
11- import edu .ie3 .datamodel .exceptions .ValidationException ;
8+ import edu .ie3 .datamodel .exceptions .*;
9+ import edu .ie3 .datamodel .models .UniqueEntity ;
1210import edu .ie3 .datamodel .models .input .AssetInput ;
1311import edu .ie3 .datamodel .models .input .MeasurementUnitInput ;
1412import edu .ie3 .datamodel .models .input .NodeInput ;
2422
2523public class GridContainerValidationUtils extends ValidationUtils {
2624
27- private static String duplicateUuidsString (String simpleName , Optional <String > exceptionString ) {
28- return "The provided entities in '"
29- + simpleName
30- + "' contains duplicate UUIDs. "
31- + "This is not allowed!\n Duplicated uuids:\n \n "
32- + exceptionString ;
33- }
34-
3525 /** Private Constructor as this class is not meant to be instantiated */
3626 private GridContainerValidationUtils () {
3727 throw new IllegalStateException ("Don't try and instantiate a Utility class." );
@@ -52,18 +42,10 @@ private GridContainerValidationUtils() {
5242 return List .of (isNull );
5343 }
5444
55- List <Try <Void , ? extends ValidationException >> exceptions = new ArrayList <>();
56-
5745 /* sanity check to ensure distinct UUIDs */
58- Optional <String > exceptionString =
59- checkForDuplicateUuids (new HashSet <>(gridContainer .allEntitiesAsList ()));
60- exceptions .add (
61- Try .ofVoid (
62- exceptionString .isPresent (),
63- () ->
64- new InvalidGridException (
65- duplicateUuidsString (
66- gridContainer .getClass ().getSimpleName (), exceptionString ))));
46+ List <Try <Void , ? extends ValidationException >> exceptions =
47+ new ArrayList <>(
48+ checkForDuplicates (gridContainer .allEntitiesAsList (), UniqueEntity ::getUuid ));
6749
6850 exceptions .addAll (checkRawGridElements (gridContainer .getRawGrid ()));
6951 exceptions .addAll (
@@ -98,18 +80,10 @@ private GridContainerValidationUtils() {
9880 return List .of (isNull );
9981 }
10082
101- List <Try <Void , ? extends ValidationException >> exceptions = new ArrayList <>();
102-
10383 /* sanity check to ensure distinct UUIDs */
104- Optional <String > exceptionString =
105- checkForDuplicateUuids (new HashSet <>(rawGridElements .allEntitiesAsList ()));
106- exceptions .add (
107- Try .ofVoid (
108- exceptionString .isPresent (),
109- () ->
110- new InvalidGridException (
111- duplicateUuidsString (
112- rawGridElements .getClass ().getSimpleName (), exceptionString ))));
84+ List <Try <Void , ? extends ValidationException >> exceptions =
85+ new ArrayList <>(
86+ checkForDuplicates (rawGridElements .allEntitiesAsList (), UniqueEntity ::getUuid ));
11387
11488 /* Checking nodes */
11589 Set <NodeInput > nodes = rawGridElements .getNodes ();
@@ -183,18 +157,18 @@ private GridContainerValidationUtils() {
183157 * Checks the validity of type ids of every entity.
184158 *
185159 * @param rawGridElements the raw grid elements
186- * @return a list of try objects either containing an {@link UnsafeEntityException } or an empty
187- * Success
160+ * @return a list of try objects either containing an {@link DuplicateEntitiesException } or an
161+ * empty Success
188162 */
189- protected static List <Try <Void , UnsafeEntityException >> checkRawGridTypeIds (
163+ protected static List <Try <Void , DuplicateEntitiesException >> checkRawGridTypeIds (
190164 RawGridElements rawGridElements ) {
191- List <Try <Void , UnsafeEntityException >> exceptions = new ArrayList <>();
192- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getNodes ()));
193- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getLines ()));
194- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getTransformer2Ws ()));
195- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getTransformer3Ws ()));
196- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getSwitches ()));
197- exceptions .addAll (ValidationUtils . checkIds (rawGridElements .getMeasurementUnits ()));
165+ List <Try <Void , DuplicateEntitiesException >> exceptions = new ArrayList <>();
166+ exceptions .addAll (checkForDuplicates (rawGridElements .getNodes (), AssetInput :: getId ));
167+ exceptions .addAll (checkForDuplicates (rawGridElements .getLines (), AssetInput :: getId ));
168+ exceptions .addAll (checkForDuplicates (rawGridElements .getTransformer2Ws (), AssetInput :: getId ));
169+ exceptions .addAll (checkForDuplicates (rawGridElements .getTransformer3Ws (), AssetInput :: getId ));
170+ exceptions .addAll (checkForDuplicates (rawGridElements .getSwitches (), AssetInput :: getId ));
171+ exceptions .addAll (checkForDuplicates (rawGridElements .getMeasurementUnits (), AssetInput :: getId ));
198172
199173 return exceptions ;
200174 }
@@ -217,19 +191,10 @@ protected static List<Try<Void, UnsafeEntityException>> checkRawGridTypeIds(
217191 return List .of (isNull );
218192 }
219193
220- List <Try <Void , ? extends ValidationException >> exceptions = new ArrayList <>();
221-
222194 // sanity check for distinct uuids
223- Optional <String > exceptionString =
224- ValidationUtils .checkForDuplicateUuids (
225- new HashSet <>(systemParticipants .allEntitiesAsList ()));
226- exceptions .add (
227- Try .ofVoid (
228- exceptionString .isPresent (),
229- () ->
230- new InvalidGridException (
231- duplicateUuidsString (
232- systemParticipants .getClass ().getSimpleName (), exceptionString ))));
195+ List <Try <Void , ? extends ValidationException >> exceptions =
196+ new ArrayList <>(
197+ checkForDuplicates (systemParticipants .allEntitiesAsList (), UniqueEntity ::getUuid ));
233198
234199 exceptions .addAll (checkSystemParticipants (systemParticipants .getBmPlants (), nodes ));
235200 exceptions .addAll (checkSystemParticipants (systemParticipants .getChpPlants (), nodes ));
@@ -274,23 +239,23 @@ protected static List<Try<Void, UnsafeEntityException>> checkRawGridTypeIds(
274239 * Checks the validity of type ids of every entity.
275240 *
276241 * @param systemParticipants the system participants
277- * @return a list of try objects either containing an {@link UnsafeEntityException } or an empty
278- * Success
242+ * @return a list of try objects either containing an {@link DuplicateEntitiesException } or an
243+ * empty Success
279244 */
280- protected static List <Try <Void , UnsafeEntityException >> checkSystemParticipantsTypeIds (
245+ protected static List <Try <Void , DuplicateEntitiesException >> checkSystemParticipantsTypeIds (
281246 SystemParticipants systemParticipants ) {
282- List <Try <Void , UnsafeEntityException >> exceptions = new ArrayList <>();
283- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getBmPlants ()));
284- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getChpPlants ()));
285- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getEvCS ()));
286- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getEvs ()));
287- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getFixedFeedIns ()));
288- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getHeatPumps ()));
289- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getLoads ()));
290- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getPvPlants ()));
291- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getStorages ()));
292- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getWecPlants ()));
293- exceptions .addAll (ValidationUtils . checkIds (systemParticipants .getEmSystems ()));
247+ List <Try <Void , DuplicateEntitiesException >> exceptions = new ArrayList <>();
248+ exceptions .addAll (checkForDuplicates (systemParticipants .getBmPlants (), AssetInput :: getId ));
249+ exceptions .addAll (checkForDuplicates (systemParticipants .getChpPlants (), AssetInput :: getId ));
250+ exceptions .addAll (checkForDuplicates (systemParticipants .getEvCS (), AssetInput :: getId ));
251+ exceptions .addAll (checkForDuplicates (systemParticipants .getEvs (), AssetInput :: getId ));
252+ exceptions .addAll (checkForDuplicates (systemParticipants .getFixedFeedIns (), AssetInput :: getId ));
253+ exceptions .addAll (checkForDuplicates (systemParticipants .getHeatPumps (), AssetInput :: getId ));
254+ exceptions .addAll (checkForDuplicates (systemParticipants .getLoads (), AssetInput :: getId ));
255+ exceptions .addAll (checkForDuplicates (systemParticipants .getPvPlants (), AssetInput :: getId ));
256+ exceptions .addAll (checkForDuplicates (systemParticipants .getStorages (), AssetInput :: getId ));
257+ exceptions .addAll (checkForDuplicates (systemParticipants .getWecPlants (), AssetInput :: getId ));
258+ exceptions .addAll (checkForDuplicates (systemParticipants .getEmSystems (), AssetInput :: getId ));
294259
295260 return exceptions ;
296261 }
@@ -312,18 +277,10 @@ protected static List<Try<Void, UnsafeEntityException>> checkSystemParticipantsT
312277 return List .of (isNull );
313278 }
314279
315- List <Try <Void , ? extends ValidationException >> exceptions = new ArrayList <>();
316-
317280 // sanity check for distinct uuids
318- Optional <String > exceptionString =
319- checkForDuplicateUuids (new HashSet <>(graphicElements .allEntitiesAsList ()));
320- exceptions .add (
321- Try .ofVoid (
322- exceptionString .isPresent (),
323- () ->
324- new InvalidGridException (
325- duplicateUuidsString (
326- graphicElements .getClass ().getSimpleName (), exceptionString ))));
281+ List <Try <Void , ? extends ValidationException >> exceptions =
282+ new ArrayList <>(
283+ checkForDuplicates (graphicElements .allEntitiesAsList (), UniqueEntity ::getUuid ));
327284
328285 graphicElements
329286 .getNodeGraphics ()
0 commit comments