View Javadoc
1   /*
2    * Copyright (C) 2010 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 2.1 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  
20  package org.exoplatform.services.portletcache;
21  
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  /**
29   * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
30   * @version $Revision$
31   */
32  class Util
33  {
34  
35    static Map<String, String[]> clone(Map<String, String[]> map)
36    {
37      if (map.isEmpty())
38      {
39        return Collections.emptyMap();
40      }
41      Map<String, String[]> copy = new HashMap<String, String[]>(map);
42      for (Map.Entry<String, String[]> entry : copy.entrySet())
43      {
44        entry.setValue(entry.getValue().clone());
45      }
46      return copy;
47    }
48  
49    static int hashCode(Map<String, String[]> map)
50    {
51      int hashCode = 0;
52      if (map.size() > 0)
53      {
54        for (Map.Entry<String, String[]> parameter : map.entrySet())
55        {
56          int parameterHashCode = parameter.getKey().hashCode();
57          for (String parameterValue : parameter.getValue())
58          {
59            parameterHashCode = parameterHashCode * 43 + parameterValue.hashCode();
60          }
61          hashCode = hashCode * 43 + parameterHashCode;
62        }
63      }
64      return hashCode;
65    }
66  
67    static boolean equals(Map<String, String[]> map1, Map<String, String[]> map2)
68    {
69      if (map1.keySet().equals(map2.keySet()))
70      {
71        for (Map.Entry<String, String[]> parameter : map1.entrySet())
72        {
73          String[] thatParameterValues = map2.get(parameter.getKey());
74          if (thatParameterValues != null)
75          {
76            if (!Arrays.equals(parameter.getValue(), thatParameterValues))
77            {
78              return false;
79            }
80          }
81        }
82        return true;
83      }
84      return false;
85    }
86  
87    static void toString(Map<String, String[]> map, StringBuilder sb)
88    {
89      sb.append('{');
90      for (Iterator<Map.Entry<String, String[]>> i = map.entrySet().iterator();i.hasNext();)
91      {
92        Map.Entry<String, String[]> entry = i.next();
93        sb.append(entry.getKey()).append('=').append('[');
94        String[] value = entry.getValue();
95        for (int j = 0;j < value.length;j++)
96        {
97          if (j > 0)
98          {
99            sb.append(',');
100         }
101         sb.append(value[j]);
102       }
103       sb.append(']');
104       if (i.hasNext())
105       {
106         sb.append(',');
107       }
108     }
109     sb.append('}');
110   }
111 }