public static String toString(PhoneNumber phoneNumber) { if (phoneNumber == null) return ""; if (isEmpty(phoneNumber)) return ""; // String text = phoneNumber.toString(); String area = phoneNumber.getArea(); String number = phoneNumber.getNumber(); String text = "(" + area + ") " + number.substring(0, 3) + "-" + number.substring(3); return text; }
public static boolean isEmpty(PhoneNumber phoneNumber) { if (phoneNumber == null) return true; boolean status = false; status |= StringUtils.isEmpty(phoneNumber.getArea()); status |= StringUtils.isEmpty(phoneNumber.getNumber()); status |= phoneNumber.getCountry() != Country.USA; status |= phoneNumber.getType() != PhoneLocation.HOME; return status; }
public static boolean validate(PhoneNumber phoneNumber) { if (phoneNumber == null) return false; Validator validator = Validator.getValidator(); validator.notEmpty(phoneNumber.getArea(), "\"Area\" must be specified"); validator.notNull(phoneNumber.getCountry(), "\"Country\" must be specified"); validator.notEmpty(phoneNumber.getNumber(), "\"Number\" must be specified"); validator.notNull(phoneNumber.getType(), "\"Type\" must be specified"); validator.notNull( phoneNumber.getArea().length() != 3, "\"Area\" must have length 3: " + phoneNumber.getArea()); validator.notNull( phoneNumber.getNumber().length() != 7, "\"Number\" must have length 7: " + phoneNumber.getNumber()); boolean isValid = validator.isValid(); return isValid; }
public static String toPhoneNumberString(PhoneNumber phoneNumber) { if (phoneNumber == null) return null; StringBuffer buf = new StringBuffer(); if (phoneNumber.getArea() != null) buf.append("(" + phoneNumber.getArea() + ") "); if (phoneNumber.getNumber() != null) { buf.append(phoneNumber.getNumber().substring(0, 3) + "-"); buf.append(phoneNumber.getNumber().substring(3, 7)); if (phoneNumber.getExtension() != null) buf.append(" " + phoneNumber.getExtension()); } return buf.toString(); }
public static PhoneNumber toPhoneNumber(String text) { if (text == null || text.length() == 0) return null; text = text.replaceAll("\\(", ""); text = text.replaceAll("\\)", ""); text = text.replaceAll("-", ""); text = text.replaceAll("x", ""); text = text.replaceAll(" ", ""); text = text.replaceAll("[a-z]", ""); text = text.replaceAll("[A-Z]", ""); if (text.length() < 10) { throw new RuntimeException("Unexpected value for PhoneNumber: " + text); } PhoneNumber phoneNumber = new PhoneNumber(); // TODO externalize this: phoneNumber.setCountry(Country.USA); if (text.length() >= 3) phoneNumber.setArea(text.substring(0, 3)); if (text.length() >= 10) phoneNumber.setNumber(text.substring(3, 10)); else if (text.length() >= 3) phoneNumber.setNumber(text.substring(3)); if (text.length() > 10) phoneNumber.setExtension(text.substring(10)); return phoneNumber; }
public static void initialize(PhoneNumber phoneNumber) { if (phoneNumber.getCountry() == null) phoneNumber.setCountry(Country.USA); if (phoneNumber.getType() == null) phoneNumber.setType(PhoneLocation.HOME); }
public static PhoneNumber create(PhoneLocation phoneLocation) { PhoneNumber phoneNumber = create(); phoneNumber.setType(phoneLocation); return phoneNumber; }
public static PhoneNumber clone(PhoneNumber phoneNumber) { if (phoneNumber == null) return null; PhoneNumber clone = create(); clone.setId(ObjectUtil.clone(phoneNumber.getId())); clone.setArea(ObjectUtil.clone(phoneNumber.getArea())); clone.setNumber(ObjectUtil.clone(phoneNumber.getNumber())); clone.setExtension(ObjectUtil.clone(phoneNumber.getExtension())); clone.setCountry(phoneNumber.getCountry()); clone.setType(phoneNumber.getType()); clone.setValue(ObjectUtil.clone(phoneNumber.getValue())); return clone; }