|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Util.java | 50% | 62.5% | 50% | 55.9% |
|
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; | |
25 | import java.util.Map; | |
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.4 $ | |
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 | 4838 | public static Class loadClass(String classname) throws ClassNotFoundException |
48 | { | |
49 | 4838 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
50 | 4838 | if (cl == null) |
51 | 0 | cl = ClassLoader.getSystemClassLoader(); |
52 | 4838 | return cl.loadClass(classname); |
53 | } | |
54 | ||
55 | /** | |
56 | * Prevent instantiation | |
57 | */ | |
58 | 0 | 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,org.jboss.cache.CacheListener.ModificationType,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}. | |
65 | * Refer to {@link org.jboss.cache.CacheListener#nodeModified(org.jboss.cache.Fqn,boolean,boolean,org.jboss.cache.CacheListener.ModificationType,Map)}. | |
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 | 9 | public static MapModifications diffNodeData(Map<Object, Object> pre, Map<Object, Object> post) |
72 | { | |
73 | 9 | MapModifications mods = new MapModifications(); |
74 | ||
75 | // let's start with what's been added and modified. | |
76 | 9 | for (Object key : post.keySet()) |
77 | { | |
78 | 15 | if (pre.containsKey(key)) |
79 | { | |
80 | 11 | if (!post.get(key).equals(pre.get(key))) |
81 | { | |
82 | 4 | mods.modifiedEntries.put(key, post.get(key)); |
83 | } | |
84 | } | |
85 | else | |
86 | { | |
87 | 4 | mods.addedEntries.put(key, post.get(key)); |
88 | } | |
89 | } | |
90 | ||
91 | // now the removed entries. | |
92 | 7 | for (Object key : pre.keySet()) |
93 | { | |
94 | 15 | if (!post.containsKey(key)) |
95 | { | |
96 | 4 | mods.removedEntries.put(key, pre.get(key)); |
97 | } | |
98 | } | |
99 | ||
100 | 6 | 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<Object, Object> addedEntries = new HashMap<Object, Object>(); | |
109 | public final Map<Object, Object> removedEntries = new HashMap<Object, Object>(); | |
110 | public final Map<Object, Object> modifiedEntries = new HashMap<Object, Object>(); | |
111 | ||
112 | ||
113 | 6 | public boolean equals(Object o) |
114 | { | |
115 | 0 | if (this == o) return true; |
116 | 0 | if (o == null || getClass() != o.getClass()) return false; |
117 | ||
118 | 6 | MapModifications that = (MapModifications) o; |
119 | ||
120 | 0 | if (addedEntries != null ? !addedEntries.equals(that.addedEntries) : that.addedEntries != null) return false; |
121 | 6 | if (modifiedEntries != null ? !modifiedEntries.equals(that.modifiedEntries) : that.modifiedEntries != null) |
122 | 0 | return false; |
123 | 6 | if (removedEntries != null ? !removedEntries.equals(that.removedEntries) : that.removedEntries != null) |
124 | 0 | return false; |
125 | ||
126 | 6 | return true; |
127 | } | |
128 | ||
129 | 0 | public int hashCode() |
130 | { | |
131 | 0 | int result; |
132 | 0 | result = (addedEntries != null ? addedEntries.hashCode() : 0); |
133 | 0 | result = 31 * result + (removedEntries != null ? removedEntries.hashCode() : 0); |
134 | 0 | result = 31 * result + (modifiedEntries != null ? modifiedEntries.hashCode() : 0); |
135 | 0 | return result; |
136 | } | |
137 | ||
138 | 0 | public String toString() |
139 | { | |
140 | 0 | return "Added Entries " + addedEntries + " Removeed Entries " + removedEntries + " Modified Entries " + modifiedEntries; |
141 | } | |
142 | } | |
143 | ||
144 | } |
|