diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java index 094092855..a7d2903f6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java @@ -16,6 +16,8 @@ package org.springframework.data.document.mongodb.convert; +import static org.springframework.data.document.mongodb.convert.ObjectIdConverters.*; + import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; @@ -603,58 +605,6 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext initializeConverters(); } - /** - * Simple singleton to convert {@link ObjectId}s to their {@link String} representation. - * - * @author Oliver Gierke - */ - public static enum ObjectIdToStringConverter implements Converter { - INSTANCE; - - public String convert(ObjectId id) { - return id.toString(); - } - } - - /** - * Simple singleton to convert {@link String}s to their {@link ObjectId} representation. - * - * @author Oliver Gierke - */ - public static enum StringToObjectIdConverter implements Converter { - INSTANCE; - - public ObjectId convert(String source) { - return new ObjectId(source); - } - } - - /** - * Simple singleton to convert {@link ObjectId}s to their {@link java.math.BigInteger} representation. - * - * @author Oliver Gierke - */ - public static enum ObjectIdToBigIntegerConverter implements Converter { - INSTANCE; - - public BigInteger convert(ObjectId source) { - return new BigInteger(source.toString(), 16); - } - } - - /** - * Simple singleton to convert {@link BigInteger}s to their {@link ObjectId} representation. - * - * @author Oliver Gierke - */ - public static enum BigIntegerToObjectIdConverter implements Converter { - INSTANCE; - - public ObjectId convert(BigInteger source) { - return new ObjectId(source.toString(16)); - } - } - protected class PersistentPropertyWrapper { private final PersistentProperty property; private final DBObject target; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/ObjectIdConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/ObjectIdConverters.java new file mode 100644 index 000000000..d96633d24 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/ObjectIdConverters.java @@ -0,0 +1,88 @@ +/* + * Copyright 2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.convert; + +import java.math.BigInteger; + +import org.bson.types.ObjectId; +import org.springframework.core.convert.converter.Converter; + +/** + * Wrapper class to contain useful {@link ObjectId}-to-something-and-back converters. + * + * @author Oliver Gierke + */ +abstract class ObjectIdConverters { + + /** + * Private constructor to prevent instantiation. + */ + private ObjectIdConverters() { + + } + + /** + * Simple singleton to convert {@link ObjectId}s to their {@link String} representation. + * + * @author Oliver Gierke + */ + public static enum ObjectIdToStringConverter implements Converter { + INSTANCE; + + public String convert(ObjectId id) { + return id.toString(); + } + } + + /** + * Simple singleton to convert {@link String}s to their {@link ObjectId} representation. + * + * @author Oliver Gierke + */ + public static enum StringToObjectIdConverter implements Converter { + INSTANCE; + + public ObjectId convert(String source) { + return new ObjectId(source); + } + } + + /** + * Simple singleton to convert {@link ObjectId}s to their {@link java.math.BigInteger} representation. + * + * @author Oliver Gierke + */ + public static enum ObjectIdToBigIntegerConverter implements Converter { + INSTANCE; + + public BigInteger convert(ObjectId source) { + return new BigInteger(source.toString(), 16); + } + } + + /** + * Simple singleton to convert {@link BigInteger}s to their {@link ObjectId} representation. + * + * @author Oliver Gierke + */ + public static enum BigIntegerToObjectIdConverter implements Converter { + INSTANCE; + + public ObjectId convert(BigInteger source) { + return new ObjectId(source.toString(16)); + } + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/SimpleMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/SimpleMongoConverter.java index 1f04fefe7..f3505d33c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/SimpleMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/SimpleMongoConverter.java @@ -15,6 +15,7 @@ */ package org.springframework.data.document.mongodb.convert; +import static org.springframework.data.document.mongodb.convert.ObjectIdConverters.*; import java.lang.reflect.*; import java.math.BigInteger; import java.util.*; @@ -540,56 +541,4 @@ public class SimpleMongoConverter implements MongoConverter, InitializingBean { public void afterPropertiesSet() { initializeConverters(); } - - /** - * Simple singleton to convert {@link ObjectId}s to their {@link String} representation. - * - * @author Oliver Gierke - */ - public static enum ObjectIdToStringConverter implements Converter { - INSTANCE; - - public String convert(ObjectId id) { - return id.toString(); - } - } - - /** - * Simple singleton to convert {@link String}s to their {@link ObjectId} representation. - * - * @author Oliver Gierke - */ - public static enum StringToObjectIdConverter implements Converter { - INSTANCE; - - public ObjectId convert(String source) { - return new ObjectId(source); - } - } - - /** - * Simple singleton to convert {@link ObjectId}s to their {@link BigInteger} representation. - * - * @author Oliver Gierke - */ - public static enum ObjectIdToBigIntegerConverter implements Converter { - INSTANCE; - - public BigInteger convert(ObjectId source) { - return new BigInteger(source.toString(), 16); - } - } - - /** - * Simple singleton to convert {@link BigInteger}s to their {@link ObjectId} representation. - * - * @author Oliver Gierke - */ - public static enum BigIntegerToObjectIdConverter implements Converter { - INSTANCE; - - public ObjectId convert(BigInteger source) { - return new ObjectId(source.toString(16)); - } - } }