io.rxmicro.rest.local.FromStringValueConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxmicro-rest Show documentation
Show all versions of rxmicro-rest Show documentation
The basic module that defines basic RxMicro annotations, required when using the REST architecture of building program systems.
/*
* Copyright (c) 2020. https://rxmicro.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.rxmicro.rest.local;
import io.rxmicro.http.error.ValidationException;
import io.rxmicro.rest.model.HttpModelType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static io.rxmicro.common.local.Examples.INSTANT_EXAMPLE;
import static io.rxmicro.common.util.Strings.split;
import static io.rxmicro.http.HttpValues.STRING_ARRAY_DELIMITER;
import static java.util.Collections.unmodifiableList;
/**
* @author nedis
* @since 0.1
*/
@SuppressWarnings({"ForLoopReplaceableByForEach", "rawtypes", "unchecked"})
public abstract class FromStringValueConverter {
private static final int DEFAULT_SIZE = 5;
private static final List EMPTY_LIST = List.of();
/**
* Enable for overriding by sub classes
*/
@SuppressWarnings("SameReturnValue")
protected String getStringArrayDelimiter() {
return STRING_ARRAY_DELIMITER;
}
// -----------------------------------------------------------------------------------------------------------------
protected final > E toEnum(final Class enumClass,
final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Enum.valueOf(enumClass, value);
} catch (final IllegalArgumentException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a value from the set ?, but actual is '?'!",
httpModelType, modelName, Arrays.toString(enumClass.getEnumConstants()), value
);
}
}
}
protected final > List toEnumArray(final Class enumClass,
final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
try {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List strings = split(value, getStringArrayDelimiter());
for (int j = 0; j < strings.size(); j++) {
result.add(toEnum(enumClass, strings.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} catch (final ClassCastException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a string array, but actual is ?!",
httpModelType, modelName, list
);
}
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Boolean toBoolean(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
if (Boolean.TRUE.toString().equalsIgnoreCase(value)) {
return Boolean.TRUE;
} else if (Boolean.FALSE.toString().equalsIgnoreCase(value)) {
return Boolean.FALSE;
} else {
throw new ValidationException(
"Invalid ? \"?\": Expected a boolean value, but actual is '?'!",
httpModelType, modelName, value);
}
}
}
protected final List toBooleanArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toBoolean(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Byte toByte(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Byte.parseByte(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a byte value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toByteArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toByte(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Short toShort(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Short.parseShort(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a short value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toShortArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toShort(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Integer toInteger(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Integer.parseInt(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected an integer value, but actual is '?'!",
httpModelType, modelName
);
}
}
}
protected final List toIntegerArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toInteger(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Long toLong(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Long.parseLong(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a long value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toLongArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toLong(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Character toCharacter(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
if (value.length() != 1) {
throw new ValidationException(
"Invalid ? \"?\": Expected a character, but actual is '?'!",
httpModelType, modelName, value
);
}
return value.charAt(0);
}
}
protected final List toCharacterArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toCharacter(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Float toFloat(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Float.parseFloat(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a float value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toFloatArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toFloat(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Double toDouble(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Double.parseDouble(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a double value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toDoubleArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toDouble(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final BigDecimal toBigDecimal(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return new BigDecimal(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a big decimal value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toBigDecimalArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toBigDecimal(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final BigInteger toBigInteger(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return new BigInteger(value);
} catch (final NumberFormatException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected a big integer value, but actual is '?'!",
httpModelType, modelName, value
);
}
}
}
protected final List toBigIntegerArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toBigInteger(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final Instant toInstant(final String value,
final HttpModelType httpModelType,
final String modelName) {
if (value == null) {
return null;
} else {
try {
return Instant.parse(value);
} catch (final DateTimeParseException ignore) {
throw new ValidationException(
"Invalid ? \"?\": Expected an ISO-8601" +
"(Example: '?'), but actual is '?'!",
httpModelType, modelName, INSTANT_EXAMPLE, value);
}
}
}
protected final List toInstantArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toInstant(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
@SuppressWarnings("unused")
protected final String toString(final String value,
final HttpModelType httpModelType,
final String modelName) {
return value;
}
protected final List toStringArray(final List list,
final HttpModelType httpModelType,
final String modelName) {
if (list != null) {
final int size = list.size();
final List result = new ArrayList<>(size == 1 ? DEFAULT_SIZE : size);
for (int i = 0; i < size; i++) {
final String value = list.get(i);
final List array = split(value, getStringArrayDelimiter());
for (int j = 0; j < array.size(); j++) {
result.add(toString(array.get(j), httpModelType, modelName));
}
}
return unmodifiableList(result);
} else {
return EMPTY_LIST;
}
}
// -----------------------------------------------------------------------------------------------------------------
protected final void throwNotImplYet(final String message) {
throw new UnsupportedOperationException(message);
}
}