View Javadoc
1   package org.dbunit.assertion.comparer.value.builder;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.dbunit.assertion.comparer.value.ValueComparer;
8   
9   /**
10   * Convenience methods to help build the map of table name -> map of column name
11   * -> {@link ValueComparer}.
12   *
13   * @author jjensen
14   * @since 2.6.0
15   */
16  public class TableColumnValueComparerMapBuilder
17  {
18      private Map<String, Map<String, ValueComparer>> comparers =
19              new HashMap<String, Map<String, ValueComparer>>();
20  
21      /**
22       * Add all mappings from the specified table map to this builder.
23       *
24       * @return this for fluent syntax.
25       */
26      public TableColumnValueComparerMapBuilder add(
27              final Map<String, Map<String, ValueComparer>> tableColumnValueComparers)
28      {
29          comparers.putAll(tableColumnValueComparers);
30  
31          return this;
32      }
33  
34      /**
35       * Add all mappings from the specified
36       * {@link TableColumnValueComparerMapBuilder} builder to this builder.
37       *
38       * @return this for fluent syntax.
39       */
40      public TableColumnValueComparerMapBuilder add(
41              final TableColumnValueComparerMapBuilder tableColumnValueComparerMapBuilder)
42      {
43          final Map<String, Map<String, ValueComparer>> map =
44                  tableColumnValueComparerMapBuilder.build();
45          comparers.putAll(map);
46  
47          return this;
48      }
49  
50      /**
51       * Add all mappings from the specified column map to a column map for the
52       * specified table in this builder.
53       *
54       * @return this for fluent syntax.
55       */
56      public TableColumnValueComparerMapBuilder add(final String tableName,
57              final Map<String, ValueComparer> columnValueComparers)
58      {
59          final Map<String, ValueComparer> map = findOrMakeColumnMap(tableName);
60  
61          map.putAll(columnValueComparers);
62  
63          return this;
64      }
65  
66      /**
67       * Add all mappings from the specified {@link ColumnValueComparerMapBuilder}
68       * builder to a column map for the specified table in this builder.
69       *
70       * @return this for fluent syntax.
71       */
72      public TableColumnValueComparerMapBuilder add(final String tableName,
73              final ColumnValueComparerMapBuilder columnValueComparerMapBuilder)
74      {
75          final Map<String, ValueComparer> map = findOrMakeColumnMap(tableName);
76          final Map<String, ValueComparer> columnMap =
77                  columnValueComparerMapBuilder.build();
78  
79          map.putAll(columnMap);
80  
81          return this;
82      }
83  
84      /**
85       * Add a table to column to {@link ValueComparer} mapping.
86       *
87       * @return this for fluent syntax.
88       */
89      public TableColumnValueComparerMapBuilder add(final String tableName,
90              final String columnName, final ValueComparer valueComparer)
91      {
92          final Map<String, ValueComparer> map = findOrMakeColumnMap(tableName);
93          map.put(columnName, valueComparer);
94  
95          return this;
96      }
97  
98      /** @return The unmodifiable assembled map. */
99      public Map<String, Map<String, ValueComparer>> build()
100     {
101         return Collections.unmodifiableMap(comparers);
102     }
103 
104     protected Map<String, ValueComparer> findOrMakeColumnMap(
105             final String tableName)
106     {
107         Map<String, ValueComparer> map = comparers.get(tableName);
108         if (map == null)
109         {
110             map = makeColumnToValueComparerMap();
111             comparers.put(tableName, map);
112         }
113 
114         return map;
115     }
116 
117     protected Map<String, ValueComparer> makeColumnToValueComparerMap()
118     {
119         return new HashMap<String, ValueComparer>();
120     }
121 }