1+ package ru .javaops .masterjava .service ;
2+
3+ import java .util .Collections ;
4+ import java .util .List ;
5+ import java .util .Set ;
6+
7+ public class MailService {
8+ private static final String OK = "OK" ;
9+
10+ private static final String INTERRUPTED_BY_FAULTS_NUMBER = "+++ Interrupted by faults number" ;
11+ private static final String INTERRUPTED_BY_TIMEOUT = "+++ Interrupted by timeout" ;
12+ private static final String INTERRUPTED_EXCEPTION = "+++ InterruptedException" ;
13+
14+ public GroupResult sendToList (final String template , final Set <String > emails ) throws Exception {
15+ return new GroupResult (0 , Collections .emptyList (), null );
16+ }
17+
18+
19+ // dummy realization
20+ public MailResult sendToUser (String template , String email ) throws Exception {
21+ try {
22+ Thread .sleep (500 ); //delay
23+ } catch (InterruptedException e ) {
24+ // log cancel;
25+ return null ;
26+ }
27+ return Math .random () < 0.7 ? MailResult .ok (email ) : MailResult .error (email , "Error" );
28+ }
29+
30+ public static class MailResult {
31+ private final String email ;
32+ private final String result ;
33+
34+ private static MailResult ok (String email ) {
35+ return new MailResult (email , OK );
36+ }
37+
38+ private static MailResult error (String email , String error ) {
39+ return new MailResult (email , error );
40+ }
41+
42+ public boolean isOk () {
43+ return OK .equals (result );
44+ }
45+
46+ private MailResult (String email , String cause ) {
47+ this .email = email ;
48+ this .result = cause ;
49+ }
50+
51+ @ Override
52+ public String toString () {
53+ return '(' + email + ',' + result + ')' ;
54+ }
55+ }
56+
57+ public static class GroupResult {
58+ private final int success ; // number of successfully sent email
59+ private final List <MailResult > failed ; // failed emails with causes
60+ private final String failedCause ; // global fail cause
61+
62+ public GroupResult (int success , List <MailResult > failed , String failedCause ) {
63+ this .success = success ;
64+ this .failed = failed ;
65+ this .failedCause = failedCause ;
66+ }
67+
68+ @ Override
69+ public String toString () {
70+ return "Success: " + success + '\n' +
71+ "Failed: " + failed .toString () + '\n' +
72+ (failedCause == null ? "" : "Failed cause" + failedCause );
73+ }
74+ }
75+ }
0 commit comments