package org.lantern;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;

public class EmailAddressUtils {
    /**
     * From https://github.com/getlantern/lantern/issues/1300:
     *
     * "We should have a LanternControllerUtils.normalizeEmail that takes an email (or
     * the userid part of a jabberid) and returns a normalized version with folded
     * case, '.'s and '+'s stripped from the username, and '@googlemail.com' replaced
     * with '@gmail.com'."
     *
     * Domains other than gmail.com and googlemail.com are assumed to be Google Apps for
     * Domains domains and are not changed.
     *
     * If a string that is not of the expected form is passed in, no guarantee is
     * made about the result.
     *
     * @param input The email address to normalize.
     * @return The normalized email address.
     */
    public static String normalizedEmail(String input) throws Exception {
        input = input.toLowerCase(Locale.ENGLISH);
        int idx = input.indexOf("@");
        String beforeAt = input.substring(0, idx);
        String rest = input.substring(idx);
        beforeAt = beforeAt.replaceAll("\\.", "");
        beforeAt = StringUtils.substringBefore(beforeAt, "+");
        rest = rest.replaceAll("googlemail.com$", "gmail.com");
        return beforeAt + rest;
    }
}

@@ -138,144 +138,13 @@
   * 
-From https://github.com/getlantern/lantern/issues/1300:%0A     *%0A     * %22We should have a LanternControllerUtils.normalizeEmail that takes
+Given
  an 
@@ -152,23 +152,16 @@
 mail (or
-%0A     *
  the use
@@ -183,20 +183,17 @@
 abberid)
- and
+,
  returns
@@ -222,152 +222,221 @@
 with
- fold
+%0A     * the following transformations appli
 ed
+:
 %0A     *
- case, '.'s and '+'s stripped from the username, and '@googlemail.com' replaced%0A     * with '@gmail.com'.%22
+%0A     *     - lowercase%0A     *     - '.' characters removed from the username part%0A     *     - plus-extensions removed from the username
 %0A     *
-%0A
      
-* D
+- d
 omains o
 ther
@@ -435,79 +435,58 @@
 ns o
-ther than gmail.com and googlemail.com are assumed to be Google Apps fo
+f 'googlemail.com' replaced with 'gmail.com'. othe
 r%0A  
@@ -494,15 +494,13 @@
   * 
-Domains
+     
  dom
@@ -508,27 +508,18 @@
 ins 
-and are not changed
+left as-is
 .%0A  

improve docstring