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