KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > util > Util


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.cache.util;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * General utility methods used throughout the JBC code base.
29  *
30  * @author <a HREF="brian.stansberry@jboss.com">Brian Stansberry</a>
31  * @version $Revision: 1.2 $
32  */

33 public final class Util
34 {
35    /**
36     * Loads the specified class using this class's classloader, or, if it is <code>null</code>
37     * (i.e. this class was loaded by the bootstrap classloader), the system classloader.
38     * <p/>
39     * If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this
40     * class may be loaded by the bootstrap classloader.
41     * </p>
42     *
43     * @param classname name of the class to load
44     * @return the class
45     * @throws ClassNotFoundException
46     */

47    public static Class JavaDoc loadClass(String JavaDoc classname) throws ClassNotFoundException JavaDoc
48    {
49       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
50       if (cl == null)
51          cl = ClassLoader.getSystemClassLoader();
52       return cl.loadClass(classname);
53    }
54
55    /**
56     * Prevent instantiation
57     */

58    private Util()
59    {
60    }
61
62    /**
63     * Calculates the diffs between data maps passed in to {@link org.jboss.cache.CacheListener#nodeModified(org.jboss.cache.Fqn,boolean,boolean,java.util.Map)}
64     * before and after modification. This only makes sense if the modification type is {@link org.jboss.cache.CacheListener.ModificationType#PUT_MAP}. See the javadoc on
65     * {@link org.jboss.cache.CacheListener#nodeModified(org.jboss.cache.Fqn,boolean,boolean,java.util.Map)} for more details.
66     *
67     * @param pre map of data before the node was modified
68     * @param post Map of data after the node was modified
69     * @return MapModifications containing the differences.
70     */

71    public static MapModifications diffNodeData(Map JavaDoc<Object JavaDoc, Object JavaDoc> pre, Map JavaDoc<Object JavaDoc, Object JavaDoc> post)
72    {
73       MapModifications mods = new MapModifications();
74
75       // let's start with what's been added and modified.
76
for (Object JavaDoc key : post.keySet())
77       {
78          if (pre.containsKey(key))
79          {
80             if (!post.get(key).equals(pre.get(key)))
81             {
82                mods.modifiedEntries.put(key, post.get(key));
83             }
84          }
85          else
86          {
87             mods.addedEntries.put(key, post.get(key));
88          }
89       }
90
91       // now the removed entries.
92
for (Object JavaDoc key : pre.keySet())
93       {
94          if (!post.containsKey(key))
95          {
96             mods.removedEntries.put(key, pre.get(key));
97          }
98       }
99
100       return mods;
101    }
102
103    /**
104     * Static inner class that holds 3 maps - for data added, removed and modified.
105     */

106    public static class MapModifications
107    {
108       public final Map JavaDoc<Object JavaDoc, Object JavaDoc> addedEntries = new HashMap JavaDoc<Object JavaDoc, Object JavaDoc>();
109       public final Map JavaDoc<Object JavaDoc, Object JavaDoc> removedEntries = new HashMap JavaDoc<Object JavaDoc, Object JavaDoc>();
110       public final Map JavaDoc<Object JavaDoc, Object JavaDoc> modifiedEntries = new HashMap JavaDoc<Object JavaDoc, Object JavaDoc>();
111
112
113       public boolean equals(Object JavaDoc o)
114       {
115          if (this == o) return true;
116          if (o == null || getClass() != o.getClass()) return false;
117
118          MapModifications that = (MapModifications) o;
119
120          if (addedEntries != null ? !addedEntries.equals(that.addedEntries) : that.addedEntries != null) return false;
121          if (modifiedEntries != null ? !modifiedEntries.equals(that.modifiedEntries) : that.modifiedEntries != null)
122             return false;
123          if (removedEntries != null ? !removedEntries.equals(that.removedEntries) : that.removedEntries != null)
124             return false;
125
126          return true;
127       }
128
129       public int hashCode()
130       {
131          int result;
132          result = (addedEntries != null ? addedEntries.hashCode() : 0);
133          result = 31 * result + (removedEntries != null ? removedEntries.hashCode() : 0);
134          result = 31 * result + (modifiedEntries != null ? modifiedEntries.hashCode() : 0);
135          return result;
136       }
137
138       public String JavaDoc toString()
139       {
140          return "Added Entries " + addedEntries + " Removeed Entries " + removedEntries + " Modified Entries " + modifiedEntries;
141       }
142    }
143
144 }
145
Popular Tags