Refactor BlogProperties enum

pull/137/head
johnniang 2019-03-25 13:18:09 +08:00
parent c52123edc6
commit ee2bc1c5ed
3 changed files with 184 additions and 31 deletions

View File

@ -1,5 +1,8 @@
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
@ -9,158 +12,165 @@ public enum BlogProperties implements ValueEnum<String> {
/** /**
* *
*/ */
BLOG_LOCALE("blog_locale"), BLOG_LOCALE("blog_locale", String.class),
/** /**
* *
*/ */
BLOG_TITLE("blog_title"), BLOG_TITLE("blog_title", String.class),
/** /**
* *
*/ */
BLOG_URL("blog_url"), BLOG_URL("blog_url", String.class),
/** /**
* *
*/ */
POST_SUMMARY("post_summary"), POST_SUMMARY("post_summary", Long.class),
/** /**
* *
*/ */
INDEX_POSTS("index_posts"), INDEX_POSTS("index_posts", Integer.class),
/** /**
* *
*/ */
INDEX_COMMENTS("index_comments"), INDEX_COMMENTS("index_comments", Integer.class),
/** /**
* *
*/ */
IS_INSTALL("is_install"), IS_INSTALL("is_install", Boolean.class),
/** /**
* RSS * RSS
*/ */
RSS_POSTS("rss_posts"), RSS_POSTS("rss_posts", Integer.class),
/** /**
* API * API
*/ */
API_STATUS("api_status"), API_STATUS("api_status", Boolean.class),
/** /**
* *
*/ */
MAIL_SMTP_HOST("mail_smtp_host"), MAIL_SMTP_HOST("mail_smtp_host", String.class),
/** /**
* *
*/ */
MAIL_SMTP_USERNAME("mail_smtp_username"), MAIL_SMTP_USERNAME("mail_smtp_username", String.class),
/** /**
* *
*/ */
MAIL_SMTP_PASSWORD("mail_smtp_password"), MAIL_SMTP_PASSWORD("mail_smtp_password", String.class),
/** /**
* *
*/ */
MAIL_FROM_NAME("mail_from_name"), MAIL_FROM_NAME("mail_from_name", String.class),
/** /**
* *
*/ */
SMTP_EMAIL_ENABLE("smtp_email_enable"), SMTP_EMAIL_ENABLE("smtp_email_enable", Boolean.class),
/** /**
* *
*/ */
COMMENT_REPLY_NOTICE("comment_reply_notice"), COMMENT_REPLY_NOTICE("comment_reply_notice", Boolean.class),
/** /**
* *
*/ */
NEW_COMMENT_NEED_CHECK("new_comment_need_check"), NEW_COMMENT_NEED_CHECK("new_comment_need_check", Boolean.class),
/** /**
* *
*/ */
NEW_COMMENT_NOTICE("new_comment_notice"), NEW_COMMENT_NOTICE("new_comment_notice", Boolean.class),
/** /**
* *
*/ */
COMMENT_PASS_NOTICE("comment_pass_notice"), COMMENT_PASS_NOTICE("comment_pass_notice", Boolean.class),
/** /**
* *
*/ */
SEO_DESC("seo_desc"), SEO_DESC("seo_desc", String.class),
/** /**
* *
*/ */
THEME("theme"), THEME("theme", String.class),
/** /**
* *
*/ */
BLOG_START("blog_start"), BLOG_START("blog_start", Long.class),
/** /**
* *
*/ */
WIDGET_POSTCOUNT("widget_postcount"), WIDGET_POSTCOUNT("widget_postcount", Boolean.class),
/** /**
* *
*/ */
WIDGET_COMMENTCOUNT("widget_commentcount"), WIDGET_COMMENTCOUNT("widget_commentcount", Boolean.class),
/** /**
* *
*/ */
WIDGET_ATTACHMENTCOUNT("widget_attachmentcount"), WIDGET_ATTACHMENTCOUNT("widget_attachmentcount", Boolean.class),
/** /**
* *
*/ */
WIDGET_DAYCOUNT("widget_daycount"), WIDGET_DAYCOUNT("widget_daycount", Boolean.class),
/** /**
* *
*/ */
DEFAULT_THUMBNAIL("/static/halo-content/images/thumbnail/thumbnail.png"), DEFAULT_THUMBNAIL("/static/halo-content/images/thumbnail/thumbnail.png", String.class),
/** /**
* *
*/ */
AUTO_BACKUP("auto_backup"), AUTO_BACKUP("auto_backup", Boolean.class),
/** /**
* API Token * API Token
*/ */
API_TOKEN("api_token"), API_TOKEN("api_token", String.class),
/** /**
* *
*/ */
ATTACH_LOC("attach_loc"), ATTACH_LOC("attach_loc", String.class),
/** /**
* Zone. * Zone.
*/ */
QINIU_ZONE("qiniu_zone"); QINIU_ZONE("qiniu_zone", String.class);
private String value; private String value;
BlogProperties(String value) { private Class<?> type;
BlogProperties(String value, Class<?> type) {
if (!supportType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
this.value = value; this.value = value;
this.type = type;
} }
/** /**
@ -172,4 +182,81 @@ public enum BlogProperties implements ValueEnum<String> {
public String getValue() { public String getValue() {
return value; return value;
} }
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 (!supportType(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 supportType(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

@ -129,6 +129,48 @@ public interface OptionService extends CrudService<Option, Integer> {
@NonNull @NonNull
Optional<String> getByProperty(@NonNull BlogProperties property); Optional<String> getByProperty(@NonNull BlogProperties property);
/**
* Gets property value by blog property.
*
* @param property blog property must not be null
* @param propertyType property type must not be null
* @param defaultValue default value
* @param <T> property type
* @return property value
*/
<T> T getByProperty(@NonNull BlogProperties property, @NonNull Class<T> propertyType, T defaultValue);
/**
* Gets property value by blog property.
*
* @param property blog property must not be null
* @param propertyType property type must not be null
* @param <T> property type
* @return property value
*/
<T> Optional<T> getByProperty(@NonNull BlogProperties property, @NonNull Class<T> propertyType);
/**
* Gets value by key.
*
* @param key key must not be null
* @param valueType value type must not be null
* @param defaultValue default value
* @param <T> property type
* @return value
*/
<T> T getByKey(@NonNull String key, @NonNull Class<T> valueType, T defaultValue);
/**
* Gets value by key.
*
* @param key key must not be null
* @param valueType value type must not be null
* @param <T> value type
* @return value
*/
<T> Optional<T> getByKey(@NonNull String key, @NonNull Class<T> valueType);
/** /**
* Gets post page size. * Gets post page size.
* *

View File

@ -21,6 +21,8 @@ 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
* *
@ -165,6 +167,28 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
return getByKey(property.getValue()); return getByKey(property.getValue());
} }
@Override
public <T> T getByProperty(BlogProperties 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));
}
@Override
public <T> T getByKey(String key, Class<T> valueType, T defaultValue) {
return getByKey(key, valueType).orElse(defaultValue);
}
@Override
public <T> Optional<T> getByKey(String key, Class<T> valueType) {
return getByKey(key).map(value -> convertTo(value, valueType));
}
@Override @Override
public int getPostPageSize() { public int getPostPageSize() {
try { try {