Skip to content

Commit f6babed

Browse files
committed
Merge pull request #39 from otaviojava/clean_code
Removed some unnecessary cast and Array creation.
2 parents b211069 + 38e29c5 commit f6babed

9 files changed

+25
-27
lines changed

src/main/java/javax/money/AbstractContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
66
* AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
77
* BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency
8-
* API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights
8+
* API ("Specification") Copyright (c) 2012-2013, Credit SUISSE All rights
99
* reserved.
1010
*/
1111
package javax.money;
@@ -18,7 +18,7 @@
1818

1919
/**
2020
* Represents a general context of data targeting an item of type {@code Q}. Contexts are used to add arbitrary
21-
* data that cannot be be mapped in a atandard way to the money API, e.g. use case or customer specific
21+
* data that cannot be be mapped in a standard way to the money API, e.g. use case or customer specific
2222
* extensions os specialities.<p>
2323
* Superclasses of this class must be final, immutable, serializable and thread-safe.
2424
*/
@@ -47,7 +47,7 @@ public abstract class AbstractContext implements Serializable{
4747
@SuppressWarnings({"unchecked", "rawtypes"})
4848
protected AbstractContext(AbstractContextBuilder<?,?> builder){
4949
for(Map.Entry<Class,Map<Object,Object>> en : ((Map<Class,Map<Object,Object>>) builder.data).entrySet()){
50-
Map<Object,Object> presentMap = (Map<Object,Object>) this.data.get(en.getKey());
50+
Map<Object,Object> presentMap = this.data.get(en.getKey());
5151
if(presentMap == null){
5252
presentMap = new HashMap<>(en.getValue());
5353
this.data.put(en.getKey(), presentMap);

src/main/java/javax/money/AbstractContextBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public <T> B set(Object key, T value, Class<? extends T> type){
357357
@Override
358358
public String toString(){
359359
StringBuilder attrsBuilder = new StringBuilder();
360-
for(Map.Entry<Class,Map<Object,Object>> en : ((Map<Class,Map<Object,Object>>) this.data).entrySet()){
360+
for(Map.Entry<Class,Map<Object,Object>> en : this.data.entrySet()){
361361

362362
Map<Object,Object> sortedMap = new TreeMap<>(Comparator.comparing(Object::toString));
363363
sortedMap.putAll(en.getValue());

src/main/java/javax/money/RoundingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String getRoundingName(){
5959
* @return the target CurrencyUnit, or null.
6060
*/
6161
public CurrencyUnit getCurrency(){
62-
return get(CurrencyUnit.class, (CurrencyUnit) null);
62+
return get(CurrencyUnit.class, null);
6363
}
6464

6565
/**

src/main/java/javax/money/RoundingQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Integer getScale(){
7575
* @return the CurrencyUnit, or null.
7676
*/
7777
public CurrencyUnit getCurrency(){
78-
return get(CurrencyUnit.class, (CurrencyUnit) null);
78+
return get(CurrencyUnit.class, null);
7979
}
8080

8181
/**

src/main/java/javax/money/spi/MonetaryAmountsSingletonQuerySpi.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
import javax.money.MonetaryAmount;
1212
import javax.money.MonetaryAmountFactory;
1313
import javax.money.MonetaryAmountFactoryQuery;
14-
import java.util.ArrayList;
1514
import java.util.Collection;
16-
import java.util.List;
15+
import java.util.stream.Collectors;
1716

1817
/**
1918
* SPI (core) for the backing implementation of the {@link javax.money.MonetaryAmounts} singleton, implementing
@@ -48,7 +47,7 @@ public interface MonetaryAmountsSingletonQuerySpi{
4847
* the required
4948
* {@link javax.money.MonetaryContext}.
5049
*/
51-
Collection<MonetaryAmountFactory<?>> getAmountFactories(MonetaryAmountFactoryQuery query);
50+
Collection<MonetaryAmountFactory<? extends MonetaryAmount>> getAmountFactories(MonetaryAmountFactoryQuery query);
5251

5352
/**
5453
* Checks if an {@link javax.money.MonetaryAmountFactory} is matching the given query.
@@ -83,12 +82,8 @@ default Class<? extends MonetaryAmount> getAmountType(MonetaryAmountFactoryQuery
8382
* @return the type found, or null.
8483
*/
8584
default Collection<Class<? extends MonetaryAmount>> getAmountTypes(MonetaryAmountFactoryQuery query){
86-
Collection<MonetaryAmountFactory<?>> factories = getAmountFactories(query);
87-
List<Class<? extends MonetaryAmount>> result = new ArrayList<>();
88-
for(MonetaryAmountFactory<?> f : factories){
89-
result.add(f.getAmountType());
90-
}
91-
return result;
85+
Collection<MonetaryAmountFactory<? extends MonetaryAmount>> factories = getAmountFactories(query);
86+
return factories.stream().map(MonetaryAmountFactory::getAmountType).collect(Collectors.toList());
9287
}
9388

9489
/**

src/test/java/javax/money/UnknownCurrencyExceptionTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
import org.testng.annotations.Test;
1414

1515
import java.util.Locale;
16+
import java.util.logging.Level;
17+
import java.util.logging.Logger;
1618

1719
import static org.testng.Assert.*;
1820

1921
/**
2022
* Created by Anatole on 05.03.14.
2123
*/
22-
public class UnknownCurrencyExceptionTest{
24+
public class UnknownCurrencyExceptionTest {
25+
26+
private static final Logger LOGGER = Logger.getLogger(UnknownCurrencyExceptionTest.class.getName());
2327
@Test
2428
public void testGetCurrencyCode() throws Exception{
2529
UnknownCurrencyException e = new UnknownCurrencyException("GGG");
@@ -34,7 +38,7 @@ public void testGetLocale() throws Exception{
3438
UnknownCurrencyException e = new UnknownCurrencyException(Locale.CANADA_FRENCH);
3539
assertEquals(Locale.CANADA_FRENCH, e.getLocale());
3640
assertNull(e.getCurrencyCode());
37-
System.out.println(e);
41+
LOGGER.log(Level.INFO, e.getMessage(), e);
3842
assertTrue(e.toString().contains(Locale.CANADA_FRENCH.toString()));
3943
assertTrue(e.toString().contains("UnknownCurrencyException"));
4044
}

src/test/java/javax/money/convert/ExchangeRate_BuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testGetSetExchangeRateChain(){
6666
ExchangeRate rate =
6767
b.setFactor(TestNumberValue.of(9)).setContext(ConversionContext.of("test", RateType.DEFERRED)).build();
6868
assertEquals(rate.getFactor().numberValue(BigDecimal.class), BigDecimal.valueOf(9));
69-
assertEquals(rate.getExchangeRateChain(), Arrays.asList(new ExchangeRate[]{rate1, rate2}));
69+
assertEquals(rate.getExchangeRateChain(), Arrays.asList(rate1, rate2));
7070
}
7171

7272
@Test

src/test/java/javax/money/convert/TestMonetaryConversionsSingletonSpi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class TestMonetaryConversionsSingletonSpi implements MonetaryConversionsS
2828

2929
@Override
3030
public ExchangeRateProvider getExchangeRateProvider(ConversionQuery conversionQuery){
31-
Collection<ExchangeRateProvider> providers = null;
3231

3332
if(conversionQuery.getProviders().isEmpty() || conversionQuery.getProviders().contains("test")){
3433
return provider;
@@ -48,7 +47,7 @@ public boolean isConversionAvailable(ConversionQuery conversionQuery){
4847

4948
@Override
5049
public Collection<String> getProviderNames(){
51-
return Arrays.asList(new String[]{"test"});
50+
return Arrays.asList("test");
5251
}
5352

5453
@Override

src/test/java/javax/money/spi/BootstrapTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void testGetServices() throws Exception{
5959
@Test
6060
public void testGetServicesWithDefault() throws Exception{
6161
Collection<Object> services = Collection.class
62-
.cast(Bootstrap.getServices(Boolean.class, Arrays.asList(new Boolean[]{Boolean.FALSE, Boolean.TRUE})));
62+
.cast(Bootstrap.getServices(Boolean.class, Arrays.asList(Boolean.FALSE, Boolean.TRUE)));
6363
assertNotNull(services);
6464
assertFalse(services.isEmpty());
6565
assertTrue(services.size() == 2);
@@ -92,27 +92,27 @@ public final static class TestServiceProvider extends DefaultServiceProvider
9292
@Override
9393
public <T> List<T> getServices(Class<T> serviceType){
9494
if(String.class.equals(serviceType)){
95-
return List.class.cast(Arrays.asList(new String[]{"service1", "service2"}));
95+
return List.class.cast(Arrays.asList("service1", "service2"));
9696
}
9797
else if(Integer.class.equals(serviceType)){
98-
return List.class.cast(Arrays.asList(new Integer[]{5}));
98+
return List.class.cast(Arrays.asList(5));
9999
}
100100
else if(Long.class.equals(serviceType)){
101-
return List.class.cast(Arrays.asList(new Long[]{(long) 111}));
101+
return List.class.cast(Arrays.asList((long) 111));
102102
}
103103
return super.getServices(serviceType);
104104
}
105105

106106
@Override
107107
public <T> List<T> getServices(Class<T> serviceType, List<T> defaultList){
108108
if(String.class.equals(serviceType)){
109-
return List.class.cast(Arrays.asList(new String[]{"service1", "service2"}));
109+
return List.class.cast(Arrays.asList("service1", "service2"));
110110
}
111111
else if(Integer.class.equals(serviceType)){
112-
return List.class.cast(Arrays.asList(new Integer[]{5}));
112+
return List.class.cast(Arrays.asList(5));
113113
}
114114
else if(Long.class.equals(serviceType)){
115-
return List.class.cast(Arrays.asList(new Long[]{(long) 111}));
115+
return List.class.cast(Arrays.asList((long) 111));
116116
}
117117
return super.getServices(serviceType, defaultList);
118118
}

0 commit comments

Comments
 (0)