Skip to content

Commit 9ef1fc7

Browse files
committed
DATAMONGO-1337 - Another round of polishes on SonarQuber complaints.
1 parent cf3a9d3 commit 9ef1fc7

File tree

11 files changed

+88
-71
lines changed

11 files changed

+88
-71
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mongodb.core.aggregation;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.List;
2021

2122
import org.springframework.util.Assert;
@@ -56,7 +57,7 @@ public AggregationExpression of(Object... parameters) {
5657
static class FunctionExpression implements AggregationExpression {
5758

5859
private final String name;
59-
private final Object[] values;
60+
private final List<Object> values;
6061

6162
/**
6263
* Creates a new {@link FunctionExpression} for the given name and values.
@@ -70,7 +71,7 @@ public FunctionExpression(String name, Object[] values) {
7071
Assert.notNull(values, "Values must not be null!");
7172

7273
this.name = name;
73-
this.values = values;
74+
this.values = Arrays.asList(values);
7475
}
7576

7677
/*
@@ -80,10 +81,10 @@ public FunctionExpression(String name, Object[] values) {
8081
@Override
8182
public DBObject toDbObject(AggregationOperationContext context) {
8283

83-
List<Object> args = new ArrayList<Object>(values.length);
84+
List<Object> args = new ArrayList<Object>(values.size());
8485

85-
for (int i = 0; i < values.length; i++) {
86-
args.add(unpack(values[i], context));
86+
for (Object value : values) {
87+
args.add(unpack(value, context));
8788
}
8889

8990
return new BasicDBObject("$" + name, args);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ static enum DbObjectToPointConverter implements Converter<DBObject, Point> {
111111
@Override
112112
public Point convert(DBObject source) {
113113

114+
if (source == null) {
115+
return null;
116+
}
117+
114118
Assert.isTrue(source.keySet().size() == 2, "Source must contain 2 elements");
115119

116-
return source == null ? null : new Point((Double) source.get("x"), (Double) source.get("y"));
120+
return new Point((Double) source.get("x"), (Double) source.get("y"));
117121
}
118122
}
119123

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

-4
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,6 @@ private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type
11981198

11991199
Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
12001200

1201-
if (object != null) {
1202-
return (T) object;
1203-
}
1204-
12051201
return (T) (object != null ? object : read(type, readRef(dbref), path));
12061202
}
12071203

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ public KeyMapper(String key) {
946946
*/
947947
protected String mapPropertyName(MongoPersistentProperty property) {
948948

949-
String mappedName = PropertyToFieldNameConverter.INSTANCE.convert(property);
949+
StringBuilder mappedName = new StringBuilder(PropertyToFieldNameConverter.INSTANCE.convert(property));
950950
boolean inspect = iterator.hasNext();
951951

952952
while (inspect) {
@@ -955,13 +955,13 @@ protected String mapPropertyName(MongoPersistentProperty property) {
955955
boolean isPositional = (isPositionalParameter(partial) && (property.isMap() || property.isCollectionLike()));
956956

957957
if (isPositional) {
958-
mappedName += "." + partial;
958+
mappedName.append(".").append(partial);
959959
}
960960

961961
inspect = isPositional && iterator.hasNext();
962962
}
963963

964-
return mappedName;
964+
return mappedName.toString();
965965
}
966966

967967
private static boolean isPositionalParameter(String partial) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import java.util.LinkedHashMap;
1919
import java.util.Map;
20+
import java.util.Map.Entry;
2021
import java.util.concurrent.TimeUnit;
2122

2223
import org.springframework.data.domain.Sort.Direction;
@@ -44,7 +45,7 @@ public enum Duplicates {
4445
*
4546
* @deprecated since 1.7.
4647
*/
47-
@Deprecated//
48+
@Deprecated //
4849
DROP
4950
}
5051

@@ -175,11 +176,18 @@ public Index unique(Duplicates duplicates) {
175176
return unique();
176177
}
177178

179+
/*
180+
* (non-Javadoc)
181+
* @see org.springframework.data.mongodb.core.index.IndexDefinition#getIndexKeys()
182+
*/
178183
public DBObject getIndexKeys() {
184+
179185
DBObject dbo = new BasicDBObject();
180-
for (String k : fieldSpec.keySet()) {
181-
dbo.put(k, fieldSpec.get(k).equals(Direction.ASC) ? 1 : -1);
186+
187+
for (Entry<String, Direction> entry : fieldSpec.entrySet()) {
188+
dbo.put(entry.getKey(), Direction.ASC.equals(entry.getValue()) ? 1 : -1);
182189
}
190+
183191
return dbo;
184192
}
185193

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTiming.java

+31-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,10 @@
1717

1818
public class MapReduceTiming {
1919

20-
private long mapTime;
21-
22-
private long emitLoopTime;
23-
24-
private long totalTime;
20+
private long mapTime, emitLoopTime, totalTime;
2521

2622
public MapReduceTiming(long mapTime, long emitLoopTime, long totalTime) {
23+
2724
this.mapTime = mapTime;
2825
this.emitLoopTime = emitLoopTime;
2926
this.totalTime = totalTime;
@@ -41,37 +38,52 @@ public long getTotalTime() {
4138
return totalTime;
4239
}
4340

41+
/*
42+
* (non-Javadoc)
43+
* @see java.lang.Object#toString()
44+
*/
4445
@Override
4546
public String toString() {
4647
return "MapReduceTiming [mapTime=" + mapTime + ", emitLoopTime=" + emitLoopTime + ", totalTime=" + totalTime + "]";
4748
}
4849

50+
/*
51+
* (non-Javadoc)
52+
* @see java.lang.Object#hashCode()
53+
*/
4954
@Override
5055
public int hashCode() {
56+
5157
final int prime = 31;
5258
int result = 1;
59+
5360
result = prime * result + (int) (emitLoopTime ^ (emitLoopTime >>> 32));
5461
result = prime * result + (int) (mapTime ^ (mapTime >>> 32));
5562
result = prime * result + (int) (totalTime ^ (totalTime >>> 32));
63+
5664
return result;
5765
}
5866

67+
/*
68+
*
69+
* (non-Javadoc)
70+
* @see java.lang.Object#equals(java.lang.Object)
71+
*/
5972
@Override
6073
public boolean equals(Object obj) {
61-
if (this == obj)
74+
75+
if (this == obj) {
6276
return true;
63-
if (obj == null)
64-
return false;
65-
if (getClass() != obj.getClass())
66-
return false;
67-
MapReduceTiming other = (MapReduceTiming) obj;
68-
if (emitLoopTime != other.emitLoopTime)
69-
return false;
70-
if (mapTime != other.mapTime)
71-
return false;
72-
if (totalTime != other.totalTime)
77+
}
78+
79+
if (!(obj instanceof MapReduceTiming)) {
7380
return false;
74-
return true;
75-
}
81+
}
82+
83+
MapReduceTiming that = (MapReduceTiming) obj;
7684

85+
return this.emitLoopTime == that.emitLoopTime && //
86+
this.mapTime == that.mapTime && //
87+
this.totalTime == that.totalTime;
88+
}
7789
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collection;
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
25+
import java.util.Map.Entry;
2526
import java.util.regex.Pattern;
2627

2728
import org.bson.BSON;
@@ -581,24 +582,25 @@ protected DBObject getSingleCriteriaObject() {
581582
DBObject dbo = new BasicDBObject();
582583
boolean not = false;
583584

584-
for (String k : this.criteria.keySet()) {
585+
for (Entry<String, Object> entry : criteria.entrySet()) {
585586

586-
Object value = this.criteria.get(k);
587+
String key = entry.getKey();
588+
Object value = entry.getValue();
587589

588590
if (requiresGeoJsonFormat(value)) {
589591
value = new BasicDBObject("$geometry", value);
590592
}
591593

592594
if (not) {
593595
DBObject notDbo = new BasicDBObject();
594-
notDbo.put(k, value);
596+
notDbo.put(key, value);
595597
dbo.put("$not", notDbo);
596598
not = false;
597599
} else {
598-
if ("$not".equals(k) && value == null) {
600+
if ("$not".equals(key) && value == null) {
599601
not = true;
600602
} else {
601-
dbo.put(k, value);
603+
dbo.put(key, value);
602604
}
603605
}
604606
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,14 +83,10 @@ public Field position(String field, int value) {
8383

8484
public DBObject getFieldsObject() {
8585

86-
DBObject dbo = new BasicDBObject();
86+
DBObject dbo = new BasicDBObject(criteria);
8787

88-
for (String k : criteria.keySet()) {
89-
dbo.put(k, criteria.get(k));
90-
}
91-
92-
for (String k : slices.keySet()) {
93-
dbo.put(k, new BasicDBObject("$slice", slices.get(k)));
88+
for (Entry<String, Object> entry : slices.entrySet()) {
89+
dbo.put(entry.getKey(), new BasicDBObject("$slice", entry.getValue()));
9490
}
9591

9692
for (Entry<String, Criteria> entry : elemMatchs.entrySet()) {
@@ -134,8 +130,8 @@ public boolean equals(Object object) {
134130
return false;
135131
}
136132

137-
boolean samePositionKey = this.postionKey == null ? that.postionKey == null : this.postionKey
138-
.equals(that.postionKey);
133+
boolean samePositionKey = this.postionKey == null ? that.postionKey == null
134+
: this.postionKey.equals(that.postionKey);
139135
boolean samePositionValue = this.positionValue == that.positionValue;
140136

141137
return samePositionKey && samePositionValue;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,9 +94,9 @@ public Query addCriteria(CriteriaDefinition criteriaDefinition) {
9494
if (existing == null) {
9595
this.criteria.put(key, criteriaDefinition);
9696
} else {
97-
throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, "
98-
+ "you can't add a second '" + key + "' criteria. " + "Query already contains '"
99-
+ existing.getCriteriaObject() + "'.");
97+
throw new InvalidMongoDbApiUsageException(
98+
"Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. "
99+
+ "Query already contains '" + existing.getCriteriaObject() + "'.");
100100
}
101101

102102
return this;
@@ -221,10 +221,8 @@ public DBObject getQueryObject() {
221221

222222
DBObject dbo = new BasicDBObject();
223223

224-
for (String k : criteria.keySet()) {
225-
CriteriaDefinition c = criteria.get(k);
226-
DBObject cl = c.getCriteriaObject();
227-
dbo.putAll(cl);
224+
for (CriteriaDefinition definition : criteria.values()) {
225+
dbo.putAll(definition.getCriteriaObject());
228226
}
229227

230228
if (!restrictedTypes.isEmpty()) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -327,12 +327,7 @@ public BitwiseOperatorBuilder bitwise(String key) {
327327
}
328328

329329
public DBObject getUpdateObject() {
330-
331-
DBObject dbo = new BasicDBObject();
332-
for (String k : modifierOps.keySet()) {
333-
dbo.put(k, modifierOps.get(k));
334-
}
335-
return dbo;
330+
return new BasicDBObject(modifierOps);
336331
}
337332

338333
protected void addFieldOperation(String operator, String key, Object value) {
@@ -355,8 +350,8 @@ protected void addMultiFieldOperation(String operator, String key, Object value)
355350
if (existingValue instanceof BasicDBObject) {
356351
keyValueMap = (BasicDBObject) existingValue;
357352
} else {
358-
throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was "
359-
+ existingValue.getClass());
353+
throw new InvalidDataAccessApiUsageException(
354+
"Modifier Operations should be a LinkedHashMap but was " + existingValue.getClass());
360355
}
361356
}
362357

0 commit comments

Comments
 (0)