Skip to content

Commit 713c413

Browse files
committed
metafacture-elasticsearch/ (main): Fix Checkstyle violations.
1 parent 823fdde commit 713c413

File tree

2 files changed

+82
-68
lines changed

2 files changed

+82
-68
lines changed

metafacture-elasticsearch/src/main/java/org/metafacture/elasticsearch/JsonToElasticsearchBulk.java

Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.metafacture.elasticsearch;
1718

19+
import org.metafacture.framework.FluxCommand;
20+
import org.metafacture.framework.ObjectReceiver;
21+
import org.metafacture.framework.annotations.In;
22+
import org.metafacture.framework.annotations.Out;
23+
import org.metafacture.framework.helpers.DefaultObjectPipe;
24+
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
1827
import java.io.IOException;
1928
import java.io.StringWriter;
2029
import java.util.Arrays;
@@ -24,14 +33,6 @@
2433
import java.util.Set;
2534
import java.util.regex.Pattern;
2635

27-
import org.metafacture.framework.FluxCommand;
28-
import org.metafacture.framework.ObjectReceiver;
29-
import org.metafacture.framework.annotations.In;
30-
import org.metafacture.framework.annotations.Out;
31-
import org.metafacture.framework.helpers.DefaultObjectPipe;
32-
33-
import com.fasterxml.jackson.databind.ObjectMapper;
34-
3536
/**
3637
* Add Elasticsearch bulk indexing metadata to JSON input.
3738
*
@@ -42,56 +43,14 @@
4243
@In(String.class)
4344
@Out(String.class)
4445
@FluxCommand("json-to-elasticsearch-bulk")
45-
public class JsonToElasticsearchBulk extends
46-
DefaultObjectPipe<String, ObjectReceiver<String>> {
47-
48-
/**
49-
* Use a MultiMap with Jackson to collect values from multiple fields with
50-
* identical names under a single key.
51-
*/
52-
static class MultiMap extends HashMap<String, Object> {
53-
private static final long serialVersionUID = 490682490432334605L;
54-
55-
MultiMap() {
56-
// default constructor for Jackson
57-
}
58-
59-
@Override
60-
public Object put(String key, Object value) {
61-
if (containsKey(key)) {
62-
Object oldValue = get(key);
63-
if (oldValue instanceof Set) {
64-
@SuppressWarnings("unchecked")
65-
Set<Object> vals = ((Set<Object>) oldValue);
66-
vals.add(value);
67-
return super.put(key, vals);
68-
}
69-
HashSet<Object> set = new HashSet<>(Arrays.asList(oldValue, value));
70-
return super.put(key, set.size() == 1 ? value : set);
71-
}
72-
return super.put(key, value);
73-
}
74-
}
46+
public class JsonToElasticsearchBulk extends DefaultObjectPipe<String, ObjectReceiver<String>> {
7547

7648
private ObjectMapper mapper = new ObjectMapper();
7749
private String[] idPath;
7850
private String type;
7951
private String index;
8052

81-
public void setIdKey(String idKey) {
82-
this.idPath = new String[]{idKey};
83-
}
84-
85-
public void setType(String type) {
86-
this.type = type;
87-
}
88-
89-
public void setIndex(String index) {
90-
this.index = index;
91-
}
92-
9353
public JsonToElasticsearchBulk() {
94-
super();
9554
this.idPath = new String[]{};
9655
this.type = null;
9756
this.index = null;
@@ -103,16 +62,16 @@ public JsonToElasticsearchBulk() {
10362
* @param type The Elasticsearch index type
10463
* @param index The Elasticsearch index name
10564
*/
106-
public JsonToElasticsearchBulk(String type, String index) {
107-
this(new String[] { }, type, index);
65+
public JsonToElasticsearchBulk(final String type, final String index) {
66+
this(new String[] {}, type, index);
10867
}
10968

11069
/**
11170
* @param idPath The key path of the JSON value to be used as the ID for the record
11271
* @param type The Elasticsearch index type
11372
* @param index The Elasticsearch index name
11473
*/
115-
public JsonToElasticsearchBulk(String[] idPath, String type, String index) {
74+
public JsonToElasticsearchBulk(final String[] idPath, final String type, final String index) {
11675
this.idPath = idPath;
11776
this.type = type;
11877
this.index = index;
@@ -123,7 +82,7 @@ public JsonToElasticsearchBulk(String[] idPath, String type, String index) {
12382
* @param type The Elasticsearch index type
12483
* @param index The Elasticsearch index name
12584
*/
126-
public JsonToElasticsearchBulk(String idKey, String type, String index) {
85+
public JsonToElasticsearchBulk(final String idKey, final String type, final String index) {
12786
this(new String[]{idKey}, type, index);
12887
}
12988

@@ -133,42 +92,96 @@ public JsonToElasticsearchBulk(String idKey, String type, String index) {
13392
* @param index The Elasticsearch index name
13493
* @param entitySeparator The separator between entity names in idKey
13594
*/
136-
public JsonToElasticsearchBulk(String idKey, String type, String index, String entitySeparator) {
95+
public JsonToElasticsearchBulk(final String idKey, final String type, final String index, final String entitySeparator) {
13796
this(idKey.split(Pattern.quote(entitySeparator)), type, index);
13897
}
13998

99+
public void setIdKey(final String idKey) {
100+
this.idPath = new String[]{idKey};
101+
}
102+
103+
public void setType(final String type) {
104+
this.type = type;
105+
}
106+
107+
public void setIndex(final String index) {
108+
this.index = index;
109+
}
110+
140111
@Override
141-
public void process(String obj) {
142-
StringWriter stringWriter = new StringWriter();
112+
public void process(final String obj) {
113+
final StringWriter stringWriter = new StringWriter();
143114
try {
144-
Map<String, Object> json = mapper.readValue(obj, MultiMap.class);
145-
Map<String, Object> detailsMap = new HashMap<String, Object>();
146-
Map<String, Object> indexMap = new HashMap<String, Object>();
115+
final Map<String, Object> json = mapper.readValue(obj, MultiMap.class);
116+
final Map<String, Object> detailsMap = new HashMap<String, Object>();
117+
final Map<String, Object> indexMap = new HashMap<String, Object>();
147118
indexMap.put("index", detailsMap);
148-
if (idPath.length > 0) detailsMap.put("_id", findId(json));
119+
if (idPath.length > 0) {
120+
detailsMap.put("_id", findId(json));
121+
}
149122
detailsMap.put("_type", type);
150123
detailsMap.put("_index", index);
151124
mapper.writeValue(stringWriter, indexMap);
152125
stringWriter.write("\n");
153126
mapper.writeValue(stringWriter, json);
154-
} catch (IOException e) {
127+
}
128+
catch (final IOException e) {
155129
e.printStackTrace();
156130
}
157131
getReceiver().process(stringWriter.toString());
158132
}
159133

160-
private Object findId(Object value) {
134+
private Object findId(final Object value) {
135+
Object newValue = value;
136+
161137
for (final String key : idPath) {
162-
if (value instanceof Map) {
138+
if (newValue instanceof Map) {
163139
@SuppressWarnings("unchecked")
164-
final Map<String, Object> nestedMap = (Map<String, Object>) value;
165-
value = nestedMap.get(key);
140+
final Map<String, Object> nestedMap = (Map<String, Object>) newValue;
141+
newValue = nestedMap.get(key);
166142
}
167143
else {
168144
return null;
169145
}
170146
}
171147

172-
return value;
148+
return newValue;
149+
}
150+
151+
/**
152+
* Use a MultiMap with Jackson to collect values from multiple fields with
153+
* identical names under a single key.
154+
*/
155+
static class MultiMap extends HashMap<String, Object> { // checkstyle-disable-line IllegalType
156+
private static final long serialVersionUID = 490682490432334605L;
157+
158+
MultiMap() {
159+
// default constructor for Jackson
160+
}
161+
162+
@Override
163+
public Object put(final String key, final Object value) {
164+
final Object newValue;
165+
166+
if (containsKey(key)) {
167+
final Object oldValue = get(key);
168+
if (oldValue instanceof Set) {
169+
@SuppressWarnings("unchecked")
170+
final Set<Object> vals = (Set<Object>) oldValue;
171+
vals.add(value);
172+
newValue = vals;
173+
}
174+
else {
175+
final Set<Object> set = new HashSet<>(Arrays.asList(oldValue, value));
176+
newValue = set.size() == 1 ? value : set;
177+
}
178+
}
179+
else {
180+
newValue = value;
181+
}
182+
183+
return super.put(key, newValue);
184+
}
173185
}
186+
174187
}

metafacture-elasticsearch/src/test/java/org/metafacture/elasticsearch/JsonToElasticsearchBulkTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.metafacture.elasticsearch;
1718

1819
import static org.mockito.Mockito.verify;

0 commit comments

Comments
 (0)