Browse Source

DATAMONGO-1337 - Another round of polishes on SonarQuber complaints.

pull/663/head
Oliver Gierke 10 years ago
parent
commit
5832055840
  1. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java
  2. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java
  3. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  4. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  5. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java
  6. 48
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTiming.java
  7. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java
  8. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java
  9. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java
  10. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  11. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java

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

@ -16,6 +16,7 @@
package org.springframework.data.mongodb.core.aggregation; package org.springframework.data.mongodb.core.aggregation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -56,7 +57,7 @@ public enum AggregationFunctionExpressions {
static class FunctionExpression implements AggregationExpression { static class FunctionExpression implements AggregationExpression {
private final String name; private final String name;
private final Object[] values; private final List<Object> values;
/** /**
* Creates a new {@link FunctionExpression} for the given name and values. * Creates a new {@link FunctionExpression} for the given name and values.
@ -70,7 +71,7 @@ public enum AggregationFunctionExpressions {
Assert.notNull(values, "Values must not be null!"); Assert.notNull(values, "Values must not be null!");
this.name = name; this.name = name;
this.values = values; this.values = Arrays.asList(values);
} }
/* /*
@ -80,10 +81,10 @@ public enum AggregationFunctionExpressions {
@Override @Override
public DBObject toDbObject(AggregationOperationContext context) { public DBObject toDbObject(AggregationOperationContext context) {
List<Object> args = new ArrayList<Object>(values.length); List<Object> args = new ArrayList<Object>(values.size());
for (int i = 0; i < values.length; i++) { for (Object value : values) {
args.add(unpack(values[i], context)); args.add(unpack(value, context));
} }
return new BasicDBObject("$" + name, args); return new BasicDBObject("$" + name, args);

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

@ -111,9 +111,13 @@ abstract class GeoConverters {
@Override @Override
public Point convert(DBObject source) { public Point convert(DBObject source) {
if (source == null) {
return null;
}
Assert.isTrue(source.keySet().size() == 2, "Source must contain 2 elements"); Assert.isTrue(source.keySet().size() == 2, "Source must contain 2 elements");
return source == null ? null : new Point((Double) source.get("x"), (Double) source.get("y")); return new Point((Double) source.get("x"), (Double) source.get("y"));
} }
} }

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

@ -1198,10 +1198,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName()); Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
if (object != null) {
return (T) object;
}
return (T) (object != null ? object : read(type, readRef(dbref), path)); return (T) (object != null ? object : read(type, readRef(dbref), path));
} }

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

@ -946,7 +946,7 @@ public class QueryMapper {
*/ */
protected String mapPropertyName(MongoPersistentProperty property) { protected String mapPropertyName(MongoPersistentProperty property) {
String mappedName = PropertyToFieldNameConverter.INSTANCE.convert(property); StringBuilder mappedName = new StringBuilder(PropertyToFieldNameConverter.INSTANCE.convert(property));
boolean inspect = iterator.hasNext(); boolean inspect = iterator.hasNext();
while (inspect) { while (inspect) {
@ -955,13 +955,13 @@ public class QueryMapper {
boolean isPositional = (isPositionalParameter(partial) && (property.isMap() || property.isCollectionLike())); boolean isPositional = (isPositionalParameter(partial) && (property.isMap() || property.isCollectionLike()));
if (isPositional) { if (isPositional) {
mappedName += "." + partial; mappedName.append(".").append(partial);
} }
inspect = isPositional && iterator.hasNext(); inspect = isPositional && iterator.hasNext();
} }
return mappedName; return mappedName.toString();
} }
private static boolean isPositionalParameter(String partial) { private static boolean isPositionalParameter(String partial) {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2014 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.index;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
@ -175,11 +176,18 @@ public class Index implements IndexDefinition {
return unique(); return unique();
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.index.IndexDefinition#getIndexKeys()
*/
public DBObject getIndexKeys() { public DBObject getIndexKeys() {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
for (String k : fieldSpec.keySet()) {
dbo.put(k, fieldSpec.get(k).equals(Direction.ASC) ? 1 : -1); for (Entry<String, Direction> entry : fieldSpec.entrySet()) {
dbo.put(entry.getKey(), Direction.ASC.equals(entry.getValue()) ? 1 : -1);
} }
return dbo; return dbo;
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,13 +17,10 @@ package org.springframework.data.mongodb.core.mapreduce;
public class MapReduceTiming { public class MapReduceTiming {
private long mapTime; private long mapTime, emitLoopTime, totalTime;
private long emitLoopTime;
private long totalTime;
public MapReduceTiming(long mapTime, long emitLoopTime, long totalTime) { public MapReduceTiming(long mapTime, long emitLoopTime, long totalTime) {
this.mapTime = mapTime; this.mapTime = mapTime;
this.emitLoopTime = emitLoopTime; this.emitLoopTime = emitLoopTime;
this.totalTime = totalTime; this.totalTime = totalTime;
@ -41,37 +38,52 @@ public class MapReduceTiming {
return totalTime; return totalTime;
} }
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override @Override
public String toString() { public String toString() {
return "MapReduceTiming [mapTime=" + mapTime + ", emitLoopTime=" + emitLoopTime + ", totalTime=" + totalTime + "]"; return "MapReduceTiming [mapTime=" + mapTime + ", emitLoopTime=" + emitLoopTime + ", totalTime=" + totalTime + "]";
} }
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + (int) (emitLoopTime ^ (emitLoopTime >>> 32)); result = prime * result + (int) (emitLoopTime ^ (emitLoopTime >>> 32));
result = prime * result + (int) (mapTime ^ (mapTime >>> 32)); result = prime * result + (int) (mapTime ^ (mapTime >>> 32));
result = prime * result + (int) (totalTime ^ (totalTime >>> 32)); result = prime * result + (int) (totalTime ^ (totalTime >>> 32));
return result; return result;
} }
/*
*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true; return true;
if (obj == null) }
return false;
if (getClass() != obj.getClass()) if (!(obj instanceof MapReduceTiming)) {
return false;
MapReduceTiming other = (MapReduceTiming) obj;
if (emitLoopTime != other.emitLoopTime)
return false;
if (mapTime != other.mapTime)
return false;
if (totalTime != other.totalTime)
return false; return false;
return true;
} }
MapReduceTiming that = (MapReduceTiming) obj;
return this.emitLoopTime == that.emitLoopTime && //
this.mapTime == that.mapTime && //
this.totalTime == that.totalTime;
}
} }

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

@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bson.BSON; import org.bson.BSON;
@ -581,9 +582,10 @@ public class Criteria implements CriteriaDefinition {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
boolean not = false; boolean not = false;
for (String k : this.criteria.keySet()) { for (Entry<String, Object> entry : criteria.entrySet()) {
Object value = this.criteria.get(k); String key = entry.getKey();
Object value = entry.getValue();
if (requiresGeoJsonFormat(value)) { if (requiresGeoJsonFormat(value)) {
value = new BasicDBObject("$geometry", value); value = new BasicDBObject("$geometry", value);
@ -591,14 +593,14 @@ public class Criteria implements CriteriaDefinition {
if (not) { if (not) {
DBObject notDbo = new BasicDBObject(); DBObject notDbo = new BasicDBObject();
notDbo.put(k, value); notDbo.put(key, value);
dbo.put("$not", notDbo); dbo.put("$not", notDbo);
not = false; not = false;
} else { } else {
if ("$not".equals(k) && value == null) { if ("$not".equals(key) && value == null) {
not = true; not = true;
} else { } else {
dbo.put(k, value); dbo.put(key, value);
} }
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -83,14 +83,10 @@ public class Field {
public DBObject getFieldsObject() { public DBObject getFieldsObject() {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject(criteria);
for (String k : criteria.keySet()) { for (Entry<String, Object> entry : slices.entrySet()) {
dbo.put(k, criteria.get(k)); dbo.put(entry.getKey(), new BasicDBObject("$slice", entry.getValue()));
}
for (String k : slices.keySet()) {
dbo.put(k, new BasicDBObject("$slice", slices.get(k)));
} }
for (Entry<String, Criteria> entry : elemMatchs.entrySet()) { for (Entry<String, Criteria> entry : elemMatchs.entrySet()) {
@ -134,8 +130,8 @@ public class Field {
return false; return false;
} }
boolean samePositionKey = this.postionKey == null ? that.postionKey == null : this.postionKey boolean samePositionKey = this.postionKey == null ? that.postionKey == null
.equals(that.postionKey); : this.postionKey.equals(that.postionKey);
boolean samePositionValue = this.positionValue == that.positionValue; boolean samePositionValue = this.positionValue == that.positionValue;
return samePositionKey && samePositionValue; return samePositionKey && samePositionValue;

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2014 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -94,9 +94,9 @@ public class Query {
if (existing == null) { if (existing == null) {
this.criteria.put(key, criteriaDefinition); this.criteria.put(key, criteriaDefinition);
} else { } else {
throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, " throw new InvalidMongoDbApiUsageException(
+ "you can't add a second '" + key + "' criteria. " + "Query already contains '" "Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. "
+ existing.getCriteriaObject() + "'."); + "Query already contains '" + existing.getCriteriaObject() + "'.");
} }
return this; return this;
@ -221,10 +221,8 @@ public class Query {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) { for (CriteriaDefinition definition : criteria.values()) {
CriteriaDefinition c = criteria.get(k); dbo.putAll(definition.getCriteriaObject());
DBObject cl = c.getCriteriaObject();
dbo.putAll(cl);
} }
if (!restrictedTypes.isEmpty()) { if (!restrictedTypes.isEmpty()) {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2014 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -327,12 +327,7 @@ public class Update {
} }
public DBObject getUpdateObject() { public DBObject getUpdateObject() {
return new BasicDBObject(modifierOps);
DBObject dbo = new BasicDBObject();
for (String k : modifierOps.keySet()) {
dbo.put(k, modifierOps.get(k));
}
return dbo;
} }
protected void addFieldOperation(String operator, String key, Object value) { protected void addFieldOperation(String operator, String key, Object value) {
@ -355,8 +350,8 @@ public class Update {
if (existingValue instanceof BasicDBObject) { if (existingValue instanceof BasicDBObject) {
keyValueMap = (BasicDBObject) existingValue; keyValueMap = (BasicDBObject) existingValue;
} else { } else {
throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was " throw new InvalidDataAccessApiUsageException(
+ existingValue.getClass()); "Modifier Operations should be a LinkedHashMap but was " + existingValue.getClass());
} }
} }

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java

@ -15,6 +15,9 @@
*/ */
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import java.util.Arrays;
import java.util.List;
import org.springframework.data.domain.Range; import org.springframework.data.domain.Range;
import org.springframework.data.geo.Distance; import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
@ -34,7 +37,7 @@ import org.springframework.util.ClassUtils;
public class MongoParametersParameterAccessor extends ParametersParameterAccessor implements MongoParameterAccessor { public class MongoParametersParameterAccessor extends ParametersParameterAccessor implements MongoParameterAccessor {
private final MongoQueryMethod method; private final MongoQueryMethod method;
private final Object[] values; private final List<Object> values;
/** /**
* Creates a new {@link MongoParametersParameterAccessor}. * Creates a new {@link MongoParametersParameterAccessor}.
@ -43,9 +46,11 @@ public class MongoParametersParameterAccessor extends ParametersParameterAccesso
* @param values must not be {@literal null}. * @param values must not be {@literal null}.
*/ */
public MongoParametersParameterAccessor(MongoQueryMethod method, Object[] values) { public MongoParametersParameterAccessor(MongoQueryMethod method, Object[] values) {
super(method.getParameters(), values); super(method.getParameters(), values);
this.method = method; this.method = method;
this.values = values; this.values = Arrays.asList(values);
} }
public Range<Distance> getDistanceRange() { public Range<Distance> getDistanceRange() {
@ -131,6 +136,6 @@ public class MongoParametersParameterAccessor extends ParametersParameterAccesso
*/ */
@Override @Override
public Object[] getValues() { public Object[] getValues() {
return values; return values.toArray();
} }
} }

Loading…
Cancel
Save