|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * BroadleafCommerce Common Libraries |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Broadleaf Commerce |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package org.broadleafcommerce.common.web.patch; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.math.BigInteger; |
| 24 | + |
| 25 | +/** |
| 26 | + * Source for this patch is taken from https://github.com/thymeleaf/thymeleaf/commit/fc360d0d50df5090f93f6922012463055f28896d. |
| 27 | + * This is part of https://github.com/thymeleaf/thymeleaf/issues/449, which will be resolved in 2.1.5-RELEASE (not released yet). |
| 28 | + * This fix benefits the performance of many Broadleaf templates that utilize string concatenation in expressions. |
| 29 | + * |
| 30 | + * When Thymeleaf 2.1.5 is released, the Broadleaf dependency should be updated and this patch should be removed. |
| 31 | + * |
| 32 | + * @author Jeff Fischer |
| 33 | + */ |
| 34 | +public class EvaluationUtilPatch { |
| 35 | + |
| 36 | + public static BigDecimal evaluateAsNumber(final Object object) { |
| 37 | + |
| 38 | + if (object == null) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if (object instanceof Number) { |
| 43 | + if (object instanceof BigDecimal) { |
| 44 | + return (BigDecimal)object; |
| 45 | + } else if (object instanceof BigInteger) { |
| 46 | + return new BigDecimal((BigInteger)object); |
| 47 | + } else if (object instanceof Short) { |
| 48 | + return new BigDecimal(((Short)object).intValue()); |
| 49 | + } else if (object instanceof Integer) { |
| 50 | + return new BigDecimal(((Integer)object).intValue()); |
| 51 | + } else if (object instanceof Long) { |
| 52 | + return new BigDecimal(((Long)object).longValue()); |
| 53 | + } else if (object instanceof Float) { |
| 54 | + //noinspection UnpredictableBigDecimalConstructorCall |
| 55 | + return new BigDecimal(((Float)object).doubleValue()); |
| 56 | + } else if (object instanceof Double) { |
| 57 | + //noinspection UnpredictableBigDecimalConstructorCall |
| 58 | + return new BigDecimal(((Double)object).doubleValue()); |
| 59 | + } |
| 60 | + } else if (object instanceof String && ((String)object).length() > 0) { |
| 61 | + final char c0 = ((String)object).charAt(0); |
| 62 | + // This test will avoid trying to create the BigDecimal most of the times, which |
| 63 | + // will improve performance by avoiding lots of NumberFormatExceptions |
| 64 | + if ((c0 >= '0' && c0 <= '9') || c0 == '+' || c0 == '-') { |
| 65 | + try { |
| 66 | + return new BigDecimal(((String)object).trim()); |
| 67 | + } catch (final NumberFormatException ignored) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments