Skip to content
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
58ca672
Improving.
staudtMarius Mar 6, 2023
438e3b4
Improving ``ValidationUtils``.
staudtMarius Mar 8, 2023
16dc44c
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Mar 9, 2023
86c91ac
Merge branch 'ms/#707-Refactor-data-sources-to-throw-exceptions-inste…
staudtMarius Mar 9, 2023
db202f5
Updating CHANGELOG.
staudtMarius Mar 9, 2023
c145bbd
Improving `ValidationUtils`.
staudtMarius Mar 10, 2023
1ae4c04
Merge branch 'ms/#707-Refactor-data-sources-to-throw-exceptions-inste…
staudtMarius Mar 11, 2023
b655ebe
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Mar 13, 2023
94f5da1
Adapting tests.
staudtMarius Mar 13, 2023
9e68d90
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Apr 11, 2023
3cd33c9
Adding some new features.
staudtMarius Apr 11, 2023
f8667c3
Adding tests.
staudtMarius Apr 12, 2023
5520c48
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Apr 18, 2023
54ecf77
fmt
staudtMarius Apr 18, 2023
9a5c7a6
Improving ``GridContainerValidationUtils``
staudtMarius Apr 18, 2023
3806ab2
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Apr 20, 2023
fe7ea69
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Apr 24, 2023
fb294de
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Apr 24, 2023
e95c356
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 2, 2023
81d87ba
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 4, 2023
6720d02
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 9, 2023
ca16f51
fmt
staudtMarius May 9, 2023
edb56c7
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 15, 2023
2c3e49f
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 16, 2023
b7e8eb6
Adapting to changes.
staudtMarius May 16, 2023
6e596fe
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 22, 2023
6f5ded4
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 25, 2023
210425a
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius May 30, 2023
cbb75ee
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Jun 7, 2023
b8aead1
Merge branch 'ms/#707-Refactor-data-sources-to-throw-exceptions-inste…
staudtMarius Jun 29, 2023
3dc162b
Adapting to changes.
staudtMarius Jun 29, 2023
1ae94b8
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Jul 19, 2023
2a8e897
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Jul 26, 2023
863b5fe
Adapting ``ValidationUtils`` to the recent changes in ``Try``.
staudtMarius Jul 26, 2023
403e326
Fixing some ``sonarqube`` issues.
staudtMarius Jul 27, 2023
d6bab5b
Fixing failing test.
staudtMarius Jul 27, 2023
fde8699
Fixing ``sonatype`` isuue.
staudtMarius Jul 27, 2023
8e3365f
Fixing ``codacy`` isuue.
staudtMarius Jul 27, 2023
d574534
Fixing ``sonarqube`` isuue.
staudtMarius Jul 27, 2023
32bc1bd
Some improvements.
staudtMarius Jul 27, 2023
0df2058
Merge branch 'dev' into ms/#758-improving-ValidationUtils
staudtMarius Jul 28, 2023
0f7ee8f
Some improvements.
staudtMarius Jul 28, 2023
807ac08
Some improvements.
staudtMarius Jul 28, 2023
aeb98fd
Merge remote-tracking branch 'origin/ms/#758-improving-ValidationUtil…
staudtMarius Jul 28, 2023
363927c
Fixing failing test.
staudtMarius Jul 28, 2023
673e9d3
Fixing ``codacy`` issue.
staudtMarius Jul 28, 2023
ecd43a9
Suggestion: Optimized structure of Try
sebastian-peter Jul 28, 2023
ce7d148
Fixing CsvRawGridSourceTest
sebastian-peter Jul 28, 2023
a2e0063
Fixing code smell
sebastian-peter Jul 28, 2023
b52d845
Fixing codacy issue
sebastian-peter Jul 30, 2023
06b4066
Minor changes.
staudtMarius Jul 30, 2023
7014bb6
Renaming childInstance() to the more fitting thisInstance()
sebastian-peter Jul 31, 2023
fd688fb
Fixing code smell
sebastian-peter Jul 31, 2023
26c9aab
Renaming remaining childInstance() methods
sebastian-peter Jul 31, 2023
0f8a390
Further simplifications
sebastian-peter Jul 31, 2023
7d62119
Adding some @Override notations and abstract method definitions
sebastian-peter Jul 31, 2023
ac7564a
Reinstated removed method
sebastian-peter Jul 31, 2023
b2df229
Improved formatting of failure messages
sebastian-peter Jul 31, 2023
47d307b
Fixing test
sebastian-peter Jul 31, 2023
0767e27
Fixing ValidationUtilsTest
sebastian-peter Jul 31, 2023
e80502b
Fixing TryTest
sebastian-peter Jul 31, 2023
a66754e
Codacy issues
sebastian-peter Jul 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Deleted parameter initFiles, set parameter append to false by default [#791](https://github.com/ie3-institute/PowerSystemDataModel/issues/791)
- Use nio paths instead of strings for file path [#723](https://github.com/ie3-institute/PowerSystemDataModel/issues/723)
- Data source will throw an exceptions instead of returning an empty optionals [#707](https://github.com/ie3-institute/PowerSystemDataModel/issues/707)
- Improving `ValidationUtils` [#758](https://github.com/ie3-institute/PowerSystemDataModel/issues/758)


## [3.0.0] - 2023-02-16
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.exceptions;

import edu.ie3.datamodel.utils.ExceptionUtils;
import java.util.List;

public class FailedValidationException extends ValidationException {
public FailedValidationException(String message, Throwable throwable) {
super(message, throwable);
}

public FailedValidationException(String message) {
super(message);
}

public FailedValidationException(List<? extends Exception> exceptions) {
super("Validation failed due to: " + ExceptionUtils.getMessages(exceptions));
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/ie3/datamodel/utils/ExceptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private ExceptionUtils() {
}

/**
* Creates a string containing multiple exception messsages.
* Creates a string containing multiple exception messages.
*
* @param exceptions list of exceptions
* @return str containing the messages
Expand Down
170 changes: 109 additions & 61 deletions src/main/java/edu/ie3/datamodel/utils/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.stream.Stream;

public abstract class Try<T, E extends Exception> {
// fields
private final T data;
private final E exception;
private final boolean isEmpty;
Expand Down Expand Up @@ -42,6 +43,8 @@ private Try(E ex) {
isEmpty = true;
}

// static utility methods

/**
* Method to create a {@link Try} object easily.
*
Expand Down Expand Up @@ -75,7 +78,7 @@ public static <T, E extends Exception> Try<T, E> of(TrySupplier<T, E> supplier,
*/
@SuppressWarnings("unchecked")
public static <E extends Exception> Try<Void, E> ofVoid(
TrySupplier<?, E> supplier, Class<E> clazz) {
VoidSupplier<E> supplier, Class<E> clazz) {
try {
supplier.get();
return Success.empty();
Expand All @@ -89,6 +92,96 @@ public static <E extends Exception> Try<Void, E> ofVoid(
}
}

/**
* Method to create a {@link Try} object easily.
*
* @param failure a {@link Failure} is returned.
* @param exception exception that should be wrapped by a {@link Failure}
* @return a {@link Try}
* @param <E> type of exception
*/
public static <E extends Exception> Try<Void, E> ofVoid(boolean failure, E exception) {
if (failure) {
return Failure.ofVoid(exception);
} else {
return Success.empty();
}
}

/**
* Utility method to check a list of {@link VoidSupplier}'s.
*
* @param supplier list of {@link VoidSupplier}
* @param clazz class of the exception
* @return a list of {@link Try}
* @param <E> type of the exception
*/
@SafeVarargs
public static <E extends Exception> List<Try<Void, E>> ofVoid(
Class<E> clazz, VoidSupplier<E>... supplier) {
return Arrays.stream(supplier).map(sup -> Try.ofVoid(sup, clazz)).toList();
}

/**
* Method to retrieve the exceptions from all {@link Failure} objects.
*
* @param tries collection of {@link Try} objects
* @return a list of {@link Exception}'s
*/
public static <D, E extends Exception> List<E> getExceptions(
Collection<Try<? extends D, E>> tries) {
return tries.stream().filter(Try::isFailure).map(t -> ((Failure<?, E>) t).get()).toList();
}

/**
* Method to scan a collection of {@link Try} objects for {@link Failure}'s.
*
* @param c collection of {@link Try} objects
* @param typeOfData type of data
* @return a {@link Success} if no {@link Failure}'s are found in the collection
* @param <U> type of data
*/
public static <U, E extends Exception> Try<Set<U>, FailureException> scanCollection(
Collection<Try<U, E>> c, Class<U> typeOfData) {
return scanStream(c.stream(), typeOfData.getSimpleName())
.transformS(stream -> stream.collect(Collectors.toSet()));
}

/**
* Method to scan a stream of {@link Try} objects for {@link Failure}'s.
*
* @param stream of {@link Try} objects
* @return a {@link Success} if no {@link Failure}'s are found in the stream
* @param <U> type of data
*/
public static <U, E extends Exception> Try<Stream<U>, FailureException> scanStream(
Stream<Try<U, E>> stream, String typeOfData) {
Map<Boolean, List<Try<U, E>>> map = stream.collect(partitioningBy(Try::isSuccess));

List<Try<U, E>> successes = map.get(true);
List<Try<U, E>> failures = map.get(false);

// Both lists should exist in map per definition of partitioningBy
assert successes != null && failures != null;

if (!failures.isEmpty()) {
E first = failures.get(0).exception;

return new Failure<>(
new FailureException(
failures.size()
+ " exception(s) occurred within \""
+ typeOfData
+ "\" data, one is: "
+ first,
first.getCause()));
} else {
return new Success<>(successes.stream().map(t -> t.data));
}
}

// methods of try object

/**
* Returns true if this object is a {@link Success} or false if this object is a {@link Failure}.
*/
Expand All @@ -111,7 +204,7 @@ public boolean isEmpty() {
* @throws E if this object is a {@link Failure}
*/
public T getOrThrow() throws E {
if (data != null) {
if (isSuccess()) {
return data;
} else {
assert exception != null;
Expand Down Expand Up @@ -223,66 +316,10 @@ public <U, R extends Exception> Try<U, R> transform(
}
}

/**
* Method to scan a collection of {@link Try} objects for {@link Failure}'s.
*
* @param c collection of {@link Try} objects
* @param typeOfData type of data
* @return a {@link Success} if no {@link Failure}'s are found in the collection
* @param <U> type of data
*/
public static <U, E extends Exception> Try<Set<U>, FailureException> scanCollection(
Collection<Try<U, E>> c, Class<U> typeOfData) {
return scanStream(c.stream(), typeOfData.getSimpleName())
.transformS(stream -> stream.collect(Collectors.toSet()));
}

/**
* Method to scan a stream of {@link Try} objects for {@link Failure}'s.
*
* @param stream of {@link Try} objects
* @return a {@link Success} if no {@link Failure}'s are found in the stream
* @param <U> type of data
*/
public static <U, E extends Exception> Try<Stream<U>, FailureException> scanStream(
Stream<Try<U, E>> stream, String typeOfData) {
Map<Boolean, List<Try<U, E>>> map = stream.collect(partitioningBy(Try::isSuccess));

List<Try<U, E>> successes = map.get(true);
List<Try<U, E>> failures = map.get(false);

// Both lists should exist in map per definition of partitioningBy
assert successes != null && failures != null;

if (!failures.isEmpty()) {
E first = failures.get(0).exception;

return new Failure<>(
new FailureException(
failures.size()
+ " exception(s) occurred within \""
+ typeOfData
+ "\" data, one is: "
+ first,
first.getCause()));
} else {
return new Success<>(successes.stream().map(t -> t.data));
}
}

/**
* Method to retrieve the exceptions from all {@link Failure} objects.
*
* @param tries collection of {@link Try} objects
* @return a list of {@link Exception}'s
*/
public static <D, E extends Exception> List<E> getExceptions(
Collection<Try<? extends D, E>> tries) {
return tries.stream().filter(Try::isFailure).map(t -> ((Failure<?, E>) t).get()).toList();
}

/** Implementation of {@link Try} class. This class is used to present a successful try. */
public static final class Success<T, E extends Exception> extends Try<T, E> {
private static final Success<Void, ?> emptySuccess = new Success<>(null);

public Success(T data) {
super(data);
}
Expand All @@ -307,8 +344,9 @@ public T get() {
*
* @param <E> type of exception
*/
@SuppressWarnings("unchecked")
public static <E extends Exception> Success<Void, E> empty() {
return new Success<>(null);
return (Success<Void, E>) emptySuccess;
}
}

Expand Down Expand Up @@ -379,4 +417,14 @@ public static <T, U, E extends Exception> Failure<U, E> of(Failure<T, E> failure
public interface TrySupplier<T, E extends Exception> {
T get() throws E;
}

/**
* Supplier for void methods to {@link Try} class.
*
* @param <E> type of exception that could be thrown
*/
@FunctionalInterface
public interface VoidSupplier<E extends Exception> {
void get() throws E;
}
}
Loading