GeezerVison is showing github.com — 508 : sonar qube critical issue fixes (#852) · coding0011/java-design-patterns@c6ecf58
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/iluwatar/abstractdocument test/java/com/iluwatar/abstractdocument
caching/src/main/java/com/iluwatar/caching cqrs/src/main/java/com/iluwatar/cqrs dao/src/main/java/com/iluwatar/dao data-mapper/src/main/java/com/iluwatar/datamapper double-dispatch/src/main/java/com/iluwatar/doubledispatch
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java +15-17Lines changed: 15 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | */ | 23 | 23 | package com.iluwatar.abstractdocument; | 24 | 24 |
| 25 | | -import com.iluwatar.abstractdocument.domain.Car; | 26 | | -import com.iluwatar.abstractdocument.domain.HasModel; | 27 | | -import com.iluwatar.abstractdocument.domain.HasParts; | 28 | | -import com.iluwatar.abstractdocument.domain.HasPrice; | 29 | | -import com.iluwatar.abstractdocument.domain.HasType; | 30 | | -import org.slf4j.Logger; | 31 | | -import org.slf4j.LoggerFactory; | 32 | | - | 33 | 25 | import java.util.Arrays; | 34 | 26 | import java.util.HashMap; | 35 | 27 | import java.util.Map; | 36 | 28 |
| | 29 | +import org.slf4j.Logger; | | 30 | +import org.slf4j.LoggerFactory; | | 31 | + | | 32 | +import com.iluwatar.abstractdocument.domain.Car; | | 33 | +import com.iluwatar.abstractdocument.domain.enums.Property; | | 34 | + | 37 | 35 | /** | 38 | 36 | * The Abstract Document pattern enables handling additional, non-static | 39 | 37 | * properties. This pattern uses concept of traits to enable type safety and | @@ -55,20 +53,20 @@ public App() { | 55 | 53 | LOGGER.info("Constructing parts and car"); | 56 | 54 |
| 57 | 55 | Map<String, Object> carProperties = new HashMap<>(); | 58 | | - carProperties.put(HasModel.PROPERTY, "300SL"); | 59 | | - carProperties.put(HasPrice.PROPERTY, 10000L); | | 56 | + carProperties.put(Property.MODEL.toString(), "300SL"); | | 57 | + carProperties.put(Property.PRICE.toString(), 10000L); | 60 | 58 |
| 61 | 59 | Map<String, Object> wheelProperties = new HashMap<>(); | 62 | | - wheelProperties.put(HasType.PROPERTY, "wheel"); | 63 | | - wheelProperties.put(HasModel.PROPERTY, "15C"); | 64 | | - wheelProperties.put(HasPrice.PROPERTY, 100L); | | 60 | + wheelProperties.put(Property.TYPE.toString(), "wheel"); | | 61 | + wheelProperties.put(Property.MODEL.toString(), "15C"); | | 62 | + wheelProperties.put(Property.PRICE.toString(), 100L); | 65 | 63 |
| 66 | 64 | Map<String, Object> doorProperties = new HashMap<>(); | 67 | | - doorProperties.put(HasType.PROPERTY, "door"); | 68 | | - doorProperties.put(HasModel.PROPERTY, "Lambo"); | 69 | | - doorProperties.put(HasPrice.PROPERTY, 300L); | | 65 | + doorProperties.put(Property.TYPE.toString(), "door"); | | 66 | + doorProperties.put(Property.MODEL.toString(), "Lambo"); | | 67 | + doorProperties.put(Property.PRICE.toString(), 300L); | 70 | 68 |
| 71 | | - carProperties.put(HasParts.PROPERTY, Arrays.asList(wheelProperties, doorProperties)); | | 69 | + carProperties.put(Property.PARTS.toString(), Arrays.asList(wheelProperties, doorProperties)); | 72 | 70 |
| 73 | 71 | Car car = new Car(carProperties); | 74 | 72 |
| |
Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasModel.java +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | import java.util.Optional; | 26 | 26 |
| 27 | 27 | import com.iluwatar.abstractdocument.Document; | | 28 | +import com.iluwatar.abstractdocument.domain.enums.Property; | 28 | 29 |
| 29 | 30 | /** | 30 | 31 | * HasModel trait for static access to 'model' property | 31 | 32 | */ | 32 | 33 | public interface HasModel extends Document { | 33 | 34 |
| 34 | | - String PROPERTY = "model"; | 35 | | - | 36 | 35 | default Optional<String> getModel() { | 37 | | - return Optional.ofNullable((String) get(PROPERTY)); | | 36 | + return Optional.ofNullable((String) get(Property.MODEL.toString())); | 38 | 37 | } | 39 | 38 |
| 40 | 39 | } |
Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasParts.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | import java.util.stream.Stream; | 26 | 26 |
| 27 | 27 | import com.iluwatar.abstractdocument.Document; | | 28 | +import com.iluwatar.abstractdocument.domain.enums.Property; | 28 | 29 |
| 29 | 30 | /** | 30 | 31 | * HasParts trait for static access to 'parts' property | 31 | 32 | */ | 32 | 33 | public interface HasParts extends Document { | 33 | 34 |
| 34 | | - String PROPERTY = "parts"; | 35 | 35 |
| 36 | 36 | default Stream<Part> getParts() { | 37 | | - return children(PROPERTY, Part::new); | | 37 | + return children(Property.PARTS.toString(), Part::new); | 38 | 38 | } | 39 | 39 |
| 40 | 40 | } |
Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasPrice.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | import java.util.Optional; | 26 | 26 |
| 27 | 27 | import com.iluwatar.abstractdocument.Document; | | 28 | +import com.iluwatar.abstractdocument.domain.enums.Property; | 28 | 29 |
| 29 | 30 | /** | 30 | 31 | * HasPrice trait for static access to 'price' property | 31 | 32 | */ | 32 | 33 | public interface HasPrice extends Document { | 33 | 34 |
| 34 | | - String PROPERTY = "price"; | 35 | 35 |
| 36 | 36 | default Optional<Number> getPrice() { | 37 | | - return Optional.ofNullable((Number) get(PROPERTY)); | | 37 | + return Optional.ofNullable((Number) get(Property.PRICE.toString())); | 38 | 38 | } | 39 | 39 |
| 40 | 40 | } |
Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasType.java +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | */ | 23 | 23 | package com.iluwatar.abstractdocument.domain; | 24 | 24 |
| 25 | | -import com.iluwatar.abstractdocument.Document; | 26 | | - | 27 | 25 | import java.util.Optional; | 28 | 26 |
| | 27 | +import com.iluwatar.abstractdocument.Document; | | 28 | +import com.iluwatar.abstractdocument.domain.enums.Property; | | 29 | + | 29 | 30 | /** | 30 | 31 | * HasType trait for static access to 'type' property | 31 | 32 | */ | 32 | 33 | public interface HasType extends Document { | 33 | 34 |
| 34 | | - String PROPERTY = "type"; | 35 | 35 |
| 36 | 36 | default Optional<String> getType() { | 37 | | - return Optional.ofNullable((String) get(PROPERTY)); | | 37 | + return Optional.ofNullable((String) get(Property.TYPE.toString())); | 38 | 38 | } | 39 | 39 |
| 40 | 40 | } |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.iluwatar.abstractdocument.domain.enums; | | 2 | + | | 3 | +/** | | 4 | + * | | 5 | + * Enum To Describe Property type | | 6 | + * | | 7 | + */ | | 8 | +public enum Property { | | 9 | + | | 10 | + PARTS, TYPE, PRICE, MODEL | | 11 | +} |
Collapse file Copy file name to clipboardExpand all lines: abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java +12-14Lines changed: 12 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | */ | 23 | 23 | package com.iluwatar.abstractdocument; | 24 | 24 |
| 25 | | -import com.iluwatar.abstractdocument.domain.Car; | 26 | | -import com.iluwatar.abstractdocument.domain.HasModel; | 27 | | -import com.iluwatar.abstractdocument.domain.HasParts; | 28 | | -import com.iluwatar.abstractdocument.domain.HasPrice; | 29 | | -import com.iluwatar.abstractdocument.domain.HasType; | 30 | | -import com.iluwatar.abstractdocument.domain.Part; | 31 | | -import org.junit.jupiter.api.Test; | | 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 32 | 26 |
| 33 | 27 | import java.util.Arrays; | 34 | 28 | import java.util.HashMap; | 35 | 29 | import java.util.Map; | 36 | 30 |
| 37 | | -import static org.junit.jupiter.api.Assertions.assertEquals; | | 31 | +import org.junit.jupiter.api.Test; | | 32 | + | | 33 | +import com.iluwatar.abstractdocument.domain.Car; | | 34 | +import com.iluwatar.abstractdocument.domain.Part; | | 35 | +import com.iluwatar.abstractdocument.domain.enums.Property; | 38 | 36 |
| 39 | 37 | /** | 40 | 38 | * Test for Part and Car | @@ -51,9 +49,9 @@ public class DomainTest { | 51 | 49 | @Test | 52 | 50 | public void shouldConstructPart() { | 53 | 51 | Map<String, Object> partProperties = new HashMap<>(); | 54 | | - partProperties.put(HasType.PROPERTY, TEST_PART_TYPE); | 55 | | - partProperties.put(HasModel.PROPERTY, TEST_PART_MODEL); | 56 | | - partProperties.put(HasPrice.PROPERTY, TEST_PART_PRICE); | | 52 | + partProperties.put(Property.TYPE.toString(), TEST_PART_TYPE); | | 53 | + partProperties.put(Property.MODEL.toString(), TEST_PART_MODEL); | | 54 | + partProperties.put(Property.PRICE.toString(), TEST_PART_PRICE); | 57 | 55 | Part part = new Part(partProperties); | 58 | 56 |
| 59 | 57 | assertEquals(TEST_PART_TYPE, part.getType().get()); | @@ -64,9 +62,9 @@ public void shouldConstructPart() { | 64 | 62 | @Test | 65 | 63 | public void shouldConstructCar() { | 66 | 64 | Map<String, Object> carProperties = new HashMap<>(); | 67 | | - carProperties.put(HasModel.PROPERTY, TEST_CAR_MODEL); | 68 | | - carProperties.put(HasPrice.PROPERTY, TEST_CAR_PRICE); | 69 | | - carProperties.put(HasParts.PROPERTY, Arrays.asList(new HashMap<>(), new HashMap<>())); | | 65 | + carProperties.put(Property.MODEL.toString(), TEST_CAR_MODEL); | | 66 | + carProperties.put(Property.PRICE.toString(), TEST_CAR_PRICE); | | 67 | + carProperties.put(Property.PARTS.toString(), Arrays.asList(new HashMap<>(), new HashMap<>())); | 70 | 68 | Car car = new Car(carProperties); | 71 | 69 |
| 72 | 70 | assertEquals(TEST_CAR_MODEL, car.getModel().get()); | |
Collapse file Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/DbManager.java +16-13Lines changed: 16 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 |
| 29 | 29 | import org.bson.Document; | 30 | 30 |
| | 31 | +import com.iluwatar.caching.constants.CachingConstants; | 31 | 32 | import com.mongodb.MongoClient; | 32 | 33 | import com.mongodb.client.FindIterable; | 33 | 34 | import com.mongodb.client.MongoDatabase; | @@ -90,12 +91,12 @@ public static UserAccount readFromDb(String userId) { | 90 | 91 | } | 91 | 92 | } | 92 | 93 | FindIterable<Document> iterable = | 93 | | - db.getCollection("user_accounts").find(new Document("userID", userId)); | | 94 | + db.getCollection(CachingConstants.USER_ACCOUNT).find(new Document(CachingConstants.USER_ID, userId)); | 94 | 95 | if (iterable == null) { | 95 | 96 | return null; | 96 | 97 | } | 97 | 98 | Document doc = iterable.first(); | 98 | | - return new UserAccount(userId, doc.getString("userName"), doc.getString("additionalInfo")); | | 99 | + return new UserAccount(userId, doc.getString(CachingConstants.USER_NAME), doc.getString(CachingConstants.ADD_INFO)); | 99 | 100 | } | 100 | 101 |
| 101 | 102 | /** | @@ -113,9 +114,9 @@ public static void writeToDb(UserAccount userAccount) { | 113 | 114 | e.printStackTrace(); | 114 | 115 | } | 115 | 116 | } | 116 | | - db.getCollection("user_accounts").insertOne( | 117 | | - new Document("userID", userAccount.getUserId()).append("userName", | 118 | | - userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())); | | 117 | + db.getCollection(CachingConstants.USER_ACCOUNT).insertOne( | | 118 | + new Document(CachingConstants.USER_ID ,userAccount.getUserId()).append(CachingConstants.USER_NAME, | | 119 | + userAccount.getUserName()).append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())); | 119 | 120 | } | 120 | 121 |
| 121 | 122 | /** | @@ -133,10 +134,10 @@ public static void updateDb(UserAccount userAccount) { | 133 | 134 | e.printStackTrace(); | 134 | 135 | } | 135 | 136 | } | 136 | | - db.getCollection("user_accounts").updateOne( | 137 | | - new Document("userID", userAccount.getUserId()), | 138 | | - new Document("$set", new Document("userName", userAccount.getUserName()).append( | 139 | | - "additionalInfo", userAccount.getAdditionalInfo()))); | | 137 | + db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( | | 138 | + new Document(CachingConstants.USER_ID, userAccount.getUserId()), | | 139 | + new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName()) | | 140 | + .append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()))); | 140 | 141 | } | 141 | 142 |
| 142 | 143 | /** | @@ -155,10 +156,12 @@ public static void upsertDb(UserAccount userAccount) { | 155 | 156 | e.printStackTrace(); | 156 | 157 | } | 157 | 158 | } | 158 | | - db.getCollection("user_accounts").updateOne( | 159 | | - new Document("userID", userAccount.getUserId()), | 160 | | - new Document("$set", new Document("userID", userAccount.getUserId()).append("userName", | 161 | | - userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())), | | 159 | + db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( | | 160 | + new Document(CachingConstants.USER_ID, userAccount.getUserId()), | | 161 | + new Document("$set", | | 162 | + new Document(CachingConstants.USER_ID, userAccount.getUserId()) | | 163 | + .append(CachingConstants.USER_NAME, userAccount.getUserName()).append(CachingConstants.ADD_INFO, | | 164 | + userAccount.getAdditionalInfo())), | 162 | 165 | new UpdateOptions().upsert(true)); | 163 | 166 | } | 164 | 167 | } |
Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.iluwatar.caching.constants; | | 2 | + | | 3 | +/** | | 4 | + * | | 5 | + * Constant class for defining constants | | 6 | + * | | 7 | + */ | | 8 | +public class CachingConstants { | | 9 | + | | 10 | + public static final String USER_ACCOUNT = "user_accounts"; | | 11 | + public static final String USER_ID = "userID"; | | 12 | + public static final String USER_NAME = "userName"; | | 13 | + public static final String ADD_INFO = "additionalInfo"; | | 14 | + | | 15 | +} |
Collapse file Copy file name to clipboardExpand all lines: cqrs/src/main/java/com/iluwatar/cqrs/app/App.java +14-13Lines changed: 14 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 |
| 31 | 31 | import com.iluwatar.cqrs.commandes.CommandServiceImpl; | 32 | 32 | import com.iluwatar.cqrs.commandes.ICommandService; | | 33 | +import com.iluwatar.cqrs.constants.AppConstants; | 33 | 34 | import com.iluwatar.cqrs.dto.Author; | 34 | 35 | import com.iluwatar.cqrs.dto.Book; | 35 | 36 | import com.iluwatar.cqrs.queries.IQueryService; | @@ -60,27 +61,27 @@ public static void main(String[] args) { | 60 | 61 | ICommandService commands = new CommandServiceImpl(); | 61 | 62 |
| 62 | 63 | // Create Authors and Books using CommandService | 63 | | - | 64 | | - | 65 | | - | | 64 | + | | 65 | + | | 66 | + | 66 | 67 |
| 67 | | - commands.bookAddedToAuthor("Domain-Driven Design", 60.08, "eEvans"); | 68 | | - commands.bookAddedToAuthor("Effective Java", 40.54, "jBloch"); | 69 | | - commands.bookAddedToAuthor("Java Puzzlers", 39.99, "jBloch"); | 70 | | - commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, "jBloch"); | 71 | | - commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, "mFowler"); | 72 | | - commands.bookAddedToAuthor("Domain Specific Languages", 48.89, "mFowler"); | 73 | | - commands.authorNameUpdated("eEvans", "Eric J. Evans"); | | 68 | + commands.bookAddedToAuthor("Domain-Driven Design", 60.08, AppConstants.E_EVANS); | | 69 | + commands.bookAddedToAuthor("Effective Java", 40.54, AppConstants.J_BLOCH); | | 70 | + commands.bookAddedToAuthor("Java Puzzlers", 39.99, AppConstants.J_BLOCH); | | 71 | + commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, AppConstants.J_BLOCH); | | 72 | + commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, AppConstants.M_FOWLER); | | 73 | + commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER); | | 74 | + commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans"); | 74 | 75 |
| 75 | 76 | IQueryService queries = new QueryServiceImpl(); | 76 | 77 |
| 77 | 78 | // Query the database using QueryService | 78 | 79 | Author nullAuthor = queries.getAuthorByUsername("username"); | 79 | | - Author eEvans = queries.getAuthorByUsername("eEvans"); | 80 | | - BigInteger jBlochBooksCount = queries.getAuthorBooksCount("jBloch"); | | 80 | + Author eEvans = queries.getAuthorByUsername(AppConstants.E_EVANS); | | 81 | + BigInteger jBlochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH); | 81 | 82 | BigInteger authorsCount = queries.getAuthorsCount(); | 82 | 83 | Book dddBook = queries.getBook("Domain-Driven Design"); | 83 | | - List<Book> jBlochBooks = queries.getAuthorBooks("jBloch"); | | 84 | + List<Book> jBlochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH); | 84 | 85 |
| 85 | 86 | LOGGER.info("Author username : {}", nullAuthor); | 86 | 87 | LOGGER.info("Author eEvans : {}", eEvans); | |
|