-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c4be86d
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
java-phone-utils.iml | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# QuintoAndar Java Phone Utils | ||
A simple lib to validate and parser phone numbers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>br.com.quintoandar</groupId> | ||
<artifactId>java-phone-utils</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.googlecode.libphonenumber</groupId> | ||
<artifactId>libphonenumber</artifactId> | ||
<version>${libphonenumber}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${lombok.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>${google.guava}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.1</version> | ||
<configuration> | ||
<altDeploymentRepository>internal.repo::default::file://${internal.repo.path}</altDeploymentRepository> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<properties> | ||
<libphonenumber>8.9.0</libphonenumber> | ||
<lombok.version>1.16.20</lombok.version> | ||
<google.guava>24.0-jre</google.guava> | ||
<jackson.version>2.9.4</jackson.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<internal.repo.path>${basedir}/mvn-repo/</internal.repo.path> | ||
</properties> | ||
|
||
</project> |
83 changes: 83 additions & 0 deletions
83
src/main/java/br/com/quintoandar/phone/utils/BRPhoneParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package br.com.quintoandar.phone.utils; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.i18n.phonenumbers.NumberParseException; | ||
import com.google.i18n.phonenumbers.PhoneNumberUtil; | ||
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; | ||
|
||
/** | ||
* This class parses th string containing a phone number in the format: | ||
* (XX) BYYYZZZZE | ||
* If XX is 11 AND B is 9, then E must be in the string. Otherwise, the | ||
* parser returns false. | ||
* If XX is NOT 11, then E must nor exist in the string. | ||
* | ||
* @author pericles | ||
* | ||
*/ | ||
public class BRPhoneParser { | ||
|
||
/** | ||
* Deprecated: this method does not validate the telephone number correctly, if phoneNumber has only 1 digit, | ||
* it will return false. However, if it has more than one, it will return true. | ||
* <br> | ||
* Use {@link BRPhoneParser#isValidPhoneNumber(String, boolean)} instead. It validates the number, including | ||
* the new 9-digit. | ||
* <br> | ||
* Parser method | ||
* @param phoneNumber - Phone String | ||
* @param notNullNumber - Flag to tell parser if the phone must be | ||
* not null or not (if it is a primary number or not) | ||
* @return true or false | ||
*/ | ||
@Deprecated | ||
public static boolean checkPhoneNumber (String phoneNumber, boolean notNullNumber) { | ||
if (notNullNumber) { | ||
if (Strings.isNullOrEmpty(phoneNumber)) { | ||
return false; | ||
} | ||
} | ||
|
||
if(!Strings.isNullOrEmpty(phoneNumber)){ | ||
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); | ||
try { | ||
PhoneNumber pnumber = phoneUtil.parse(phoneNumber, "BR"); | ||
return pnumber != null; | ||
} catch (NumberParseException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Validates if the phone string is a valid phone number, includes the validation | ||
* for the new 9-digit (on all states where it's applicable). | ||
* @see PhoneNumberUtil#isValidNumber(PhoneNumber) | ||
* | ||
* @param phoneNumber - Phone string | ||
* @param notNullNumber - Flag to tell parser if the phone must be | ||
* not null or not (if it is a primary number or not) | ||
* @return | ||
*/ | ||
public static boolean isValidPhoneNumber(String phoneNumber, boolean notNullNumber) { | ||
if (notNullNumber) { | ||
if (Strings.isNullOrEmpty(phoneNumber)) { | ||
return false; | ||
} | ||
} | ||
|
||
if(! Strings.isNullOrEmpty(phoneNumber)){ | ||
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); | ||
try { | ||
PhoneNumber pnumber = phoneUtil.parse(phoneNumber, "BR"); | ||
return pnumber != null && phoneUtil.isValidNumber(pnumber); | ||
} catch (NumberParseException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/br/com/quintoandar/phone/utils/PhoneNumberSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package br.com.quintoandar.phone.utils; | ||
|
||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.google.i18n.phonenumbers.NumberParseException; | ||
import com.google.i18n.phonenumbers.PhoneNumberUtil; | ||
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; | ||
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; | ||
|
||
public class PhoneNumberSerializer extends JsonSerializer<String> { | ||
|
||
protected PhoneNumberUtil phoneUtil; | ||
|
||
public PhoneNumberSerializer() { | ||
phoneUtil = PhoneNumberUtil.getInstance(); | ||
} | ||
|
||
public String writeAsPhoneNumber(String src) throws NumberParseException{ | ||
if (src != null) { | ||
PhoneNumber phonNumb; | ||
|
||
phonNumb = phoneUtil.parse(src, "BR"); | ||
String fmt = phoneUtil.format(phonNumb, PhoneNumberFormat.INTERNATIONAL); | ||
if (fmt.startsWith("+55")) { | ||
fmt = phoneUtil.format(phonNumb, PhoneNumberFormat.NATIONAL); | ||
} | ||
return fmt; | ||
} | ||
return src; | ||
} | ||
|
||
public String parseFromPhoneNumber(String src) | ||
throws NumberParseException, NumberFormatException { | ||
PhoneNumber phonNumb = phoneUtil.parse(src, "BR"); | ||
return phoneUtil.format(phonNumb, PhoneNumberFormat.E164); | ||
} | ||
|
||
@Override | ||
public void serialize(String src, JsonGenerator gen, SerializerProvider prov) | ||
throws IOException, JsonProcessingException { | ||
try { | ||
gen.writeObject(writeAsPhoneNumber(src)); | ||
} catch (NumberParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |