All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gridgo.utils.format.CommonNumberTransformerRegistry Maven / Gradle / Ivy

package io.gridgo.utils.format;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class CommonNumberTransformerRegistry extends DefaultFormatTransformerRegistry {

    public static final FormatTransformer ROUND_TO_INTEGER = (source) -> source == null ? null
            : Long.toString(new BigDecimal(source.toString()).longValue());

    public static final FormatTransformer FORMAT_PERCENTAGE = newDecimaFormatTransformer("#.##%");

    public static final FormatTransformer FORMAT_WITH_THOUSAND_SEPARATOR = newDecimaFormatTransformer("###,###.####");

    public static final FormatTransformer newDecimaFormatTransformer(final String pattern) {
        return source -> {
            if (source == null) {
                return null;
            }
            DecimalFormat df = new DecimalFormat(pattern);
            return df.format(Double.valueOf(source.toString()));
        };
    }

//    public static final FormatTransformer newEvalExpTransformer(final String expression,
//            final String defaultSingleVariable) {
//        return source -> {
//            if (source == null) {
//                return null;
//            }
//
//            final Expression exp = new Expression(expression);
//            if (source instanceof Map) {
//                for (Entry entry : ((Map) source).entrySet()) {
//                    exp.with(entry.getKey().toString(),
//                            entry.getValue() instanceof Number
//                                    ? BigDecimal.valueOf(((Number) entry.getValue()).doubleValue())
//                                    : new BigDecimal(entry.getValue().toString()));
//                }
//            } else {
//                exp.with(defaultSingleVariable,
//                        source instanceof Number ? BigDecimal.valueOf(((Number) source).doubleValue())
//                                : new BigDecimal(source.toString()));
//            }
//            return exp.eval();
//        };
//    }

//    public static final FormatTransformer newXEvalExpTransformer(final String expression) {
//        return newEvalExpTransformer(expression, "x");
//    }

    public static final CommonNumberTransformerRegistry newInstance() {
        return new CommonNumberTransformerRegistry();
    }

    private CommonNumberTransformerRegistry() {
        this.addTransformer("roundToInteger", ROUND_TO_INTEGER);
        this.addTransformer("percentage", FORMAT_PERCENTAGE);
        this.addTransformer("thousandSeparate", FORMAT_WITH_THOUSAND_SEPARATOR);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy