mirror of https://github.com/halo-dev/halo
Refactor BlogProperties
parent
7052c3ccf7
commit
e042d83baf
|
@ -1,13 +1,10 @@
|
||||||
package cc.ryanc.halo.model.enums;
|
package cc.ryanc.halo.model.enums;
|
||||||
|
|
||||||
import org.springframework.lang.NonNull;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : RYAN0UP
|
* @author : RYAN0UP
|
||||||
* @date : 2019-03-17
|
* @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;
|
private Class<?> type;
|
||||||
|
|
||||||
BlogProperties(String value, Class<?> type) {
|
BlogProperties(String value, Class<?> type) {
|
||||||
if (!isSupportedType(type)) {
|
if (!PropertyEnum.isSupportedType(type)) {
|
||||||
throw new IllegalArgumentException("Unsupported blog property type: " + type);
|
throw new IllegalArgumentException("Unsupported blog property type: " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,80 +180,9 @@ public enum BlogProperties implements ValueEnum<String> {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Class<?> getType() {
|
public Class<?> getType() {
|
||||||
return type;
|
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,13 @@ import java.util.stream.Stream;
|
||||||
*/
|
*/
|
||||||
public interface ValueEnum<T> {
|
public interface ValueEnum<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets enum value.
|
||||||
|
*
|
||||||
|
* @return enum value
|
||||||
|
*/
|
||||||
|
T getValue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts value to corresponding enum.
|
* Converts value to corresponding enum.
|
||||||
*
|
*
|
||||||
|
@ -32,10 +39,4 @@ public interface ValueEnum<T> {
|
||||||
.orElseThrow(() -> new IllegalArgumentException("unknown database value: " + value));
|
.orElseThrow(() -> new IllegalArgumentException("unknown database value: " + value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets enum value.
|
|
||||||
*
|
|
||||||
* @return enum value
|
|
||||||
*/
|
|
||||||
T getValue();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.exception.MissingPropertyValueException;
|
||||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Option;
|
import cc.ryanc.halo.model.entity.Option;
|
||||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
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.model.params.OptionParam;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
import com.qiniu.common.Zone;
|
import com.qiniu.common.Zone;
|
||||||
|
@ -59,7 +60,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @param properties blog properties
|
* @param properties blog properties
|
||||||
* @param source source
|
* @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
|
* Get all options
|
||||||
|
@ -109,7 +110,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @return an option value
|
* @return an option value
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
String getByPropertyOfNullable(@NonNull BlogProperties property);
|
String getByPropertyOfNullable(@NonNull PropertyEnum<String> property);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets option value by blog 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
|
* @throws MissingPropertyValueException throws when property value dismisses
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
String getByPropertyOfNonNull(@NonNull BlogProperties property);
|
String getByPropertyOfNonNull(@NonNull PropertyEnum<String> property);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets option value by blog property.
|
* Gets option value by blog property.
|
||||||
|
@ -128,7 +129,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @return an optional option value
|
* @return an optional option value
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
Optional<String> getByProperty(@NonNull BlogProperties property);
|
Optional<String> getByProperty(@NonNull PropertyEnum<String> property);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets property value by blog property.
|
* Gets property value by blog property.
|
||||||
|
@ -139,7 +140,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @param <T> property type
|
* @param <T> property type
|
||||||
* @return property value
|
* @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.
|
* Gets property value by blog property.
|
||||||
|
@ -149,7 +150,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @param <T> property type
|
* @param <T> property type
|
||||||
* @return property value
|
* @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.
|
* Gets value by key.
|
||||||
|
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.exception.MissingPropertyValueException;
|
||||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Option;
|
import cc.ryanc.halo.model.entity.Option;
|
||||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
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.model.params.OptionParam;
|
||||||
import cc.ryanc.halo.repository.OptionRepository;
|
import cc.ryanc.halo.repository.OptionRepository;
|
||||||
import cc.ryanc.halo.service.OptionService;
|
import cc.ryanc.halo.service.OptionService;
|
||||||
|
@ -22,8 +23,6 @@ import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static cc.ryanc.halo.model.enums.BlogProperties.convertTo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OptionService implementation class
|
* OptionService implementation class
|
||||||
*
|
*
|
||||||
|
@ -103,12 +102,14 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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)) {
|
if (CollectionUtils.isEmpty(properties)) {
|
||||||
return;
|
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
|
@Override
|
||||||
public String getByPropertyOfNullable(BlogProperties property) {
|
public String getByPropertyOfNullable(PropertyEnum<String> property) {
|
||||||
return getByProperty(property).orElse(null);
|
return getByProperty(property).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getByPropertyOfNonNull(BlogProperties property) {
|
public String getByPropertyOfNonNull(PropertyEnum<String> property) {
|
||||||
Assert.notNull(property, "Blog property must not be null");
|
Assert.notNull(property, "Blog property must not be null");
|
||||||
|
|
||||||
return getByKeyOfNonNull(property.getValue());
|
return getByKeyOfNonNull(property.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getByProperty(BlogProperties property) {
|
public Optional<String> getByProperty(PropertyEnum<String> property) {
|
||||||
Assert.notNull(property, "Blog property must not be null");
|
Assert.notNull(property, "Blog property must not be null");
|
||||||
|
|
||||||
return getByKey(property.getValue());
|
return getByKey(property.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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");
|
Assert.notNull(property, "Blog property must not be null");
|
||||||
|
|
||||||
return getByProperty(property, propertyType).orElse(defaultValue);
|
return getByProperty(property, propertyType).orElse(defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> getByProperty(BlogProperties property, Class<T> propertyType) {
|
public <T> Optional<T> getByProperty(PropertyEnum<String> property, Class<T> propertyType) {
|
||||||
return getByProperty(property).map(propertyValue -> convertTo(propertyValue, propertyType));
|
return getByProperty(property).map(propertyValue -> PropertyEnum.convertTo(propertyValue, propertyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -187,7 +188,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> getByKey(String key, Class<T> valueType) {
|
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
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue