/src/mpv5/utils/text/TypeConversion.java
Java | 114 lines | 52 code | 16 blank | 46 comment | 12 complexity | 9be7b9f033fa4c151be182705980cbd4 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
1 2/* 3 * This file is part of YaBS. 4 * 5 * YaBS is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * YaBS is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 17 */ 18package mpv5.utils.text; 19 20//~--- JDK imports ------------------------------------------------------------ 21import java.net.InetAddress; 22import java.util.Locale; 23import java.util.logging.Level; 24import java.util.logging.Logger; 25import java.util.regex.Pattern; 26import javax.mail.internet.AddressException; 27import javax.mail.internet.InternetAddress; 28 29/** 30 * 31 * 32 */ 33public class TypeConversion { 34 35 private static final Pattern rfc2822 = Pattern.compile( 36 "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"); 37 38 /** 39 * Converts a boolean value into either "0" (false) or "1" (true) 40 * 41 * @param bool 42 * @return The string value 43 */ 44 public static String booleanToString(boolean bool) { 45 String booleans = "0"; 46 47 if (bool) { 48 booleans = "1"; 49 } 50 51 return booleans; 52 } 53 54 /** 55 * Converts a String into a boolean value. Everything except the String "1" 56 * or "true" will return false. 57 * 58 * @param string 59 * @return 60 */ 61 public static boolean stringToBoolean(String string) { 62 boolean val = false; 63 64 if (string != null) { 65 if (string.equals("1") || Boolean.valueOf(string)) { 66 val = true; 67 } 68 } 69 70 return val; 71 } 72 73 /** 74 * Converts a String into a Locale 75 * 76 * @param localestring In Format "de" or "de_DE" 77 * @return 78 */ 79 public static Locale stringToLocale(String localestring) { 80 String[] data = localestring.split("_"); 81 String lang = localestring.substring(0, 2); 82 String country = null; 83 84 if ((data != null) && (data.length > 1)) { 85 country = data[1]; 86 } 87 88 if (country != null) { 89 return new Locale(lang.toLowerCase(), country.toUpperCase()); 90 } else { 91 return new Locale(lang.toLowerCase()); 92 } 93 } 94 95 /** 96 * 97 * @param email 98 * @return 99 */ 100 public static InternetAddress stringToMail(String email) { 101 if (!rfc2822.matcher(email).matches()) { 102 return null; 103 } 104 try { 105 return new InternetAddress(email); 106 } catch (AddressException ex) { 107 Logger.getLogger(TypeConversion.class.getName()).log(Level.SEVERE, null, ex); 108 return null; 109 } 110 } 111} 112 113 114//~ Formatted by Jindent --- http://www.jindent.com