1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.format;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  /**
22   * <a href="USAPhoneNumberFormat.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class USAPhoneNumberFormat implements PhoneNumberFormat {
27  
28      public String format(String phoneNumber) {
29          if (phoneNumber == null) {
30              return StringPool.BLANK;
31          }
32  
33          if (phoneNumber.length() > 10) {
34              StringBundler sb = new StringBundler(8);
35  
36              sb.append(StringPool.OPEN_PARENTHESIS);
37              sb.append(phoneNumber.substring(0, 3));
38              sb.append(") ");
39              sb.append(phoneNumber.substring(3, 6));
40              sb.append(StringPool.DASH);
41              sb.append(phoneNumber.substring(6, 10));
42              sb.append(" x");
43              sb.append(phoneNumber.substring(10));
44  
45              return sb.toString();
46          }
47          else if (phoneNumber.length() == 10) {
48              StringBundler sb = new StringBundler(6);
49  
50              sb.append(StringPool.OPEN_PARENTHESIS);
51              sb.append(phoneNumber.substring(0, 3));
52              sb.append(") ");
53              sb.append(phoneNumber.substring(3, 6));
54              sb.append(StringPool.DASH);
55              sb.append(phoneNumber.substring(6));
56  
57              return sb.toString();
58          }
59          else if (phoneNumber.length() == 7) {
60              return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
61                  phoneNumber.substring(3));
62          }
63          else {
64              return phoneNumber;
65          }
66      }
67  
68      public String strip(String phoneNumber) {
69          return StringUtil.extractDigits(phoneNumber);
70      }
71  
72  }