Skip to content

Commit

Permalink
Merge pull request #78 from vincejv/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
vincejv authored Oct 23, 2022
2 parents 3eeefc6 + e811826 commit e2a46e7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.abavilla.fpi.fw.entity.enums;

import java.util.Map;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

/**
* Base implementation of {@link Enum} types
Expand All @@ -26,4 +30,53 @@
@RegisterForReflection
public interface IBaseEnum {

/**
* Creates an enum based from given string value, returns the default enum value if value does not map
*
* @param value the string value
* @param enumMap id to enum mapping
* @param unknownVal default enum value
* @return the created enum
*/
static IBaseEnum fromValue(String value, Map<Integer, IBaseEnum> enumMap, IBaseEnum unknownVal) {
if (StringUtils.isBlank(value)) {
return null;
} else {
return enumMap.values().stream().filter(enumItem ->
StringUtils.equalsIgnoreCase(value, enumItem.getValue())).findAny()
.orElse(unknownVal);
}
}

/**
* Creates an enum based from given an ordinal id, returns the default enum value if id does not map
*
* @param id the ordinal id
* @param enumMap id to enum mapping
* @param unknownVal default enum value
* @return the created enum
*/
static IBaseEnum fromId(int id, Map<Integer, IBaseEnum> enumMap, IBaseEnum unknownVal) {
return enumMap.values().stream().filter(enumItem -> id == enumItem.getId()).findAny()
.orElse(unknownVal);
}

/**
* Gets the value
*
* @return the value
*/
default String getValue() {
throw new NotImplementedException();
}

/**
* Gets the id
*
* @return the id
*/
default int getId() {
throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
* Template base {@link Enum} for creating enums from, since {@link Enum} do not
Expand All @@ -48,7 +47,7 @@ public enum SampleEnum implements IBaseEnum {
/**
* Ordinal id to enum mapping
*/
private static final Map<Integer, SampleEnum> ENUM_MAP = new HashMap<>();
private static final Map<Integer, IBaseEnum> ENUM_MAP = new HashMap<>();

static {
for(SampleEnum w : EnumSet.allOf(SampleEnum.class))
Expand All @@ -63,7 +62,7 @@ public enum SampleEnum implements IBaseEnum {
/**
* The enum value
*/
private String value;
private final String value;

/**
* Creates an enum based from given string value
Expand All @@ -72,21 +71,8 @@ public enum SampleEnum implements IBaseEnum {
* @return the created enum
*/
@JsonCreator
public static SampleEnum fromValue(String value) {
if (StringUtils.isBlank(value)) {
return null;
} else {
return ENUM_MAP.values().stream().filter(enumItem -> StringUtils.equalsIgnoreCase(value, enumItem.getValue())).findAny()
.orElseGet(() -> {
var unknown = UNKNOWN;
String enumValue = value;
if (StringUtils.startsWithIgnoreCase(enumValue, UNKNOWN_PREFIX)) {
enumValue = StringUtils.removeStart(enumValue, UNKNOWN_PREFIX);
}
unknown.value = UNKNOWN_PREFIX + enumValue;
return unknown;
});
}
public static IBaseEnum fromValue(String value) {
return IBaseEnum.fromValue(value, ENUM_MAP, UNKNOWN);
}

/**
Expand All @@ -95,13 +81,8 @@ public static SampleEnum fromValue(String value) {
* @param id the ordinal id
* @return the created enum
*/
public static SampleEnum fromId(int id) {
return ENUM_MAP.values().stream().filter(enumItem -> id == enumItem.getId()).findAny()
.orElseGet(() -> {
var unknown = UNKNOWN;
unknown.value = UNKNOWN_PREFIX + id;
return unknown;
});
public static IBaseEnum fromId(int id) {
return IBaseEnum.fromId(id, ENUM_MAP, UNKNOWN);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public final class FWConst {
/**
* Prefix for Unknown enum values
*/
public static final String UNKNOWN_PREFIX = "UNK >> ";
public static final String UNKNOWN_PREFIX = "Unknown";

}
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true

0 comments on commit e2a46e7

Please sign in to comment.