diff --git a/morphia/src/main/java/org/mongodb/morphia/converters/CurrencyConverter.java b/morphia/src/main/java/org/mongodb/morphia/converters/CurrencyConverter.java new file mode 100644 index 00000000000..b86ccb3be6c --- /dev/null +++ b/morphia/src/main/java/org/mongodb/morphia/converters/CurrencyConverter.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 MongoDB, Inc. + * + * 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.mongodb.morphia.converters; + +import org.mongodb.morphia.mapping.MappedField; + +import java.util.Currency; +import java.util.Locale; + +/** + * @author Adam Dispenza, (adam@dispenza.org) + */ +public class CurrencyConverter extends TypeConverter implements SimpleValueConverter { + /** + * Creates the Converter. + */ + public CurrencyConverter() { + super(Currency.class); + } + + @Override + public Object encode(final Object value, final MappedField optionalExtraInfo) { + return isValidCurrency(value) ? ((Currency) value).getCurrencyCode() : null; + } + + @Override + public Object decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) { + if (fromDBObject == null) { + return null; + } + + if (fromDBObject instanceof Locale) { + return Currency.getInstance((Locale) fromDBObject); + } + + return Currency.getInstance((String) fromDBObject); + } + + private boolean isValidCurrency(final Object potentialCurrency) { + return potentialCurrency != null && potentialCurrency instanceof Currency; + } +} + diff --git a/morphia/src/main/java/org/mongodb/morphia/converters/DefaultConverters.java b/morphia/src/main/java/org/mongodb/morphia/converters/DefaultConverters.java index 39bd282b3d2..8d2d3484db6 100644 --- a/morphia/src/main/java/org/mongodb/morphia/converters/DefaultConverters.java +++ b/morphia/src/main/java/org/mongodb/morphia/converters/DefaultConverters.java @@ -59,6 +59,7 @@ public DefaultConverters(final Mapper mapper) { addConverter(new ObjectIdConverter()); addConverter(new TimestampConverter()); addConverter(new BigDecimalConverter()); + addConverter(new CurrencyConverter()); // Converters for Geo entities addConverter(new GeometryShapeConverter.PointConverter()); diff --git a/morphia/src/test/java/org/mongodb/morphia/converters/CurrencyConverterTest.java b/morphia/src/test/java/org/mongodb/morphia/converters/CurrencyConverterTest.java new file mode 100644 index 00000000000..95dc89d58c6 --- /dev/null +++ b/morphia/src/test/java/org/mongodb/morphia/converters/CurrencyConverterTest.java @@ -0,0 +1,121 @@ +/* + * Copyright 2017 MongoDB, Inc. + * + * 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.mongodb.morphia.converters; + +import java.util.Currency; +import java.util.Locale; + +import org.bson.types.ObjectId; +import org.junit.Before; +import org.junit.Test; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +public class CurrencyConverterTest extends ConverterTest { + private final CurrencyConverter converter = new CurrencyConverter(); + + public CurrencyConverterTest() { + super(new CurrencyConverter()); + } + + @Before + public void serverCheck() { + checkMinServerVersion(3.4); + } + + @Test + public void convertNull() { + assertNull(getConverter().decode(null, null)); + assertNull(getConverter().encode(null)); + } + + @Test + public void decodes() { + assertEquals(Currency.getInstance("USD"), converter.decode(Currency.class, "USD")); + assertEquals(Currency.getInstance(Locale.CHINA), converter.decode(Currency.class, Locale.CHINA)); + } + + @Test + public void testConversion() { + compare(Currency.class, Currency.getInstance("USD")); + } + + @Test(expected = IllegalArgumentException.class) + public void testBadCurrencyCode() { + converter.decode(Currency.class, "BLAH"); + } + + @Test + public void testEntity() { + final Foo foo = new Foo(); + foo.setCurrency(Currency.getInstance("USD")); + getDs().save(foo); + + assertEquals(foo, getDs().find(Foo.class).get()); + } + + @Entity + private static class Foo { + + @Id + private ObjectId id; + private Currency currency; + + public ObjectId getId() { + return id; + } + + public void setId(final ObjectId id) { + this.id = id; + } + + public Currency getCurrency() { + return currency; + } + + public void setCurrency(final Currency currency) { + this.currency = currency; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Foo)) { + return false; + } + + final Foo foo = (Foo) o; + + if (getId() != null ? !getId().equals(foo.getId()) : foo.getId() != null) { + return false; + } + return getCurrency() != null ? getCurrency().equals(foo.getCurrency()) : foo.getCurrency() == null; + } + + @Override + public int hashCode() { + int result = getId() != null ? getId().hashCode() : 0; + result = 31 * result + (getCurrency() != null ? getCurrency().hashCode() : 0); + return result; + } + } +}