未验证 提交 7f3b318c 编写于 作者: L Liang Zhang 提交者: GitHub

Add new domain TypedPropertyValue (#4809)

* Add new domain TypedPropertiesValue

* rename TypedPropertiesKey and TypedPropertiesValue

* remove useless StringUtil
上级 8f7c02a9
......@@ -21,14 +21,14 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Apollo properties enum.
*/
@RequiredArgsConstructor
@Getter
public enum ApolloPropertiesEnum implements TypedPropertiesKey {
public enum ApolloPropertiesEnum implements TypedPropertyKey {
/**
* Apollo config client app id param.
......
......@@ -19,14 +19,14 @@ package org.apache.shardingsphere.orchestration.center.instance;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Etcd properties enum.
*/
@RequiredArgsConstructor
@Getter
public enum EtcdPropertiesEnum implements TypedPropertiesKey {
public enum EtcdPropertiesEnum implements TypedPropertyKey {
/**
* The portal url for apollo open api client.
......
......@@ -19,14 +19,14 @@ package org.apache.shardingsphere.orchestration.center.instance;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Nacos properties enum.
*/
@RequiredArgsConstructor
@Getter
public enum NacosPropertiesEnum implements TypedPropertiesKey {
public enum NacosPropertiesEnum implements TypedPropertyKey {
/**
* Nacos config service group name.
......
......@@ -19,14 +19,14 @@ package org.apache.shardingsphere.orchestration.center.instance;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Zookeeper properties enum.
*/
@RequiredArgsConstructor
@Getter
public enum ZookeeperPropertiesEnum implements TypedPropertiesKey {
public enum ZookeeperPropertiesEnum implements TypedPropertyKey {
/**
* Retry interval milliseconds when connect with zookeeper curator client.
......
......@@ -20,108 +20,58 @@ package org.apache.shardingsphere.underlying.common.properties.common;
import com.google.common.base.Joiner;
import lombok.Getter;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.underlying.common.util.StringUtil;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* Typed properties with a specified enum.
*/
public abstract class TypedProperties<E extends Enum & TypedPropertiesKey> {
public abstract class TypedProperties<E extends Enum & TypedPropertyKey> {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
@Getter
private final Properties props;
private final Map<E, Object> cache;
private final Map<E, TypedPropertyValue> cache;
public TypedProperties(final Class<E> keyClass, final Properties props) {
this.props = props;
validate(keyClass);
cache = preload(keyClass);
}
private void validate(final Class<E> keyClass) {
private Map<E, TypedPropertyValue> preload(final Class<E> keyClass) {
E[] enumConstants = keyClass.getEnumConstants();
Map<E, TypedPropertyValue> result = new HashMap<>(enumConstants.length, 1);
Collection<String> errorMessages = new LinkedList<>();
for (E each : keyClass.getEnumConstants()) {
if (null != props.get(each.getKey())) {
validate(each, props.get(each.getKey()).toString()).ifPresent(errorMessages::add);
for (E each : enumConstants) {
TypedPropertyValue value = null;
try {
value = new TypedPropertyValue(each, props.getOrDefault(each.getKey(), each.getDefaultValue()).toString());
} catch (final TypedPropertyValueException ex) {
errorMessages.add(ex.getMessage());
}
result.put(each, value);
}
if (!errorMessages.isEmpty()) {
throw new ShardingSphereConfigurationException(Joiner.on(LINE_SEPARATOR).join(errorMessages));
}
}
private Optional<String> validate(final E enumKey, final String value) {
Class<?> type = enumKey.getType();
if (type == boolean.class && !StringUtil.isBooleanValue(value)) {
return Optional.of(createErrorMessage(enumKey, value));
}
if (type == int.class && !StringUtil.isIntValue(value)) {
return Optional.of(createErrorMessage(enumKey, value));
}
if (type == long.class && !StringUtil.isLongValue(value)) {
return Optional.of(createErrorMessage(enumKey, value));
}
return Optional.empty();
}
private String createErrorMessage(final E enumKey, final String invalidValue) {
return String.format("Value '%s' of '%s' cannot convert to type '%s'. ", invalidValue, enumKey.getKey(), enumKey.getType().getName());
}
private Map<E, Object> preload(final Class<E> keyClass) {
return preloadActualValues(preloadStringValues(keyClass));
}
private Map<E, String> preloadStringValues(final Class<E> keyClass) {
E[] enumConstants = keyClass.getEnumConstants();
Map<E, String> result = new HashMap<>(enumConstants.length, 1);
for (E each : enumConstants) {
result.put(each, props.getOrDefault(each.getKey(), each.getDefaultValue()).toString());
}
return result;
}
private Map<E, Object> preloadActualValues(final Map<E, String> stringValueMap) {
Map<E, Object> result = new ConcurrentHashMap<>(stringValueMap.size(), 1);
for (Entry<E, String> entry : stringValueMap.entrySet()) {
result.put(entry.getKey(), getActualValue(entry.getKey(), entry.getValue()));
}
return result;
}
private Object getActualValue(final E enumKey, final String value) {
if (boolean.class == enumKey.getType()) {
return Boolean.valueOf(value);
}
if (int.class == enumKey.getType()) {
return Integer.valueOf(value);
}
if (long.class == enumKey.getType()) {
return Long.valueOf(value);
}
return value;
}
/**
* Get property value.
*
* @param enumKey enum key
* @param key property key
* @param <T> class type of return value
* @return property value
*/
@SuppressWarnings("unchecked")
public <T> T getValue(final E enumKey) {
return (T) cache.get(enumKey);
public <T> T getValue(final E key) {
return (T) (cache.containsKey(key) ? cache.get(key).getValue() : null);
}
}
......@@ -18,28 +18,28 @@
package org.apache.shardingsphere.underlying.common.properties.common;
/**
* Typed properties key interface.
* Typed property key.
*/
public interface TypedPropertiesKey {
public interface TypedPropertyKey {
/**
* Return the specific key.
* Get property key.
*
* @return key
* @return property key
*/
String getKey();
/**
* Return the default value.
* Get default property value.
*
* @return defaultValue
* @return default property value
*/
String getDefaultValue();
/**
* Return the specific type.
* Get property type.
*
* @return type
* @return property type
*/
Class<?> getType();
}
......@@ -15,54 +15,40 @@
* limitations under the License.
*/
package org.apache.shardingsphere.underlying.common.util;
package org.apache.shardingsphere.underlying.common.properties.common;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* String utility class.
* Typed property value.
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class StringUtil {
@Getter
public final class TypedPropertyValue {
/**
* Judge is boolean value or not.
*
* @param value to be judged string value
* @return is boolean value or not
*/
public static boolean isBooleanValue(final String value) {
return Boolean.TRUE.toString().equalsIgnoreCase(value) || Boolean.FALSE.toString().equalsIgnoreCase(value);
}
private Object value;
/**
* Judge is int value or not.
*
* @param value to be judged string value
* @return is int value or not
*/
public static boolean isIntValue(final String value) {
try {
Integer.parseInt(value);
return true;
} catch (final NumberFormatException ex) {
return false;
}
public TypedPropertyValue(final TypedPropertyKey key, final String value) throws TypedPropertyValueException {
this.value = createTypedValue(key, value);
}
/**
* Judge is long value or not.
*
* @param value to be judged string value
* @return is long value or not
*/
public static boolean isLongValue(final String value) {
try {
Long.parseLong(value);
return true;
} catch (final NumberFormatException ex) {
return false;
private Object createTypedValue(final TypedPropertyKey key, final String value) throws TypedPropertyValueException {
if (boolean.class == key.getType()) {
return Boolean.valueOf(value);
}
if (int.class == key.getType()) {
try {
return Integer.valueOf(value);
} catch (final NumberFormatException ex) {
throw new TypedPropertyValueException(key, value);
}
}
if (long.class == key.getType()) {
try {
return Long.valueOf(value);
} catch (final NumberFormatException ex) {
throw new TypedPropertyValueException(key, value);
}
}
return value;
}
}
......@@ -15,27 +15,14 @@
* limitations under the License.
*/
package org.apache.shardingsphere.underlying.common.util;
package org.apache.shardingsphere.underlying.common.properties.common;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public final class StringUtilTest {
@Test
public void assertIsBooleanValue() {
assertTrue(StringUtil.isBooleanValue("TRUE"));
assertTrue(StringUtil.isBooleanValue("FALSE"));
assertTrue(StringUtil.isBooleanValue("true"));
assertTrue(StringUtil.isBooleanValue("False"));
assertFalse(StringUtil.isBooleanValue("error"));
}
/**
* Typed property value exception.
*/
public final class TypedPropertyValueException extends Exception {
@Test
public void assertIsIntValue() {
assertTrue(StringUtil.isIntValue("-10"));
assertFalse(StringUtil.isIntValue("1-1"));
public TypedPropertyValueException(final TypedPropertyKey key, final String value) {
super(String.format("Value '%s' of '%s' cannot convert to type '%s'. ", value, key.getKey(), key.getType().getName()));
}
}
......@@ -19,14 +19,14 @@ package org.apache.shardingsphere.underlying.common.properties.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Properties constant.
*/
@RequiredArgsConstructor
@Getter
public enum PropertiesConstant implements TypedPropertiesKey {
public enum PropertiesConstant implements TypedPropertyKey {
/**
* Enable or Disable to show SQL details.
......
......@@ -19,14 +19,14 @@ package org.apache.shardingsphere.underlying.common.properties.orchestration;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertiesKey;
import org.apache.shardingsphere.underlying.common.properties.common.TypedPropertyKey;
/**
* Orchestration properties enum.
*/
@RequiredArgsConstructor
@Getter
public enum OrchestrationPropertiesEnum implements TypedPropertiesKey {
public enum OrchestrationPropertiesEnum implements TypedPropertyKey {
/**
* Enable or Disable to overwrite orchestration config center data.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册