Skip to content

Commit 8529d6e

Browse files
committed
First review changes++
First review changes++
1 parent 06e0a15 commit 8529d6e

File tree

9 files changed

+118
-249
lines changed

9 files changed

+118
-249
lines changed

data-mapper/etc/data-mapper.png

-13.6 KB
Loading

data-mapper/pom.xml

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,61 @@
2323
THE SOFTWARE.
2424
2525
-->
26-
<project
27-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
28-
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
29-
<modelVersion>4.0.0</modelVersion>
30-
<parent>
31-
<groupId>com.iluwatar</groupId>
32-
<artifactId>java-design-patterns</artifactId>
33-
<version>1.11.0-SNAPSHOT</version>
34-
</parent>
35-
<artifactId>data-mapper</artifactId>
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.11.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>data-mapper</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>log4j</groupId>
42+
<artifactId>log4j</artifactId>
43+
</dependency>
44+
</dependencies>
3645

37-
<dependencies>
38-
<dependency>
39-
<groupId>junit</groupId>
40-
<artifactId>junit</artifactId>
41-
<scope>test</scope>
42-
</dependency>
43-
<dependency>
44-
<groupId>log4j</groupId>
45-
<artifactId>log4j</artifactId>
46-
</dependency>
47-
</dependencies>
46+
<build>
4847

49-
<build>
48+
<!-- log4j.xml file will be copied both in ${project.build.outputDirectory} and ${project.build.directory}. Thanks to Sean Patrick Floyd (http://stackoverflow.com/questions/5637532/maven-how-to-place-resource-file-together-with-jar) -->
49+
<resources>
50+
<resource> <!-- regular processing for every resource file -->
51+
<directory>src/main/resources</directory>
52+
</resource>
53+
<resource> <!-- processing with a different output directory for log4j.xml -->
54+
<directory>src/main/resources</directory>
55+
<includes>
56+
<include>log4j.xml</include>
57+
</includes>
58+
<targetPath>..</targetPath> <!-- relative to target/classes i.e. ${project.build.directory} -->
59+
</resource>
60+
</resources>
5061

51-
<!--
52-
log4j.xml file will be copied both in ${project.build.outputDirectory}
53-
and ${project.build.directory}. Thanks to Sean Patrick Floyd
54-
(http://stackoverflow.com/questions/5637532/maven-how-to-place-resource-file-together-with-jar)
55-
-->
56-
57-
<resources>
58-
<resource> <!-- regular processing for every resource file -->
59-
<directory>src/main/resources</directory>
60-
</resource>
61-
<resource> <!-- processing with a different output directory for log4j.xml -->
62-
<directory>src/main/resources</directory>
63-
<includes>
64-
<include>log4j.xml</include>
65-
</includes>
66-
<targetPath>..</targetPath> <!-- relative to target/classes i.e. ${project.build.directory} -->
67-
</resource>
68-
</resources>
62+
<plugins>
6963

70-
<plugins>
64+
<!-- This will exclude log4j.xml file from generated JAR -->
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
<version>2.6</version>
69+
<configuration>
70+
<excludes>
71+
<exclude>log4j.xml</exclude>
72+
</excludes>
73+
<archive>
74+
<manifest>
75+
<addClasspath>true</addClasspath>
76+
</manifest>
77+
</archive>
78+
</configuration>
79+
</plugin>
7180

72-
<!--
73-
This will exclude log4j.xml file from generated JAR
74-
-->
75-
<plugin>
76-
<groupId>org.apache.maven.plugins</groupId>
77-
<artifactId>maven-jar-plugin</artifactId>
78-
<version>2.6</version>
79-
<configuration>
80-
<excludes>
81-
<exclude>log4j.xml</exclude>
82-
</excludes>
83-
<archive>
84-
<manifest>
85-
<addClasspath>true</addClasspath>
86-
</manifest>
87-
</archive>
88-
</configuration>
89-
</plugin>
90-
91-
</plugins>
92-
</build>
81+
</plugins>
82+
</build>
9383
</project>

data-mapper/src/main/java/com/iluwatar/datamapper/App.java

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* The MIT License Copyright (c) 2014 Ilkka Seppälä
2+
* The MIT License Copyright (c) 2016 Amit Dixit
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
55
* associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -37,75 +37,39 @@ public final class App {
3737

3838
private static Logger log = Logger.getLogger(App.class);
3939

40-
private static final String DB_TYPE_FIRST = "first";
41-
private static final String DB_TYPE_SECOND = "second";
42-
4340
/**
4441
* Program entry point.
4542
*
4643
* @param args command line args.
4744
*/
4845
public static void main(final String... args) {
4946

50-
if (log.isInfoEnabled() & args.length > 0) {
51-
log.debug("App.main(), type: " + args[0]);
52-
}
53-
54-
StudentDataMapper mapper = null;
55-
56-
/* Check the desired db type from runtime arguments */
57-
if (args.length == 0) {
58-
59-
/* Create default data mapper for mysql */
60-
mapper = new StudentFirstDataMapper();
61-
62-
} else if (args.length > 0 && DB_TYPE_FIRST.equalsIgnoreCase(args[0])) {
63-
64-
/* Create new data mapper for type 'first' */
65-
mapper = new StudentFirstDataMapper();
66-
67-
} else if (args.length > 0 && DB_TYPE_SECOND.equalsIgnoreCase(args[0])) {
68-
69-
/* Create new data mapper for type 'second' */
70-
mapper = new StudentSecondDataMapper();
71-
} else {
72-
73-
/* Don't couple any Data Mapper to java.sql.SQLException */
74-
throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported");
75-
}
47+
/* Create new data mapper for type 'first' */
48+
final StudentDataMapper mapper = new StudentDataMapperImpl();
7649

7750
/* Create new student */
7851
Student student = new Student(1, "Adam", 'A');
7952

80-
/* Add student in respectibe db */
53+
/* Add student in respectibe store */
8154
mapper.insert(student);
8255

83-
if (log.isDebugEnabled()) {
84-
log.debug("App.main(), student : " + student + ", is inserted");
85-
}
56+
log.debug("App.main(), student : " + student + ", is inserted");
8657

8758
/* Find this student */
8859
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
8960

90-
if (log.isDebugEnabled()) {
91-
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
92-
}
61+
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
9362

9463
/* Update existing student object */
9564
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
9665

9766
/* Update student in respectibe db */
9867
mapper.update(student);
9968

100-
if (log.isDebugEnabled()) {
101-
log.debug("App.main(), student : " + student + ", is updated");
102-
}
69+
log.debug("App.main(), student : " + student + ", is updated");
70+
log.debug("App.main(), student : " + student + ", is going to be deleted");
10371

10472
/* Delete student in db */
105-
106-
if (log.isDebugEnabled()) {
107-
log.debug("App.main(), student : " + student + ", is deleted");
108-
}
10973
mapper.delete(student);
11074
}
11175

data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package com.iluwatar.datamapper;
2020

2121
/**
22+
* Using Runtime Exception for avoiding dependancy on implementation exceptions. This helps in
23+
* decoupling.
2224
*
2325
* @author amit.dixit
2426
*

data-mapper/src/main/java/com/iluwatar/datamapper/StudentFirstDataMapper.java renamed to data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapperImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323
import java.util.Optional;
2424

25-
public final class StudentFirstDataMapper implements StudentDataMapper {
25+
public final class StudentDataMapperImpl implements StudentDataMapper {
2626

2727
/* Note: Normally this would be in the form of an actual database */
2828
private List<Student> students = new ArrayList<>();
@@ -59,7 +59,7 @@ public void update(Student studentToBeUpdated) throws DataMapperException {
5959

6060
} else {
6161

62-
/* Throw user error */
62+
/* Throw user error after wrapping in a runtime exception */
6363
throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found");
6464
}
6565
}
@@ -75,7 +75,7 @@ public void insert(Student studentToBeInserted) throws DataMapperException {
7575

7676
} else {
7777

78-
/* Throw user error */
78+
/* Throw user error after wrapping in a runtime exception */
7979
throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists");
8080
}
8181
}
@@ -91,7 +91,7 @@ public void delete(Student studentToBeDeleted) throws DataMapperException {
9191

9292
} else {
9393

94-
/* Throw user error */
94+
/* Throw user error after wrapping in a runtime exception */
9595
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
9696
}
9797
}

data-mapper/src/main/java/com/iluwatar/datamapper/StudentSecondDataMapper.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* The MIT License Copyright (c) 2016 Amit Dixit
3-
*
43
*
54
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
65
* associated documentation files (the "Software"), to deal in the Software without restriction,

0 commit comments

Comments
 (0)