1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4//  ------------------------------------------------------------------------
5//  All rights reserved. This program and the accompanying materials
6//  are made available under the terms of the Eclipse Public License v1.0
7//  and Apache License v2.0 which accompanies this distribution.
8//
9//      The Eclipse Public License is available at
10//      http://www.eclipse.org/legal/epl-v10.html
11//
12//      The Apache License v2.0 is available at
13//      http://www.opensource.org/licenses/apache2.0.php
14//
15//  You may elect to redistribute this code under either of these licenses.
16//  ========================================================================
17//
18
19
20package org.eclipse.jetty.util;
21
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.Map;
25
26/* ------------------------------------------------------------ */
27/**
28 */
29@SuppressWarnings("serial")
30public class HostMap<TYPE> extends HashMap<String, TYPE>
31{
32
33    /* --------------------------------------------------------------- */
34    /** Construct empty HostMap.
35     */
36    public HostMap()
37    {
38        super(11);
39    }
40
41    /* --------------------------------------------------------------- */
42    /** Construct empty HostMap.
43     *
44     * @param capacity initial capacity
45     */
46    public HostMap(int capacity)
47    {
48        super (capacity);
49    }
50
51    /* ------------------------------------------------------------ */
52    /**
53     * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
54     */
55    @Override
56    public TYPE put(String host, TYPE object)
57        throws IllegalArgumentException
58    {
59        return super.put(host, object);
60    }
61
62    /* ------------------------------------------------------------ */
63    /**
64     * @see java.util.HashMap#get(java.lang.Object)
65     */
66    @Override
67    public TYPE get(Object key)
68    {
69        return super.get(key);
70    }
71
72    /* ------------------------------------------------------------ */
73    /**
74     * Retrieve a lazy list of map entries associated with specified
75     * hostname by taking into account the domain suffix matches.
76     *
77     * @param host hostname
78     * @return lazy list of map entries
79     */
80    public Object getLazyMatches(String host)
81    {
82        if (host == null)
83            return LazyList.getList(super.entrySet());
84
85        int idx = 0;
86        String domain = host.trim();
87        HashSet<String> domains = new HashSet<String>();
88        do {
89            domains.add(domain);
90            if ((idx = domain.indexOf('.')) > 0)
91            {
92                domain = domain.substring(idx+1);
93            }
94        } while (idx > 0);
95
96        Object entries = null;
97        for(Map.Entry<String, TYPE> entry: super.entrySet())
98        {
99            if (domains.contains(entry.getKey()))
100            {
101                entries = LazyList.add(entries,entry);
102            }
103        }
104
105        return entries;
106    }
107
108}
109