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

org.thymeleaf.expression.Numbers Maven / Gradle / Ivy

/*
 * =============================================================================
 * 
 *   Copyright (c) 2011, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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 org.thymeleaf.expression;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.exceptions.ExpressionEvaluationException;
import org.thymeleaf.util.NumberPointType;
import org.thymeleaf.util.NumberUtils;
import org.thymeleaf.util.Validate;


/**
 * 
 * @author Daniel Fernández
 * 
 * @since 1.0
 *
 */
public final class Numbers {
    

    public String format(final Number target, final Integer minIntegerDigits) {
        return NumberUtils.format(target, minIntegerDigits, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits));
        }
        return result;
    }

    
    
    
    public String format(final Number target, final Integer minIntegerDigits, final String thousandsPointType) {
        final NumberPointType thousandsNumberPointType = NumberPointType.match(thousandsPointType);
        if (thousandsNumberPointType == null) {
            throw new ExpressionEvaluationException(
                    "Unrecognized point format \"" + thousandsPointType + "\"");
        }
        return NumberUtils.format(target, minIntegerDigits, thousandsNumberPointType, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits, final String thousandsPointType) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits, thousandsPointType);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits, final String thousandsPointType) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits, final String thousandsPointType) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType));
        }
        return result;
    }
    

    
    
    
    
    public String format(final Number target, final Integer minIntegerDigits, final Integer decimalDigits) {
        return NumberUtils.format(target, minIntegerDigits, decimalDigits, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits, decimalDigits);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, decimalDigits));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, decimalDigits));
        }
        return result;
    }

    
    

    
    
    
    public String format(final Number target, final Integer minIntegerDigits, final Integer decimalDigits, final String decimalPointType) {
        final NumberPointType decimalNumberPointType = NumberPointType.match(decimalPointType);
        if (decimalNumberPointType == null) {
            throw new ExpressionEvaluationException(
                    "Unrecognized point format \"" + decimalPointType + "\"");
        }
        return NumberUtils.format(target, minIntegerDigits, decimalDigits, decimalNumberPointType, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits, decimalDigits, decimalPointType);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, decimalDigits, decimalPointType));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, decimalDigits, decimalPointType));
        }
        return result;
    }

    
    

    
    
    
    public String format(final Number target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits) {
        final NumberPointType thousandsNumberPointType = NumberPointType.match(thousandsPointType);
        if (thousandsNumberPointType == null) {
            throw new ExpressionEvaluationException(
                    "Unrecognized point format \"" + thousandsPointType + "\"");
        }
        return NumberUtils.format(target, minIntegerDigits, thousandsNumberPointType, decimalDigits, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits, thousandsPointType, decimalDigits);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType, decimalDigits));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType, decimalDigits));
        }
        return result;
    }


    
    
    
    
    
    
    public String format(final Number target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits, final String decimalPointType) {
        final NumberPointType decimalNumberPointType = NumberPointType.match(decimalPointType);
        if (decimalNumberPointType == null) {
            throw new ExpressionEvaluationException(
                    "Unrecognized point format \"" + decimalPointType + "\"");
        }
        final NumberPointType thousandsNumberPointType = NumberPointType.match(thousandsPointType);
        if (thousandsNumberPointType == null) {
            throw new ExpressionEvaluationException(
                    "Unrecognized point format \"" + thousandsPointType + "\"");
        }
        return NumberUtils.format(target, minIntegerDigits, thousandsNumberPointType, decimalDigits, decimalNumberPointType, getLocale());
    }

    public String[] format(final Object[] target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final String[] result = new String[target.length];
        for (int i = 0; i < target.length; i++) {
            result[i] = format((Number)target[i], minIntegerDigits, thousandsPointType, decimalDigits, decimalPointType);
        }
        return result;
    }

    public List format(final List target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final List result = new ArrayList();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType, decimalDigits, decimalPointType));
        }
        return result;
    }

    public Set format(final Set target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits, final String decimalPointType) {
        Validate.notNull(target, "Target cannot be null");
        final Set result = new LinkedHashSet();
        for (final Number element : target) {
            result.add(format(element, minIntegerDigits, thousandsPointType, decimalDigits, decimalPointType));
        }
        return result;
    }

    
    
    

    
    
    
    private static Locale getLocale() {
        Locale locale = TemplateEngine.threadLocale();
        if (locale == null) {
            // If no locale has been set by the template engine, use the system default.
            locale = Locale.getDefault();
        }
        return locale;
    }
    
    
    
    
    
    
    public Numbers() {
        super();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy