aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/addrbook/PhoneNumberI18nServiceImpl.java
blob: 6ff5a8ccf778952444af9e89d6a374296b4a0d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.addrbook;

import java.util.*;

import net.java.sip.communicator.service.protocol.*;

import com.google.i18n.phonenumbers.*;
import com.google.i18n.phonenumbers.Phonenumber.*;

/**
 * Implements <tt>PhoneNumberI18nService</tt> which aids the parsing, formatting
 * and validating of international phone numbers using the libphonenumber
 * library.
 *
 * @author Lyubomir Marinov
 */
public class PhoneNumberI18nServiceImpl
    implements PhoneNumberI18nService
{

    /**
     * Normalizes a <tt>String</tt> phone number by converting alpha characters
     * to their respective digits on a keypad and then stripping non-digit
     * characters.
     * 
     * @param phoneNumber a <tt>String</tt> which represents a phone number to
     * normalize
     * @return a <tt>String</tt> which is a normalized form of the specified
     * <tt>phoneNumber</tt>
     * @see PhoneNumberI18nService#normalize(String)
     */
    public String normalize(String phoneNumber)
    {
        return
            PhoneNumberUtil.normalizeDigitsOnly(
                    PhoneNumberUtil.convertAlphaCharactersInNumber(
                            phoneNumber));
    }

    /**
     * Determines whether two <tt>String</tt> phone numbers match.
     *
     * @param aPhoneNumber a <tt>String</tt> which represents a phone number to
     * match to <tt>bPhoneNumber</tt>
     * @param bPhoneNumber a <tt>String</tt> which represents a phone number to
     * match to <tt>aPhoneNumber</tt>
     * @return <tt>true</tt> if the specified <tt>String</tt>s match as phone
     * numbers; otherwise, <tt>false</tt>
     */
    public boolean phoneNumbersMatch(String aPhoneNumber, String bPhoneNumber)
    {
        PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

        try
        {
            String defaultCountry = Locale.getDefault().getCountry();

            if ((defaultCountry != null) && (defaultCountry.length() != 0))
            {
                PhoneNumber a
                    = phoneNumberUtil.parse(aPhoneNumber, defaultCountry);
                PhoneNumber b
                    = phoneNumberUtil.parse(bPhoneNumber, defaultCountry);

                if (PhoneNumberUtil.MatchType.NO_MATCH
                        != phoneNumberUtil.isNumberMatch(a, b))
                    return true;
            }
        }
        catch (NumberParseException npex)
        {
            /* Ignore it, we'll try without the defaultCountry. */
        }
        try
        {
            return
                PhoneNumberUtil.MatchType.NO_MATCH
                    != phoneNumberUtil.isNumberMatch(aPhoneNumber, bPhoneNumber);
        }
        catch (NumberParseException npex)
        {
            throw new IllegalArgumentException(npex);
        }
    }

    public boolean isNumber(String possibleNumber)
    {
        PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
        try
        {
            if (numberUtil.isPossibleNumber(possibleNumber, null))
                return true;
            else
                return numberUtil.isPossibleNumber(
                    numberUtil.parse(   possibleNumber,
                                        Locale.getDefault().getCountry()));
        }
        catch (NumberParseException e)
        {
            return false;
        }
    }

    public boolean isPhoneNumber(String possibleNumber)
    {
        PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
        try
        {
            if (numberUtil.isPossibleNumber(possibleNumber, null))
                return true;
            else
                return numberUtil.isPossibleNumber(
                    numberUtil.parse(   possibleNumber,
                                        Locale.getDefault().getCountry()));
        }
        catch (NumberParseException e)
        {
            return false;
        }
    }
}