|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.iluwatar.hexagonal.domain; |
| 24 | + |
| 25 | +import java.util.EnumSet; |
| 26 | + |
| 27 | +/** |
| 28 | + * |
| 29 | + * Immutable value object for passing lottery ticket submit results. |
| 30 | + * |
| 31 | + */ |
| 32 | +public class LotteryTicketSubmitResult { |
| 33 | + |
| 34 | + /** |
| 35 | + * |
| 36 | + * Indicates if the submit was successful |
| 37 | + * |
| 38 | + */ |
| 39 | + public enum Result {OK, ERROR}; |
| 40 | + |
| 41 | + /** |
| 42 | + * |
| 43 | + * Indicates the fields in the lottery ticket that have errors. |
| 44 | + * |
| 45 | + */ |
| 46 | + public enum Fields {EMAIL, BANK_ACCOUNT, PHONE, NUMBERS}; |
| 47 | + |
| 48 | + private final Result result; |
| 49 | + |
| 50 | + private final EnumSet<Fields> fields; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor. |
| 54 | + */ |
| 55 | + public LotteryTicketSubmitResult(Result submitResult) { |
| 56 | + result = submitResult; |
| 57 | + fields = EnumSet.noneOf(Fields.class); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor. |
| 62 | + */ |
| 63 | + public LotteryTicketSubmitResult(Result submitResult, EnumSet<Fields> errorFields) { |
| 64 | + result = submitResult; |
| 65 | + fields = EnumSet.copyOf(errorFields); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return submit result |
| 70 | + */ |
| 71 | + public Result getResult() { |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return fields that have errors |
| 77 | + */ |
| 78 | + public EnumSet<Fields> getErrorFields() { |
| 79 | + return EnumSet.copyOf(fields); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + final int prime = 31; |
| 85 | + int result = 1; |
| 86 | + result = prime * result + ((fields == null) ? 0 : fields.hashCode()); |
| 87 | + result = prime * result + ((this.result == null) ? 0 : this.result.hashCode()); |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object obj) { |
| 93 | + if (this == obj) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + if (obj == null) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + if (getClass() != obj.getClass()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + LotteryTicketSubmitResult other = (LotteryTicketSubmitResult) obj; |
| 103 | + if (fields == null) { |
| 104 | + if (other.fields != null) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } else if (!fields.equals(other.fields)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if (result != other.result) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | +} |
0 commit comments