01: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.services.persondir.support.merger;
07:
08: import java.util.ArrayList;
09: import java.util.HashMap;
10: import java.util.Iterator;
11: import java.util.List;
12: import java.util.Map;
13:
14: /**
15: * Merger that retains values from both maps. If a value exists for
16: * a key in both maps the following is done:
17: * <ul>
18: * <li>If both maps have a {@link List} they are merged into a single {@link List}</li>
19: * <li>If one map has a {@link List} and the other a single value the value is added to the {@link List}</li>
20: * <li>If both maps have a single value a {@link List} is created from the two.</li>
21: * </ul>
22: *
23: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
24: * @version $Revision: 35132 $ $Date: 2004-12-13 20:12:19 -0700 (Mon, 13 Dec 2004) $
25: */
26: public class MultivaluedAttributeMerger implements IAttributeMerger {
27:
28: /**
29: * Please note that the <code>dest</code> map is modified.
30: *
31: * @see org.jasig.portal.services.persondir.support.merger.IAttributeMerger#mergeAttributes(java.util.Map, java.util.Map)
32: */
33: public Map mergeAttributes(final Map dest, final Map source) {
34: if (dest == null)
35: throw new IllegalArgumentException(
36: "toModify cannot be null");
37: if (source == null)
38: throw new IllegalArgumentException(
39: "toConsider cannot be null");
40:
41: final Map sourceCopy = new HashMap(source);
42:
43: //Iterate through the dest Map
44: for (final Iterator destKeyItr = dest.keySet().iterator(); destKeyItr
45: .hasNext();) {
46: final Object destKey = destKeyItr.next();
47: final Object sourceValue = sourceCopy.remove(destKey);
48:
49: //If there is a corresponding entry in source
50: if (sourceValue != null) {
51: //Get the dest entry
52: final Object destValue = dest.get(destKey);
53:
54: //If they are both Lists merge them
55: if (destValue instanceof List
56: && sourceValue instanceof List) {
57: final List destList = (List) destValue;
58: final List sourceList = (List) sourceValue;
59:
60: destList.addAll(sourceList);
61:
62: dest.put(destKey, destList);
63: }
64: //If dest value is a List add the source value to it
65: else if (destValue instanceof List) {
66: final List destList = (List) destValue;
67:
68: destList.add(sourceValue);
69:
70: dest.put(destKey, destList);
71: }
72: //If the source value is a List add the dest value to it
73: else if (sourceValue instanceof List) {
74: final List sourceList = (List) sourceValue;
75:
76: sourceList.add(destValue);
77:
78: dest.put(destKey, sourceList);
79: }
80: //Neither are a List, create a new List
81: else {
82: final List newList = new ArrayList(2);
83:
84: newList.add(destValue);
85: newList.add(sourceValue);
86:
87: dest.put(destKey, newList);
88: }
89: }
90: }
91:
92: //Add the remaining items from source to dest
93: dest.putAll(sourceCopy);
94:
95: return dest;
96: }
97:
98: }
|