View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.beanmerger;
35  
36  import info.magnolia.cms.core.Content;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.jcr.Node;
44  
45  import org.apache.commons.collections.CollectionUtils;
46  import org.apache.commons.collections.functors.NotNullPredicate;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * Base implementation of interface BeanMerge.
53   *
54   * @version $Id: BeanMergerBase.java 51486 2011-11-15 09:27:41Z tmattsson $
55   */
56  public abstract class BeanMergerBase implements BeanMerger {
57  
58      protected Logger log = LoggerFactory.getLogger(this.getClass());
59  
60      @Override
61      public Object merge(List sources) {
62          CollectionUtils.filter(sources, NotNullPredicate.INSTANCE);
63          if (sources.isEmpty()) {
64              return null;
65          }
66  
67          if (sources.size() == 1 || isSimpleType(sources.get(0).getClass())) {
68              return sources.get(0);
69          }
70  
71          if (sources.get(0) instanceof Collection) {
72              return mergeCollections(sources);
73          }
74  
75          if (sources.get(0) instanceof Map) {
76              return mergeMaps(sources);
77          }
78  
79          return mergeBean(sources);
80      }
81  
82      protected abstract Object mergeBean(List sources);
83  
84      protected Map mergeMaps(List<Map> sources) {
85          Map mergedMap;
86          try {
87              mergedMap = sources.get(0).getClass().newInstance();
88  
89              for (Map map : sources) {
90                  for (Object key : map.keySet()) {
91                      List values = new ArrayList();
92                      for (Map map2 : sources) {
93                          if (map2.containsKey(key)) {
94                              values.add(map2.get(key));
95                          }
96                      }
97                      mergedMap.put(key, merge(values));
98                  }
99              }
100             return mergedMap;
101         }
102         catch (Exception e) {
103             log.error("", e);
104             return sources.get(0);
105         }
106     }
107 
108     protected Collection mergeCollections(List<Collection> sources) {
109         try {
110             Collection mergedCol = sources.get(0).getClass().newInstance();
111             for (Collection col : sources) {
112                 mergedCol.addAll(col);
113             }
114             return mergedCol;
115         }
116         catch (Exception e) {
117             log.error("", e);
118             return sources.get(0);
119         }
120     }
121 
122     protected boolean isSimpleType(Class type) {
123         return type.isPrimitive() ||
124             type == Boolean.class ||
125             type == Byte.class ||
126             type == Character.class ||
127             type == Short.class ||
128             type == Integer.class ||
129             type == Long.class ||
130             type == Float.class ||
131             type == Double.class ||
132             type == Void.class ||
133             // well, at least property wise
134             type == String.class ||
135             type == Class.class ||
136             // not mergable
137             Content.class.isAssignableFrom(type) ||
138             Node.class.isAssignableFrom(type);
139     }
140 
141 }