Skip to content

Commit

Permalink
Load profile parsing without casting
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-peter committed Jun 3, 2022
1 parent 0dab0d8 commit 382d917
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/**
* German standard electricity load profiles, defined by the bdew (Bundesverband der Energie- und
* Wasserwirtschaft; engl.Federal Association of the Energy and Water Industry). For more details
Expand Down Expand Up @@ -36,8 +38,8 @@ public enum BdewStandardLoadProfile implements StandardLoadProfile {
* @return The corresponding bdew load profile or throw {@link IllegalArgumentException}, if no
* matching load profile can be found
*/
public static BdewStandardLoadProfile get(String key) {
return (BdewStandardLoadProfile) LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
public static BdewStandardLoadProfile get(String key) throws ParsingException {
return LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
}

@Override
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/edu/ie3/datamodel/models/profile/LoadProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface LoadProfile extends Serializable {
/** @return The identifying String */
Expand All @@ -25,15 +24,16 @@ public interface LoadProfile extends Serializable {
static LoadProfile parse(String key) throws ParsingException {
if (key == null || key.isEmpty()) return LoadProfile.DefaultLoadProfiles.NO_LOAD_PROFILE;

String filterKey = getUniformKey(key);
return Stream.concat(
Arrays.stream(BdewStandardLoadProfile.values()),
Arrays.stream(NbwTemperatureDependantLoadProfile.values()))
.filter(profile -> profile.getKey().equals(filterKey))
.findFirst()
.orElseThrow(
() ->
new ParsingException("Cannot parse \"" + key + "\" to a valid known load profile"));
return LoadProfile.getProfile(getAllProfiles(), key);
}

static LoadProfile[] getAllProfiles() {
final LoadProfile[][] all =
new LoadProfile[][] {
BdewStandardLoadProfile.values(), NbwTemperatureDependantLoadProfile.values()
};

return Arrays.stream(all).flatMap(Arrays::stream).toArray(LoadProfile[]::new);
}

/**
Expand All @@ -43,16 +43,16 @@ static LoadProfile parse(String key) throws ParsingException {
* @param key to look for
* @return the matching load profile
*/
static LoadProfile getProfile(LoadProfile[] profiles, String key) {
static <T extends LoadProfile> T getProfile(T[] profiles, String key) throws ParsingException {
return Arrays.stream(profiles)
.filter(loadProfile -> loadProfile.getKey().equalsIgnoreCase(getUniformKey(key)))
.findFirst()
.orElseThrow(
() ->
new IllegalArgumentException(
new ParsingException(
"No predefined load profile with key '"
+ key
+ "' found. Please provide one of the following keys:"
+ "' found. Please provide one of the following keys: "
+ Arrays.stream(profiles)
.map(LoadProfile::getKey)
.collect(Collectors.joining(", "))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/** Temperature dependant determined by NBW (accessed 05/2022) */
public enum NbwTemperatureDependantLoadProfile implements TemperatureDependantLoadProfile {
// heat pumps
Expand All @@ -26,9 +28,8 @@ public enum NbwTemperatureDependantLoadProfile implements TemperatureDependantLo
* @return The corresponding nbw load profile or throw {@link IllegalArgumentException}, if no
* matching load profile can be found
*/
public static NbwTemperatureDependantLoadProfile get(String key) {
return (NbwTemperatureDependantLoadProfile)
LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
public static NbwTemperatureDependantLoadProfile get(String key) throws ParsingException {
return LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.repetitive.RepetitiveTimeSeries;

Expand All @@ -24,7 +25,7 @@ public interface StandardLoadProfile extends LoadProfile {
* @param key to look for
* @return the matching standard load profile
*/
static StandardLoadProfile parse(String key) {
return (StandardLoadProfile) LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
static StandardLoadProfile parse(String key) throws ParsingException {
return LoadProfile.getProfile(BdewStandardLoadProfile.values(), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.models.profile;

import edu.ie3.datamodel.exceptions.ParsingException;

/**
* Temperature dependant load profiles for night storage heating and heat pumps . The profiles rely
* on the VDN description for interruptable loads. For more details see <a
Expand All @@ -18,8 +20,7 @@ public interface TemperatureDependantLoadProfile extends LoadProfile {
* @param key to look for
* @return the matching temperature dependant load profile
*/
static TemperatureDependantLoadProfile parse(String key) {
return (NbwTemperatureDependantLoadProfile)
LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
static TemperatureDependantLoadProfile parse(String key) throws ParsingException {
return LoadProfile.getProfile(NbwTemperatureDependantLoadProfile.values(), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.models
package edu.ie3.datamodel.models.profile

import edu.ie3.datamodel.exceptions.ParsingException
import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile
import edu.ie3.datamodel.models.profile.LoadProfile
import edu.ie3.datamodel.models.profile.NbwTemperatureDependantLoadProfile
import edu.ie3.datamodel.models.profile.StandardLoadProfile
import edu.ie3.datamodel.models.profile.TemperatureDependantLoadProfile
import spock.lang.Specification

class LoadProfileTest extends Specification {
Expand Down Expand Up @@ -189,6 +184,6 @@ class LoadProfileTest extends Specification {

then:
def e = thrown(ParsingException)
e.message == "Cannot parse \"not_a_key\" to a valid known load profile"
e.message == "No predefined load profile with key 'not_a_key' found. Please provide one of the following keys: h0, l0, l1, l2, g0, g1, g2, g3, g4, g5, g6, ep1, ez2"
}
}

0 comments on commit 382d917

Please sign in to comment.