Refactor BlogProperties

pull/137/head
johnniang 2019-03-26 23:00:31 +08:00
parent 7052c3ccf7
commit e042d83baf
5 changed files with 121 additions and 100 deletions

View File

@ -1,13 +1,10 @@
package cc.ryanc.halo.model.enums;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
/**
* @author : RYAN0UP
* @date : 2019-03-17
*/
public enum BlogProperties implements ValueEnum<String> {
public enum BlogProperties implements PropertyEnum<String> {
/**
*
@ -165,7 +162,7 @@ public enum BlogProperties implements ValueEnum<String> {
private Class<?> type;
BlogProperties(String value, Class<?> type) {
if (!isSupportedType(type)) {
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -183,80 +180,9 @@ public enum BlogProperties implements ValueEnum<String> {
return value;
}
@Override
public Class<?> getType() {
return type;
}
/**
* Converts to value with corresponding type
*
* @param value string value must not be null
* @param type property value type must not be null
* @param <T> property value type
* @return property value
*/
@SuppressWarnings("unchecked")
public static <T> T convertTo(@NonNull String value, @NonNull Class<T> type) {
Assert.hasText(value, "Property value must not be blank");
if (!isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
if (type.isAssignableFrom(String.class)) {
return (T) value;
}
if (type.isAssignableFrom(Integer.class)) {
return (T) Integer.valueOf(value);
}
if (type.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(value);
}
if (type.isAssignableFrom(Boolean.class)) {
return (T) Boolean.valueOf(value);
}
if (type.isAssignableFrom(Short.class)) {
return (T) Short.valueOf(value);
}
if (type.isAssignableFrom(Byte.class)) {
return (T) Byte.valueOf(value);
}
if (type.isAssignableFrom(Double.class)) {
return (T) Byte.valueOf(value);
}
if (type.isAssignableFrom(Float.class)) {
return (T) Float.valueOf(value);
}
// Should never happen
throw new UnsupportedOperationException("Unsupported blog property type:" + type.getName() + " provided");
}
/**
* Check the type is support by the blog property.
*
* @param type type to check
* @return true if supports; false else
*/
public static boolean isSupportedType(Class<?> type) {
return type != null && (
type.isAssignableFrom(String.class)
|| type.isAssignableFrom(Number.class)
|| type.isAssignableFrom(Integer.class)
|| type.isAssignableFrom(Long.class)
|| type.isAssignableFrom(Boolean.class)
|| type.isAssignableFrom(Short.class)
|| type.isAssignableFrom(Byte.class)
|| type.isAssignableFrom(Double.class)
|| type.isAssignableFrom(Float.class)
);
}
}

View File

@ -0,0 +1,92 @@
package cc.ryanc.halo.model.enums;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
/**
* Property enum.
*
* @author johnniang
* @date 3/26/19
*/
public interface PropertyEnum<T> extends ValueEnum<T> {
/**
* Get property type.
*
* @return property type
*/
Class<?> getType();
/**
* Converts to value with corresponding type
*
* @param value string value must not be null
* @param type property value type must not be null
* @param <T> property value type
* @return property value
*/
@SuppressWarnings("unchecked")
static <T> T convertTo(@NonNull String value, @NonNull Class<T> type) {
Assert.hasText(value, "Property value must not be blank");
if (!isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
if (type.isAssignableFrom(String.class)) {
return (T) value;
}
if (type.isAssignableFrom(Integer.class)) {
return (T) Integer.valueOf(value);
}
if (type.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(value);
}
if (type.isAssignableFrom(Boolean.class)) {
return (T) Boolean.valueOf(value);
}
if (type.isAssignableFrom(Short.class)) {
return (T) Short.valueOf(value);
}
if (type.isAssignableFrom(Byte.class)) {
return (T) Byte.valueOf(value);
}
if (type.isAssignableFrom(Double.class)) {
return (T) Byte.valueOf(value);
}
if (type.isAssignableFrom(Float.class)) {
return (T) Float.valueOf(value);
}
// Should never happen
throw new UnsupportedOperationException("Unsupported blog property type:" + type.getName() + " provided");
}
/**
* Check the type is support by the blog property.
*
* @param type type to check
* @return true if supports; false else
*/
static boolean isSupportedType(Class<?> type) {
return type != null && (
type.isAssignableFrom(String.class)
|| type.isAssignableFrom(Number.class)
|| type.isAssignableFrom(Integer.class)
|| type.isAssignableFrom(Long.class)
|| type.isAssignableFrom(Boolean.class)
|| type.isAssignableFrom(Short.class)
|| type.isAssignableFrom(Byte.class)
|| type.isAssignableFrom(Double.class)
|| type.isAssignableFrom(Float.class)
);
}
}

View File

@ -12,6 +12,13 @@ import java.util.stream.Stream;
*/
public interface ValueEnum<T> {
/**
* Gets enum value.
*
* @return enum value
*/
T getValue();
/**
* Converts value to corresponding enum.
*
@ -32,10 +39,4 @@ public interface ValueEnum<T> {
.orElseThrow(() -> new IllegalArgumentException("unknown database value: " + value));
}
/**
* Gets enum value.
*
* @return enum value
*/
T getValue();
}

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.exception.MissingPropertyValueException;
import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PropertyEnum;
import cc.ryanc.halo.model.params.OptionParam;
import cc.ryanc.halo.service.base.CrudService;
import com.qiniu.common.Zone;
@ -59,7 +60,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param properties blog properties
* @param source source
*/
void saveProperties(@NonNull Map<BlogProperties, String> properties, String source);
void saveProperties(@NonNull Map<? extends PropertyEnum<String>, String> properties, String source);
/**
* Get all options
@ -109,7 +110,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return an option value
*/
@Nullable
String getByPropertyOfNullable(@NonNull BlogProperties property);
String getByPropertyOfNullable(@NonNull PropertyEnum<String> property);
/**
* Gets option value by blog property.
@ -119,7 +120,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @throws MissingPropertyValueException throws when property value dismisses
*/
@NonNull
String getByPropertyOfNonNull(@NonNull BlogProperties property);
String getByPropertyOfNonNull(@NonNull PropertyEnum<String> property);
/**
* Gets option value by blog property.
@ -128,7 +129,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return an optional option value
*/
@NonNull
Optional<String> getByProperty(@NonNull BlogProperties property);
Optional<String> getByProperty(@NonNull PropertyEnum<String> property);
/**
* Gets property value by blog property.
@ -139,7 +140,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param <T> property type
* @return property value
*/
<T> T getByProperty(@NonNull BlogProperties property, @NonNull Class<T> propertyType, T defaultValue);
<T> T getByProperty(@NonNull PropertyEnum<String> property, @NonNull Class<T> propertyType, T defaultValue);
/**
* Gets property value by blog property.
@ -149,7 +150,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param <T> property type
* @return property value
*/
<T> Optional<T> getByProperty(@NonNull BlogProperties property, @NonNull Class<T> propertyType);
<T> Optional<T> getByProperty(@NonNull PropertyEnum<String> property, @NonNull Class<T> propertyType);
/**
* Gets value by key.

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.exception.MissingPropertyValueException;
import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PropertyEnum;
import cc.ryanc.halo.model.params.OptionParam;
import cc.ryanc.halo.repository.OptionRepository;
import cc.ryanc.halo.service.OptionService;
@ -22,8 +23,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static cc.ryanc.halo.model.enums.BlogProperties.convertTo;
/**
* OptionService implementation class
*
@ -103,12 +102,14 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
}
@Override
public void saveProperties(Map<BlogProperties, String> properties, String source) {
public void saveProperties(Map<? extends PropertyEnum<String>, String> properties, String source) {
if (CollectionUtils.isEmpty(properties)) {
return;
}
properties.forEach((property, value) -> save(property.getValue(), value, source));
properties.forEach((property, value) -> {
save(property.getValue(), value, source);
});
}
/**
@ -150,34 +151,34 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
}
@Override
public String getByPropertyOfNullable(BlogProperties property) {
public String getByPropertyOfNullable(PropertyEnum<String> property) {
return getByProperty(property).orElse(null);
}
@Override
public String getByPropertyOfNonNull(BlogProperties property) {
public String getByPropertyOfNonNull(PropertyEnum<String> property) {
Assert.notNull(property, "Blog property must not be null");
return getByKeyOfNonNull(property.getValue());
}
@Override
public Optional<String> getByProperty(BlogProperties property) {
public Optional<String> getByProperty(PropertyEnum<String> property) {
Assert.notNull(property, "Blog property must not be null");
return getByKey(property.getValue());
}
@Override
public <T> T getByProperty(BlogProperties property, Class<T> propertyType, T defaultValue) {
public <T> T getByProperty(PropertyEnum<String> property, Class<T> propertyType, T defaultValue) {
Assert.notNull(property, "Blog property must not be null");
return getByProperty(property, propertyType).orElse(defaultValue);
}
@Override
public <T> Optional<T> getByProperty(BlogProperties property, Class<T> propertyType) {
return getByProperty(property).map(propertyValue -> convertTo(propertyValue, propertyType));
public <T> Optional<T> getByProperty(PropertyEnum<String> property, Class<T> propertyType) {
return getByProperty(property).map(propertyValue -> PropertyEnum.convertTo(propertyValue, propertyType));
}
@Override
@ -187,7 +188,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public <T> Optional<T> getByKey(String key, Class<T> valueType) {
return getByKey(key).map(value -> convertTo(value, valueType));
return getByKey(key).map(value -> PropertyEnum.convertTo(value, valueType));
}
@Override