FINERACT-2232: Generalization of StringEnumOptionData enums

This commit is contained in:
adam.magyari
2025-04-15 19:45:25 +02:00
committed by Adam Saghy
parent 7df7be2b16
commit cb8cbd2043
11 changed files with 87 additions and 95 deletions
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.api;
import java.util.Arrays;
import java.util.List;
import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;
public interface ApiFacingEnum<T extends Enum<T> & ApiFacingEnum<T>> {
static <T extends Enum<T> & ApiFacingEnum<T>> List<StringEnumOptionData> getValuesAsStringEnumOptionDataList(Class<T> clazz) {
return Arrays.stream(clazz.getEnumConstants()).map(v -> new StringEnumOptionData(v.name(), v.getCode(), v.getHumanReadableName()))
.toList();
}
static <T extends Enum<T> & ApiFacingEnum<T>> StringEnumOptionData getStringEnumOptionData(Class<T> clazz, String name) {
return name == null || name.trim().isEmpty() ? null : Enum.valueOf(clazz, name).getValueAsStringEnumOptionData();
}
default StringEnumOptionData getValueAsStringEnumOptionData() {
return new StringEnumOptionData(((Enum<T>) this).name(), getCode(), getHumanReadableName());
}
String getCode();
String getHumanReadableName();
}
@@ -18,11 +18,9 @@
*/
package org.apache.fineract.portfolio.common.domain;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;
import org.apache.fineract.infrastructure.core.api.ApiFacingEnum;
/**
* Defines the method of leap year calculation for determining the number of days in a year.
@@ -53,7 +51,7 @@ import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;
*/
@Getter
@RequiredArgsConstructor
public enum DaysInYearCustomStrategyType {
public enum DaysInYearCustomStrategyType implements ApiFacingEnum<DaysInYearCustomStrategyType> {
/** Always considers 366 days in a leap year. */
FULL_LEAP_YEAR("DaysInYearCustomStrategyType.fullLeapYear", "Full Leap Year"),
@@ -64,15 +62,4 @@ public enum DaysInYearCustomStrategyType {
private final String code;
private final String humanReadableName;
public static List<StringEnumOptionData> getValuesAsStringEnumOptionDataList() {
return Arrays.stream(values()).map(v -> new StringEnumOptionData(v.name(), v.getCode(), v.getHumanReadableName())).toList();
}
public StringEnumOptionData getValueAsStringEnumOptionData() {
return new StringEnumOptionData(name(), getCode(), getHumanReadableName());
}
public static StringEnumOptionData getStringEnumOptionData(String name) {
return name == null || name.trim().isEmpty() ? null : DaysInYearCustomStrategyType.valueOf(name).getValueAsStringEnumOptionData();
}
}