Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
 import java.util.Map;
 
 
 /* ------------------------------------------------------------ */
Internet address map to object

Internet addresses may be specified as absolute address or as a combination of four octet wildcard specifications (a.b.c.d) that are defined as follows.

 nnn - an absolute value (0-255)
 mmm-nnn - an inclusive range of absolute values, 
           with following shorthand notations:
           nnn- => nnn-255
           -nnn => 0-nnn
           -    => 0-255
 a,b,... - a list of wildcard specifications
 
 
 @SuppressWarnings("serial")
 public class IPAddressMap<TYPE> extends HashMap<String, TYPE>
 {
     private final HashMap<String,IPAddrPattern_patterns = new HashMap<String,IPAddrPattern>();
 
     /* --------------------------------------------------------------- */
    
Construct empty IPAddressMap.
 
     public IPAddressMap()
     {
         super(11);
     }
    
     /* --------------------------------------------------------------- */
    
Construct empty IPAddressMap.

Parameters:
capacity initial capacity
 
     public IPAddressMap(int capacity)
     {
         super (capacity);
     }
 
     /* ------------------------------------------------------------ */
    
Insert a new internet address into map

 
     @Override
     public TYPE put(String addrSpec, TYPE object)
         throws IllegalArgumentException
     {
         if (addrSpec == null || addrSpec.trim().length() == 0)
             throw new IllegalArgumentException("Invalid IP address pattern: "+addrSpec);
         
         String spec = addrSpec.trim();
         if (.get(spec) == null)
             .put(spec,new IPAddrPattern(spec));
         
         return super.put(specobject);
     }
     
     /* ------------------------------------------------------------ */
    
Retrieve the object mapped to the specified internet address literal

 
     @Override
     public TYPE get(Object key)
     {
         return super.get(key);
     }
     
     /* ------------------------------------------------------------ */
    
Retrieve the first object that is associated with the specified internet address by taking into account the wildcard specifications.

Parameters:
addr internet address
Returns:
associated object
    public TYPE match(String addr)
    {
        Map.Entry<String, TYPE> entry = getMatch(addr);
        return entry==null ? null : entry.getValue();
    }
    
    /* ------------------------------------------------------------ */
    
Retrieve the first map entry that is associated with the specified internet address by taking into account the wildcard specifications.

Parameters:
addr internet address
Returns:
map entry associated
    public Map.Entry<String, TYPE> getMatch(String addr)
    {
        if (addr != null)
        {
            for(Map.Entry<String, TYPE> entrysuper.entrySet())
            {
                if (.get(entry.getKey()).match(addr))
                {
                    return entry;
                }
            }
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    
Retrieve a lazy list of map entries associated with specified internet address by taking into account the wildcard specifications.

Parameters:
addr internet address
Returns:
lazy list of map entries
    public Object getLazyMatches(String addr)
    {
        if (addr == null)
            return LazyList.getList(super.entrySet());
        
        Object entries = null;
        for(Map.Entry<String, TYPE> entrysuper.entrySet())
        {
            if (.get(entry.getKey()).match(addr))
            {
                entries = LazyList.add(entries,entry);
            }
        }
        return entries;        
    }
    
    /* ------------------------------------------------------------ */
    
IPAddrPattern Represents internet address wildcard. Matches the wildcard to provided internet address.
    private static class IPAddrPattern
    {
        private final OctetPattern[] _octets = new OctetPattern[4];
        /* ------------------------------------------------------------ */
        
Create new IPAddrPattern

Parameters:
value internet address wildcard specification
Throws:
java.lang.IllegalArgumentException if wildcard specification is invalid
        public IPAddrPattern(String value)
            throws IllegalArgumentException
        {
            if (value == null || value.trim().length() == 0)
                throw new IllegalArgumentException("Invalid IP address pattern: "+value);
                
            try
            {
                StringTokenizer parts = new StringTokenizer(value".");
                
                String part;
                for (int idx=0; idx<4; idx++)
                {
                    part = parts.hasMoreTokens() ? parts.nextToken().trim() : "0-255";
                    
                    int len = part.length();
                    if (len == 0 && parts.hasMoreTokens())
                        throw new IllegalArgumentException("Invalid IP address pattern: "+value);
                    
                    [idx] = new OctetPattern(len==0 ? "0-255" : part);
                }
            }
            catch (IllegalArgumentException ex)
            {
                throw new IllegalArgumentException("Invalid IP address pattern: "+valueex);
            }
        }
        
        /* ------------------------------------------------------------ */
        
Match the specified internet address against the wildcard

Parameters:
value internet address
Returns:
true if specified internet address matches wildcard specification
Throws:
java.lang.IllegalArgumentException if specified internet address is invalid
        public boolean match(String value)
            throws IllegalArgumentException
        {
            if (value == null || value.trim().length() == 0)
                throw new IllegalArgumentException("Invalid IP address: "+value);
            
            try
            {
                StringTokenizer parts = new StringTokenizer(value".");
                
                boolean result = true;
                for (int idx=0; idx<4; idx++)
                {
                    if (!parts.hasMoreTokens())
                        throw new IllegalArgumentException("Invalid IP address: "+value);
                        
                    if (!(result &= [idx].match(parts.nextToken())))
                        break;
                }
                return result;
            }
            catch (IllegalArgumentException ex)
            {
                throw new IllegalArgumentException("Invalid IP address: "+valueex);
            }
        }
    }
        
    /* ------------------------------------------------------------ */
    
OctetPattern Represents a single octet wildcard. Matches the wildcard to the specified octet value.
    private static class OctetPattern extends BitSet
    {
        private final BitSet _mask = new BitSet(256);
        
        /* ------------------------------------------------------------ */
        
Create new OctetPattern

Parameters:
octetSpec octet wildcard specification
Throws:
java.lang.IllegalArgumentException if wildcard specification is invalid
        public OctetPattern(String octetSpec)
            throws IllegalArgumentException
        {
            try
            {
                if (octetSpec != null)
                {
                    String spec = octetSpec.trim();
                    if(spec.length() == 0)
                    {
                        .set(0,255);
                    }
                    else
                    {
                        StringTokenizer parts = new StringTokenizer(spec,",");
                        while (parts.hasMoreTokens())
                        {
                            String part = parts.nextToken().trim();
                            if (part.length() > 0)
                            {
                                if (part.indexOf('-') < 0)
                                {
                                    Integer value = Integer.valueOf(part);
                                    .set(value);
                                }
                                else
                                {
                                    int low = 0, high = 255;
                                    
                                    String[] bounds = part.split("-",-2);
                                    if (bounds.length != 2)
                                    {
                                        throw new IllegalArgumentException("Invalid octet spec: "+octetSpec);
                                    }
                                    
                                    if (bounds[0].length() > 0)
                                    {
                                        low = Integer.parseInt(bounds[0]);
                                    }
                                    if (bounds[1].length() > 0)
                                    {
                                        high = Integer.parseInt(bounds[1]);
                                    }
                                    
                                    if (low > high)
                                    {
                                        throw new IllegalArgumentException("Invalid octet spec: "+octetSpec);
                                    }
                                    
                                    .set(lowhigh+1);
                                }
                            }
                        }
                    }
                }
            }
            catch (NumberFormatException ex)
            {
                throw new IllegalArgumentException("Invalid octet spec: "+octetSpecex);
            }
        }
        
        /* ------------------------------------------------------------ */
        
Match specified octet value against the wildcard

Parameters:
value octet value
Returns:
true if specified octet value matches the wildcard
Throws:
java.lang.IllegalArgumentException if specified octet value is invalid
        public boolean match(String value)
            throws IllegalArgumentException
        {
            if (value == null || value.trim().length() == 0)
                throw new IllegalArgumentException("Invalid octet: "+value);
            try
            {
                int number = Integer.parseInt(value);
                return match(number);
            }
            catch (NumberFormatException ex)
            {
                throw new IllegalArgumentException("Invalid octet: "+value);
            }
        }
        
        /* ------------------------------------------------------------ */
        
Match specified octet value against the wildcard

Parameters:
number octet value
Returns:
true if specified octet value matches the wildcard
Throws:
java.lang.IllegalArgumentException if specified octet value is invalid
        public boolean match(int number)
            throws IllegalArgumentException
        {
            if (number < 0 || number > 255)
                throw new IllegalArgumentException("Invalid octet: "+number);
            
            return .get(number);
        }
    }   
New to GrepCode? Check out our FAQ X