From 219f075d0b391bc43aa6c8ecf9663f59abbe4c0e Mon Sep 17 00:00:00 2001 From: deepcloudlabs Date: Fri, 22 Jul 2022 12:00:32 +0300 Subject: [PATCH] initial commit --- lottery-module/src/application.properties | 1 + .../lottery/application/LotteryApp.java | 19 +++++++++++++++++-- .../src/com/example/random/service/QoS.java | 12 ++++++++++++ .../example/random/service/QualityLevel.java | 5 +++++ .../business/CheapRandomNumberService.java | 17 +++++++++++++++++ .../business/FastRandomNumberService.java | 4 ++++ .../business/SecureRandomNumberService.java | 4 ++++ random-module/src/module-info.java | 3 ++- 8 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 lottery-module/src/application.properties create mode 100644 random-module/src/com/example/random/service/QoS.java create mode 100644 random-module/src/com/example/random/service/QualityLevel.java create mode 100644 random-module/src/com/example/random/service/business/CheapRandomNumberService.java diff --git a/lottery-module/src/application.properties b/lottery-module/src/application.properties new file mode 100644 index 0000000..0b2706f --- /dev/null +++ b/lottery-module/src/application.properties @@ -0,0 +1 @@ +random.service=CHEAP \ No newline at end of file diff --git a/lottery-module/src/com/example/lottery/application/LotteryApp.java b/lottery-module/src/com/example/lottery/application/LotteryApp.java index c9bdf1c..6c9da08 100644 --- a/lottery-module/src/com/example/lottery/application/LotteryApp.java +++ b/lottery-module/src/com/example/lottery/application/LotteryApp.java @@ -1,16 +1,31 @@ package com.example.lottery.application; +import java.io.File; +import java.io.FileInputStream; +import java.util.Properties; import java.util.ServiceLoader; +import java.util.ServiceLoader.Provider; import com.example.lottery.service.business.StandardLotterService; +import com.example.random.service.QoS; +import com.example.random.service.QualityLevel; import com.example.random.service.RandomNumberService; public class LotteryApp { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { + var props = new Properties(); + props.load(new FileInputStream(new File("src","application.properties"))); + var qualityLevel = QualityLevel.valueOf(props.getProperty("random.service")); var lotteryService = new StandardLotterService(); var services = ServiceLoader.load(RandomNumberService.class); - lotteryService.setRandomNumberService(services.findFirst().get()); + var service = services.stream() + .map(Provider::get) + .filter(srv -> srv.getClass().isAnnotationPresent(QoS.class)) + .filter(srv -> srv.getClass().getAnnotation(QoS.class).value()==qualityLevel) + .findFirst() + .orElseThrow(); + lotteryService.setRandomNumberService(service); lotteryService.getLotteryNumbers(60, 6, 10) .forEach(System.out::println); } diff --git a/random-module/src/com/example/random/service/QoS.java b/random-module/src/com/example/random/service/QoS.java new file mode 100644 index 0000000..837b706 --- /dev/null +++ b/random-module/src/com/example/random/service/QoS.java @@ -0,0 +1,12 @@ +package com.example.random.service; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface QoS { + QualityLevel value(); +} diff --git a/random-module/src/com/example/random/service/QualityLevel.java b/random-module/src/com/example/random/service/QualityLevel.java new file mode 100644 index 0000000..d78715d --- /dev/null +++ b/random-module/src/com/example/random/service/QualityLevel.java @@ -0,0 +1,5 @@ +package com.example.random.service; + +public enum QualityLevel { + FAST, SECURE, CHEAP +} diff --git a/random-module/src/com/example/random/service/business/CheapRandomNumberService.java b/random-module/src/com/example/random/service/business/CheapRandomNumberService.java new file mode 100644 index 0000000..3c24f8a --- /dev/null +++ b/random-module/src/com/example/random/service/business/CheapRandomNumberService.java @@ -0,0 +1,17 @@ +package com.example.random.service.business; + +import com.example.random.service.QoS; +import com.example.random.service.QualityLevel; +import com.example.random.service.RandomNumberService; + +@QoS(QualityLevel.CHEAP) +public class CheapRandomNumberService implements RandomNumberService { + private static int counter = 0; + @Override + public int generate(int min, int max) { + System.err.println("CheapRandomNumberService::generate"); + counter++; + return min + (counter % (max-min+1) ); + } + +} diff --git a/random-module/src/com/example/random/service/business/FastRandomNumberService.java b/random-module/src/com/example/random/service/business/FastRandomNumberService.java index 052cfec..c59e6b7 100644 --- a/random-module/src/com/example/random/service/business/FastRandomNumberService.java +++ b/random-module/src/com/example/random/service/business/FastRandomNumberService.java @@ -2,12 +2,16 @@ import java.util.concurrent.ThreadLocalRandom; +import com.example.random.service.QoS; +import com.example.random.service.QualityLevel; import com.example.random.service.RandomNumberService; +@QoS(QualityLevel.FAST) public class FastRandomNumberService implements RandomNumberService { @Override public int generate(int min, int max) { + System.err.println("FastRandomNumberService::generate"); return ThreadLocalRandom.current().nextInt(min, max); } diff --git a/random-module/src/com/example/random/service/business/SecureRandomNumberService.java b/random-module/src/com/example/random/service/business/SecureRandomNumberService.java index fac3bc8..b0deacd 100644 --- a/random-module/src/com/example/random/service/business/SecureRandomNumberService.java +++ b/random-module/src/com/example/random/service/business/SecureRandomNumberService.java @@ -2,14 +2,18 @@ import java.security.SecureRandom; +import com.example.random.service.QoS; +import com.example.random.service.QualityLevel; import com.example.random.service.RandomNumberService; +@QoS(QualityLevel.SECURE) public class SecureRandomNumberService implements RandomNumberService { private SecureRandom random = new SecureRandom(); @Override public int generate(int min, int max) { + System.err.println("SecureRandomNumberService::generate"); return random.nextInt(min, max); } diff --git a/random-module/src/module-info.java b/random-module/src/module-info.java index 37b2026..9453efb 100644 --- a/random-module/src/module-info.java +++ b/random-module/src/module-info.java @@ -1,8 +1,9 @@ import com.example.random.service.RandomNumberService; +import com.example.random.service.business.CheapRandomNumberService; import com.example.random.service.business.FastRandomNumberService; import com.example.random.service.business.SecureRandomNumberService; module com.example.random { exports com.example.random.service; - provides RandomNumberService with FastRandomNumberService,SecureRandomNumberService; + provides RandomNumberService with FastRandomNumberService,SecureRandomNumberService,CheapRandomNumberService; } \ No newline at end of file