1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }