001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.components.valid.interceptor;
017
018import com.jfinal.aop.Interceptor;
019import com.jfinal.aop.Invocation;
020import com.jfinal.kit.Ret;
021import io.jboot.components.valid.ValidUtil;
022import io.jboot.utils.ClassUtil;
023
024import javax.validation.constraints.DecimalMin;
025import java.lang.reflect.Parameter;
026import java.math.BigDecimal;
027import java.math.BigInteger;
028
029public class DecimalMinInterceptor implements Interceptor {
030
031    @Override
032    public void intercept(Invocation inv) {
033        Parameter[] parameters = inv.getMethod().getParameters();
034
035        for (int index = 0; index < parameters.length; index++) {
036            DecimalMin decimalMin = parameters[index].getAnnotation(DecimalMin.class);
037            if (decimalMin == null) {
038                continue;
039            }
040            Object validObject = inv.getArg(index);
041            if (validObject != null && !matches(decimalMin, validObject)) {
042                String reason = parameters[index].getName() + " min value is " + decimalMin.value() + ", but current value is " + validObject + " at method: " + ClassUtil.buildMethodString(inv.getMethod());
043                Ret paras = Ret.by("value", decimalMin.value());
044                ValidUtil.throwValidException(parameters[index].getName(), decimalMin.message(), paras, reason);
045            }
046        }
047
048        inv.invoke();
049    }
050
051
052    private boolean matches(DecimalMin decimalMax, Object validObject) {
053        if (validObject instanceof BigInteger) {
054            return ((BigInteger) validObject).compareTo(new BigInteger(decimalMax.value())) >= 0;
055        } else if (validObject instanceof BigDecimal) {
056            return ((BigDecimal) validObject).compareTo(new BigDecimal(decimalMax.value())) >= 0;
057        } else if (validObject instanceof CharSequence) {
058            return (new BigDecimal(validObject.toString())).compareTo(new BigDecimal(decimalMax.value())) >= 0;
059        } else if (validObject instanceof Float) {
060            return ((Float) validObject) >= Float.parseFloat(decimalMax.value());
061        } else if (validObject instanceof Double) {
062            return ((Double) validObject) >= Double.parseDouble(decimalMax.value());
063        } else if (validObject instanceof Number) {
064            return ((Number) validObject).longValue() >= Long.parseLong(decimalMax.value());
065        }
066        return false;
067    }
068
069
070}