Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GeldbetragSingletonSpi provided as service class
- Loading branch information
oboehm
committed
Aug 3, 2018
1 parent
0048a5c
commit 8926fd0
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/main/java/de/jfachwert/bank/internal/GeldbetragSingletonSpi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2018 by Oliver Boehm | ||
* | ||
* 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. | ||
* | ||
* (c)reated 03.08.2018 by oboehm (ob@oasd.de) | ||
*/ | ||
package de.jfachwert.bank.internal; | ||
|
||
import de.jfachwert.bank.Geldbetrag; | ||
import de.jfachwert.bank.GeldbetragFactory; | ||
|
||
import javax.money.Monetary; | ||
import javax.money.MonetaryAmount; | ||
import javax.money.MonetaryAmountFactory; | ||
import javax.money.MonetaryException; | ||
import javax.money.spi.Bootstrap; | ||
import javax.money.spi.MonetaryAmountFactoryProviderSpi; | ||
import javax.money.spi.MonetaryAmountsSingletonSpi; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Eine Implementierung fuer {@link MonetaryAmountsSingletonSpi}, die dafuer | ||
* sorgt, dass der {@link de.jfachwert.bank.Geldbetrag} als Implementierung | ||
* gesehen wird. Als Vorlage fuer die Implementierung diente die | ||
* DefaultMonetaryAmountsSingletonSpi aus der Referenzimplementierung, die | ||
* auf den {@link Bootstrap}-Mechanismus des JSRs aufsetzt. | ||
* <p> | ||
* Diese Klasse war notwendig, um die TCK-Suite zu JSR 354, die aus | ||
* GeldbetragIT aufgerufen wird, zu bestehen. | ||
* </p> | ||
*/ | ||
public class GeldbetragSingletonSpi implements MonetaryAmountsSingletonSpi { | ||
|
||
private final Map<Class<? extends MonetaryAmount>, MonetaryAmountFactoryProviderSpi<?>> factories = | ||
new ConcurrentHashMap<>(); | ||
|
||
public GeldbetragSingletonSpi() { | ||
for (MonetaryAmountFactoryProviderSpi<?> f : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) { | ||
factories.putIfAbsent(f.getAmountType(), f); | ||
} | ||
} | ||
|
||
// save cast, since members are managed by this instance | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T extends MonetaryAmount> MonetaryAmountFactory<T> getAmountFactory(Class<T> amountType) { | ||
if (Geldbetrag.class.equals(amountType)) { | ||
return (MonetaryAmountFactory<T>) new GeldbetragFactory(); | ||
} | ||
MonetaryAmountFactoryProviderSpi<T> f = MonetaryAmountFactoryProviderSpi.class.cast(factories.get(amountType)); | ||
if (Objects.nonNull(f)) { | ||
return f.createMonetaryAmountFactory(); | ||
} | ||
throw new MonetaryException("no MonetaryAmountFactory found for " + amountType); | ||
} | ||
|
||
/** | ||
* Liefert den {@link Geldbetrag} als Default-Implemntierung fuer | ||
* {@link MonetaryAmount}. | ||
* | ||
* @return Geldbetrag-Klasse | ||
* @see Monetary#getDefaultAmountType() | ||
*/ | ||
@Override | ||
public Class<? extends MonetaryAmount> getDefaultAmountType() { | ||
return Geldbetrag.class; | ||
} | ||
|
||
/** | ||
* Liefert eine Liste der registrierten {@link MonetaryAmount}-Klassen, | ||
* in der auch die {@link Geldbetrag}-Klasse enthalten ist. | ||
* | ||
* @return Liste der verfuegbaren {@link MonetaryAmount}-Klassen | ||
*/ | ||
@Override | ||
public Set<Class<? extends MonetaryAmount>> getAmountTypes() { | ||
Set<Class<? extends MonetaryAmount>> amountTypes = new HashSet<>(factories.keySet()); | ||
amountTypes.add(Geldbetrag.class); | ||
return amountTypes; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/de/jfachwert/bank/internal/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2018 by Oliver Boehm | ||
* | ||
* 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. | ||
* | ||
* (c)reated 03.08.2018 by oboehm (ob@oasd.de) | ||
*/ | ||
|
||
/** | ||
* Dieses Package ist nur fuer den internen Gebrauch gedacht. | ||
*/ | ||
package de.jfachwert.bank.internal; |
3 changes: 3 additions & 0 deletions
3
src/main/resources/META-INF/services/javax.money.spi.MonetaryAmountsSingletonSpi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Diese Klasse ist dafuer zustaendig, dass unsere Instanz der | ||
# {@link javax.money.MonetaryAmountFactory} registriert wird. | ||
de.jfachwert.bank.internal.GeldbetragSingletonSpi |
60 changes: 60 additions & 0 deletions
60
src/test/java/de/jfachwert/bank/internal/GeldbetragSingletonSpiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2018 by Oliver Boehm | ||
* | ||
* 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. | ||
* | ||
* (c)reated 03.08.2018 by oboehm (ob@oasd.de) | ||
*/ | ||
package de.jfachwert.bank.internal; | ||
|
||
import de.jfachwert.bank.Geldbetrag; | ||
import de.jfachwert.bank.GeldbetragFactory; | ||
import org.junit.Test; | ||
|
||
import javax.money.MonetaryAmount; | ||
import javax.money.MonetaryAmountFactory; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Unit-Tests fuer {@link GeldbetragSingletonSpi}-Klasse. | ||
* | ||
* @author oboehm | ||
*/ | ||
public final class GeldbetragSingletonSpiTest { | ||
|
||
private final GeldbetragSingletonSpi singletonSpi = new GeldbetragSingletonSpi(); | ||
|
||
@Test | ||
public void testGetAmountTypes() { | ||
Set<Class<? extends MonetaryAmount>> amountTypes = singletonSpi.getAmountTypes(); | ||
assertThat(amountTypes, hasItem(Geldbetrag.class)); | ||
} | ||
|
||
@Test | ||
public void testGetDefaultAmountType() { | ||
assertEquals(Geldbetrag.class, singletonSpi.getDefaultAmountType()); | ||
} | ||
|
||
@Test | ||
public void testGetAmountFactories() { | ||
Collection<MonetaryAmountFactory<?>> amountFactories = singletonSpi.getAmountFactories(); | ||
long n = amountFactories.stream().filter(f -> GeldbetragFactory.class.equals(f.getClass())).count(); | ||
assertEquals(1L, n); | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
src/test/resources/META-INF/services/org.javamoney.tck.JSR354TestConfiguration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# Setup fuer Integrationstests der Geldbetrag-Klasse. | ||
# Dies wird fuer das TCK zum JSR354 benoetigt. | ||
de.jfachwert.bank.GeldbetragIT |