001
014
015 package com.liferay.util.format;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020
021
024 public class USAPhoneNumberFormat implements PhoneNumberFormat {
025
026 public String format(String phoneNumber) {
027 if (phoneNumber == null) {
028 return StringPool.BLANK;
029 }
030
031 if (phoneNumber.length() > 10) {
032 StringBundler sb = new StringBundler(8);
033
034 sb.append(StringPool.OPEN_PARENTHESIS);
035 sb.append(phoneNumber.substring(0, 3));
036 sb.append(") ");
037 sb.append(phoneNumber.substring(3, 6));
038 sb.append(StringPool.DASH);
039 sb.append(phoneNumber.substring(6, 10));
040 sb.append(" x");
041 sb.append(phoneNumber.substring(10));
042
043 return sb.toString();
044 }
045 else if (phoneNumber.length() == 10) {
046 StringBundler sb = new StringBundler(6);
047
048 sb.append(StringPool.OPEN_PARENTHESIS);
049 sb.append(phoneNumber.substring(0, 3));
050 sb.append(") ");
051 sb.append(phoneNumber.substring(3, 6));
052 sb.append(StringPool.DASH);
053 sb.append(phoneNumber.substring(6));
054
055 return sb.toString();
056 }
057 else if (phoneNumber.length() == 7) {
058 return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
059 phoneNumber.substring(3));
060 }
061 else {
062 return phoneNumber;
063 }
064 }
065
066 public String strip(String phoneNumber) {
067 return StringUtil.extractDigits(phoneNumber);
068 }
069
070 }