From 22d49fea860f8ec25ff20ca511a5e9627e10a89f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Nov 2015 12:27:22 +0100 Subject: [PATCH] DATAMONGO-1337 - Another round of polishes on SonarQuber complaints. --- .../AggregationFunctionExpressions.java | 11 ++-- .../mongodb/core/convert/GeoConverters.java | 6 ++- .../core/convert/MappingMongoConverter.java | 4 -- .../mongodb/core/convert/QueryMapper.java | 6 +-- .../data/mongodb/core/index/Index.java | 16 ++++-- .../core/mapreduce/MapReduceTiming.java | 50 ++++++++++++------- .../data/mongodb/core/query/Criteria.java | 12 +++-- .../data/mongodb/core/query/Field.java | 16 +++--- .../data/mongodb/core/query/Query.java | 14 +++--- .../data/mongodb/core/query/Update.java | 13 ++--- .../MongoParametersParameterAccessor.java | 5 ++ 11 files changed, 85 insertions(+), 68 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java index 8f3525c03..0b88c039c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java +++ b/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; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.springframework.util.Assert; @@ -56,7 +57,7 @@ public enum AggregationFunctionExpressions { static class FunctionExpression implements AggregationExpression { private final String name; - private final Object[] values; + private final List 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!"); this.name = name; - this.values = values; + this.values = Arrays.asList(values); } /* @@ -80,10 +81,10 @@ public enum AggregationFunctionExpressions { @Override public DBObject toDbObject(AggregationOperationContext context) { - List args = new ArrayList(values.length); + List args = new ArrayList(values.size()); - for (int i = 0; i < values.length; i++) { - args.add(unpack(values[i], context)); + for (Object value : values) { + args.add(unpack(value, context)); } return new BasicDBObject("$" + name, args); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java index 038f72e23..7c6e1229c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java @@ -111,9 +111,13 @@ abstract class GeoConverters { @Override public Point convert(DBObject source) { + if (source == null) { + return null; + } + 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")); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 1d21c9693..e1e590014 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/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()); - if (object != null) { - return (T) object; - } - return (T) (object != null ? object : read(type, readRef(dbref), path)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 841a8a9b9..cf308769d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -936,7 +936,7 @@ public class QueryMapper { */ protected String mapPropertyName(MongoPersistentProperty property) { - String mappedName = PropertyToFieldNameConverter.INSTANCE.convert(property); + StringBuilder mappedName = new StringBuilder(PropertyToFieldNameConverter.INSTANCE.convert(property)); boolean inspect = iterator.hasNext(); while (inspect) { @@ -945,13 +945,13 @@ public class QueryMapper { boolean isPositional = (isPositionalParameter(partial) && (property.isMap() || property.isCollectionLike())); if (isPositional) { - mappedName += "." + partial; + mappedName.append(".").append(partial); } inspect = isPositional && iterator.hasNext(); } - return mappedName; + return mappedName.toString(); } private static boolean isPositionalParameter(String partial) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java index b5c0353cd..dbf59f6e2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java +++ b/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"); * 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.Map; +import java.util.Map.Entry; import java.util.concurrent.TimeUnit; import org.springframework.data.domain.Sort.Direction; @@ -44,7 +45,7 @@ public class Index implements IndexDefinition { * * @deprecated since 1.7. */ - @Deprecated// + @Deprecated // DROP } @@ -175,11 +176,18 @@ public class Index implements IndexDefinition { return unique(); } + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.index.IndexDefinition#getIndexKeys() + */ public DBObject getIndexKeys() { + DBObject dbo = new BasicDBObject(); - for (String k : fieldSpec.keySet()) { - dbo.put(k, fieldSpec.get(k).equals(Direction.ASC) ? 1 : -1); + + for (Entry entry : fieldSpec.entrySet()) { + dbo.put(entry.getKey(), Direction.ASC.equals(entry.getValue()) ? 1 : -1); } + return dbo; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTiming.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTiming.java index 9cd4abbe1..770297b59 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTiming.java +++ b/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"); * 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 { - private long mapTime; - - private long emitLoopTime; - - private long totalTime; + private long mapTime, emitLoopTime, totalTime; public MapReduceTiming(long mapTime, long emitLoopTime, long totalTime) { + this.mapTime = mapTime; this.emitLoopTime = emitLoopTime; this.totalTime = totalTime; @@ -41,37 +38,52 @@ public class MapReduceTiming { return totalTime; } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return "MapReduceTiming [mapTime=" + mapTime + ", emitLoopTime=" + emitLoopTime + ", totalTime=" + totalTime + "]"; } + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ @Override public int hashCode() { + final int prime = 31; int result = 1; + result = prime * result + (int) (emitLoopTime ^ (emitLoopTime >>> 32)); result = prime * result + (int) (mapTime ^ (mapTime >>> 32)); result = prime * result + (int) (totalTime ^ (totalTime >>> 32)); + return result; } + /* + * + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ @Override public boolean equals(Object obj) { - if (this == obj) + + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - MapReduceTiming other = (MapReduceTiming) obj; - if (emitLoopTime != other.emitLoopTime) - return false; - if (mapTime != other.mapTime) - return false; - if (totalTime != other.totalTime) + } + + if (!(obj instanceof MapReduceTiming)) { return false; - return true; - } + } + + MapReduceTiming that = (MapReduceTiming) obj; + return this.emitLoopTime == that.emitLoopTime && // + this.mapTime == that.mapTime && // + this.totalTime == that.totalTime; + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 7bea97ce9..66cf5c346 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/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.LinkedHashMap; import java.util.List; +import java.util.Map.Entry; import java.util.regex.Pattern; import org.bson.BSON; @@ -565,9 +566,10 @@ public class Criteria implements CriteriaDefinition { DBObject dbo = new BasicDBObject(); boolean not = false; - for (String k : this.criteria.keySet()) { + for (Entry entry : criteria.entrySet()) { - Object value = this.criteria.get(k); + String key = entry.getKey(); + Object value = entry.getValue(); if (requiresGeoJsonFormat(value)) { value = new BasicDBObject("$geometry", value); @@ -575,14 +577,14 @@ public class Criteria implements CriteriaDefinition { if (not) { DBObject notDbo = new BasicDBObject(); - notDbo.put(k, value); + notDbo.put(key, value); dbo.put("$not", notDbo); not = false; } else { - if ("$not".equals(k) && value == null) { + if ("$not".equals(key) && value == null) { not = true; } else { - dbo.put(k, value); + dbo.put(key, value); } } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java index e24bb4bcf..0676b52fa 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -83,14 +83,10 @@ public class Field { public DBObject getFieldsObject() { - DBObject dbo = new BasicDBObject(); + DBObject dbo = new BasicDBObject(criteria); - for (String k : criteria.keySet()) { - dbo.put(k, criteria.get(k)); - } - - for (String k : slices.keySet()) { - dbo.put(k, new BasicDBObject("$slice", slices.get(k))); + for (Entry entry : slices.entrySet()) { + dbo.put(entry.getKey(), new BasicDBObject("$slice", entry.getValue())); } for (Entry entry : elemMatchs.entrySet()) { @@ -134,8 +130,8 @@ public class Field { return false; } - boolean samePositionKey = this.postionKey == null ? that.postionKey == null : this.postionKey - .equals(that.postionKey); + boolean samePositionKey = this.postionKey == null ? that.postionKey == null + : this.postionKey.equals(that.postionKey); boolean samePositionValue = this.positionValue == that.positionValue; return samePositionKey && samePositionValue; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index b730068b3..b8bb5957b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -94,9 +94,9 @@ public class Query { if (existing == null) { this.criteria.put(key, criteriaDefinition); } else { - throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, " - + "you can't add a second '" + key + "' criteria. " + "Query already contains '" - + existing.getCriteriaObject() + "'."); + throw new InvalidMongoDbApiUsageException( + "Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. " + + "Query already contains '" + existing.getCriteriaObject() + "'."); } return this; @@ -221,10 +221,8 @@ public class Query { DBObject dbo = new BasicDBObject(); - for (String k : criteria.keySet()) { - CriteriaDefinition c = criteria.get(k); - DBObject cl = c.getCriteriaObject(); - dbo.putAll(cl); + for (CriteriaDefinition definition : criteria.values()) { + dbo.putAll(definition.getCriteriaObject()); } if (!restrictedTypes.isEmpty()) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java index 6003ae5fb..6911d953a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -327,12 +327,7 @@ public class Update { } public DBObject getUpdateObject() { - - DBObject dbo = new BasicDBObject(); - for (String k : modifierOps.keySet()) { - dbo.put(k, modifierOps.get(k)); - } - return dbo; + return new BasicDBObject(modifierOps); } protected void addFieldOperation(String operator, String key, Object value) { @@ -355,8 +350,8 @@ public class Update { if (existingValue instanceof BasicDBObject) { keyValueMap = (BasicDBObject) existingValue; } else { - throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was " - + existingValue.getClass()); + throw new InvalidDataAccessApiUsageException( + "Modifier Operations should be a LinkedHashMap but was " + existingValue.getClass()); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java index aea9f371e..8d1cc82ad 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java +++ b/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; +import java.util.Arrays; +import java.util.List; + import org.springframework.data.domain.Range; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Point; @@ -41,7 +44,9 @@ public class MongoParametersParameterAccessor extends ParametersParameterAccesso * @param values must not be {@@iteral null}. */ public MongoParametersParameterAccessor(MongoQueryMethod method, Object[] values) { + super(method.getParameters(), values); + this.method = method; }