/*
 * Copyright 2014-2017 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron.driver.media;

import java.net.InetSocketAddress;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Integer.parseInt;

class SocketAddressUtil
{
    private static final Pattern IPV4_ADDRESS_PATTERN = Pattern.compile("([^:]+)(?::([0-9]+))?");
    private static final Pattern IPV6_ADDRESS_PATTERN = Pattern.compile(
        "\\[([0-9A-Fa-f:]+)(?:%[a-zA-Z0-9_.~-]+)?\\](?::([0-9]+))?");

    static InetSocketAddress parse(
        final CharSequence cs, final BiFunction<String, String, InetSocketAddress> consumer)
    {
        if (null == cs)
        {
            throw new NullPointerException("Input string must not be null");
        }

        final Matcher ipV4Matcher = IPV4_ADDRESS_PATTERN.matcher(cs);

        if (ipV4Matcher.matches())
        {
            final String host = ipV4Matcher.group(1);
            final String portString = ipV4Matcher.group(2);

            return consumer.apply(host, portString);
        }

        final Matcher ipV6Matcher = IPV6_ADDRESS_PATTERN.matcher(cs);

        if (ipV6Matcher.matches())
        {
            final String host = ipV6Matcher.group(1);
            final String portString = ipV6Matcher.group(2);

            return consumer.apply(host, portString);
        }

        throw new IllegalArgumentException("Invalid format: " + cs);
    }

    /**
     * Utility for parsing socket addresses from a {@link CharSequence}.  Supports
     * hostname:port, ipV4Address:port and [ipV6Address]:port
     *
     * @param cs Input string
     * @return An InetSocketAddress parsed from the input.
     */
    static InetSocketAddress parse(final CharSequence cs)
    {
        return parse(cs, (hostString, portString) ->
        {
            if (null == portString)
            {
                throw new IllegalArgumentException("The 'port' portion of the address is required");
            }

            return new InetSocketAddress(hostString, parseInt(portString));
        });
    }
}

@@ -664,46 +664,8 @@
 ss;%0A
-import java.util.function.BiFunction;%0A
 impo
@@ -1044,131 +1044,348 @@
     
-static InetSocketAddress parse(
+/**%0A     * Utility for parsing socket addresses from a %7B@link CharSequence%7D.  Supports%0A     * hostname:port, ipV4Address:port and %5BipV6Address%5D:port
 %0A     
+*%0A
    
-final CharSequence cs, final BiFunction%3CString, String, InetSocketAddress%3E consumer
+  * @param cs to be parsed for the socket address.%0A     * @return An %7B@link InetSocketAddress%7D for the parsed input.%0A     */%0A    static InetSocketAddress parse(final CharSequence cs
 )%0A  
@@ -1705,30 +1705,24 @@
  String port
-String
  = ipV4Match
@@ -1750,38 +1750,40 @@
      return 
-consumer.apply
+newSocketAddress
 (host, portS
@@ -1773,38 +1773,32 @@
 dress(host, port
-String
 );%0A        %7D%0A%0A  
@@ -1994,22 +1994,16 @@
 ing port
-String
  = ipV6M
@@ -2043,22 +2043,24 @@
 urn 
-consumer.apply
+newSocketAddress
 (hos
@@ -2066,22 +2066,16 @@
 st, port
-String
 );%0A     
@@ -2164,276 +2164,44 @@
     
-/**%0A     * Utility for parsing socket addresses from a %7B@link CharSequence%7D.  Supports%0A     * hostname:port, ipV4Address:port and %5BipV6Address%5D:port%0A     *%0A     * @param cs Input string%0A     * @return An InetSocketAddress parsed from the input.%0A     */%0A    static Inet
+private static InetSocketAddress new
 Sock
@@ -2213,105 +2213,51 @@
 ress
- parse
 (final 
-CharSequence cs)%0A    %7B%0A        return parse(cs, (host
+String host, final 
 String
-,
  port
-String) -%3E%0A  
+)%0A
     
-  
 %7B%0A  
@@ -2258,28 +2258,24 @@
   %7B%0A        
-    
 if (null == 
@@ -2278,22 +2278,16 @@
  == port
-String
 )%0A      
@@ -2284,34 +2284,26 @@
 rt)%0A        
-    %7B%0A    
+%7B%0A
             
@@ -2391,35 +2391,27 @@
 %22);%0A        
-    %7D%0A%0A    
+%7D%0A%0A
         retu
@@ -2439,22 +2439,16 @@
 ess(host
-String
 , parseI
@@ -2458,27 +2458,9 @@
 port
-String));%0A        %7D
+)
 );%0A 

Remove unnecessary and complicating allocating lambda.