package com.alexanderlogistics;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Pattern;
/**
 * Rundungs Methode
 * 
 * See: https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places
 * 
 * @author rsoika
 *
 */
public class InvoiceUtil {

    public static double round(double value) {
        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
    
    
    /**
     * Hilfsmethode zum validieren der Mailadresse
     * 
     * @param emailAddress
     * @return
     */
    public static boolean validateEmail(String emailAddress) {
        //String regexPattern = "^(.+)@(\\S+)$";
        
        String regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" 
                + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
        return Pattern.compile(regexPattern)
          .matcher(emailAddress)
          .matches();
    }
    
    
    @Deprecated
    public static double roundConservative(double value) {
        value = value * 100;
        long tmp = Math.round(value);
        return (double) tmp / 100;
    }

}
