1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.error;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.IdentityHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import com.allanbank.mongodb.builder.BatchedWrite;
31 import com.allanbank.mongodb.builder.write.WriteOperation;
32
33
34
35
36
37
38
39 public class BatchedWriteException extends ReplyException {
40
41
42 private static final long serialVersionUID = -7797670480003384909L;
43
44
45
46
47
48
49
50
51 private static String createErrorMessage(
52 final Map<WriteOperation, Throwable> errors) {
53 final StringBuilder builder = new StringBuilder();
54 if (errors.size() == 1) {
55 builder.append("Error sending a batched write: ");
56 builder.append(errors.values().iterator().next().getMessage());
57 }
58 else {
59 builder.append(errors.size());
60 builder.append(" errors sending a batched write. Unique error messages:\n\t");
61
62 final Set<String> unique = new HashSet<String>();
63 for (final Throwable error : errors.values()) {
64 final String message = error.getMessage();
65 if (unique.add(message)) {
66 builder.append(message);
67 builder.append("\n\t");
68 }
69 }
70 builder.setLength(builder.length() - 2);
71 }
72
73 return builder.toString();
74 }
75
76
77 private final Map<WriteOperation, Throwable> myErrors;
78
79
80 private final long myN;
81
82
83 private final List<WriteOperation> mySkipped;
84
85
86 private final BatchedWrite myWrite;
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public BatchedWriteException(final BatchedWrite write, final long n,
101 final List<WriteOperation> skipped,
102 final Map<WriteOperation, Throwable> errors) {
103 super(null, createErrorMessage(errors));
104
105 myWrite = write;
106 myN = n;
107 mySkipped = Collections.unmodifiableList(new ArrayList<WriteOperation>(
108 skipped));
109 myErrors = Collections
110 .unmodifiableMap(new IdentityHashMap<WriteOperation, Throwable>(
111 errors));
112 }
113
114
115
116
117
118
119 public Map<WriteOperation, Throwable> getErrors() {
120 return myErrors;
121 }
122
123
124
125
126
127
128 public long getN() {
129 return myN;
130 }
131
132
133
134
135
136
137 public List<WriteOperation> getSkipped() {
138 return mySkipped;
139 }
140
141
142
143
144
145
146 public BatchedWrite getWrite() {
147 return myWrite;
148 }
149 }