Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#617 add java.util.Currency converter #1176

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Currency, String> {
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;
}
}
}