Skip to content

Commit 0d508df

Browse files
committed
This code is a part of the Chapter 14 of Clean Code book. This is an attempt to clean this clean code. This is the first of the many commits.
- This commit replaces the ArgsException with InvalidArgumentException and InvalidSchemaException
1 parent 77e6426 commit 0d508df

19 files changed

Lines changed: 646 additions & 209 deletions

.classpath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="src" path="test"/>
6+
<classpathentry exported="true" kind="lib" path="/Users/venks/lib/junit.jar"/>
7+
<classpathentry exported="true" kind="lib" path="/Users/venks/lib/lombok.jar"/>
8+
<classpathentry kind="output" path="bin"/>
9+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>javaargs</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 316 additions & 0 deletions
Large diffs are not rendered by default.

.settings/org.eclipse.jdt.ui.prefs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
cleanup.add_default_serial_version_id=false
2+
cleanup.add_generated_serial_version_id=true
3+
cleanup.add_missing_annotations=true
4+
cleanup.add_missing_deprecated_annotations=true
5+
cleanup.add_missing_methods=false
6+
cleanup.add_missing_nls_tags=false
7+
cleanup.add_missing_override_annotations=true
8+
cleanup.add_missing_override_annotations_interface_methods=true
9+
cleanup.add_serial_version_id=true
10+
cleanup.always_use_blocks=true
11+
cleanup.always_use_parentheses_in_expressions=true
12+
cleanup.always_use_this_for_non_static_field_access=false
13+
cleanup.always_use_this_for_non_static_method_access=false
14+
cleanup.convert_functional_interfaces=false
15+
cleanup.convert_to_enhanced_for_loop=true
16+
cleanup.correct_indentation=true
17+
cleanup.format_source_code=true
18+
cleanup.format_source_code_changes_only=false
19+
cleanup.insert_inferred_type_arguments=false
20+
cleanup.make_local_variable_final=true
21+
cleanup.make_parameters_final=true
22+
cleanup.make_private_fields_final=true
23+
cleanup.make_type_abstract_if_missing_method=false
24+
cleanup.make_variable_declarations_final=true
25+
cleanup.never_use_blocks=false
26+
cleanup.never_use_parentheses_in_expressions=false
27+
cleanup.organize_imports=true
28+
cleanup.qualify_static_field_accesses_with_declaring_class=false
29+
cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true
30+
cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true
31+
cleanup.qualify_static_member_accesses_with_declaring_class=true
32+
cleanup.qualify_static_method_accesses_with_declaring_class=false
33+
cleanup.remove_private_constructors=true
34+
cleanup.remove_redundant_type_arguments=false
35+
cleanup.remove_trailing_whitespaces=true
36+
cleanup.remove_trailing_whitespaces_all=true
37+
cleanup.remove_trailing_whitespaces_ignore_empty=false
38+
cleanup.remove_unnecessary_casts=true
39+
cleanup.remove_unnecessary_nls_tags=true
40+
cleanup.remove_unused_imports=true
41+
cleanup.remove_unused_local_variables=false
42+
cleanup.remove_unused_private_fields=true
43+
cleanup.remove_unused_private_members=false
44+
cleanup.remove_unused_private_methods=true
45+
cleanup.remove_unused_private_types=true
46+
cleanup.sort_members=true
47+
cleanup.sort_members_all=false
48+
cleanup.use_anonymous_class_creation=false
49+
cleanup.use_blocks=true
50+
cleanup.use_blocks_only_for_return_and_throw=false
51+
cleanup.use_lambda=true
52+
cleanup.use_parentheses_in_expressions=true
53+
cleanup.use_this_for_non_static_field_access=true
54+
cleanup.use_this_for_non_static_field_access_only_if_necessary=true
55+
cleanup.use_this_for_non_static_method_access=true
56+
cleanup.use_this_for_non_static_method_access_only_if_necessary=true
57+
cleanup_profile=_eclipse-venks
58+
cleanup_settings_version=2
59+
eclipse.preferences.version=1
60+
formatter_profile=_Java Conventions [built-in] - venks
61+
formatter_settings_version=13

src/com/cleancoder/args/Args.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@
22

33
import java.util.*;
44

5-
import static com.cleancoder.args.ArgsException.ErrorCode.*;
5+
import static com.cleancoder.args.InvalidSchemaException.ErrorCode.*;
6+
import static com.cleancoder.args.InvalidArgumentException.ErrorCode.*;
67

78
public class Args {
89
private Map<Character, ArgumentMarshaler> marshalers;
910
private Set<Character> argsFound;
1011
private ListIterator<String> currentArgument;
1112

12-
public Args(String schema, String[] args) throws ArgsException {
13+
public Args(String schema, String[] args) {
1314
marshalers = new HashMap<Character, ArgumentMarshaler>();
1415
argsFound = new HashSet<Character>();
1516

1617
parseSchema(schema);
1718
parseArgumentStrings(Arrays.asList(args));
1819
}
1920

20-
private void parseSchema(String schema) throws ArgsException {
21+
private void parseSchema(String schema) {
2122
for (String element : schema.split(","))
2223
if (element.length() > 0)
2324
parseSchemaElement(element.trim());
2425
}
2526

26-
private void parseSchemaElement(String element) throws ArgsException {
27+
private void parseSchemaElement(String element) {
2728
char elementId = element.charAt(0);
2829
String elementTail = element.substring(1);
2930
validateSchemaElementId(elementId);
@@ -40,15 +41,15 @@ else if (elementTail.equals("[*]"))
4041
else if (elementTail.equals("&"))
4142
marshalers.put(elementId, new MapArgumentMarshaler());
4243
else
43-
throw new ArgsException(INVALID_ARGUMENT_FORMAT, elementId, elementTail);
44+
throw new InvalidSchemaException(UNSUPPORTED_SCHEMA_TYPE, elementId, elementTail);
4445
}
4546

46-
private void validateSchemaElementId(char elementId) throws ArgsException {
47+
private void validateSchemaElementId(char elementId) {
4748
if (!Character.isLetter(elementId))
48-
throw new ArgsException(INVALID_ARGUMENT_NAME, elementId, null);
49+
throw new InvalidArgumentException(INVALID_ARGUMENT_NAME, elementId);
4950
}
5051

51-
private void parseArgumentStrings(List<String> argsList) throws ArgsException {
52+
private void parseArgumentStrings(List<String> argsList) {
5253
for (currentArgument = argsList.listIterator(); currentArgument.hasNext();) {
5354
String argString = currentArgument.next();
5455
if (argString.startsWith("-")) {
@@ -60,21 +61,21 @@ private void parseArgumentStrings(List<String> argsList) throws ArgsException {
6061
}
6162
}
6263

63-
private void parseArgumentCharacters(String argChars) throws ArgsException {
64+
private void parseArgumentCharacters(String argChars) {
6465
for (int i = 0; i < argChars.length(); i++)
6566
parseArgumentCharacter(argChars.charAt(i));
6667
}
6768

68-
private void parseArgumentCharacter(char argChar) throws ArgsException {
69+
private void parseArgumentCharacter(char argChar) {
6970
ArgumentMarshaler m = marshalers.get(argChar);
7071
if (m == null) {
71-
throw new ArgsException(UNEXPECTED_ARGUMENT, argChar, null);
72+
throw new InvalidArgumentException(UNEXPECTED_ARGUMENT, argChar);
7273
} else {
7374
argsFound.add(argChar);
7475
try {
75-
m.set(currentArgument);
76-
} catch (ArgsException e) {
77-
e.setErrorArgumentId(argChar);
76+
m.set(currentArgument, argChar);
77+
} catch (InvalidArgumentException e) {
78+
//e.setErrorArgumentId(argChar);
7879
throw e;
7980
}
8081
}

src/com/cleancoder/args/ArgsException.java

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

src/com/cleancoder/args/ArgumentMarshaler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import java.util.Iterator;
44

55
public interface ArgumentMarshaler {
6-
void set(Iterator<String> currentArgument) throws ArgsException;
6+
void set(Iterator<String> argumentValue, final char argument);
77
}

src/com/cleancoder/args/BooleanArgumentMarshaler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class BooleanArgumentMarshaler implements ArgumentMarshaler {
66
private boolean booleanValue = false;
77

8-
public void set(Iterator<String> currentArgument) throws ArgsException {
8+
public void set(Iterator<String> argumentValue, final char argument) {
99
booleanValue = true;
1010
}
1111

src/com/cleancoder/args/DoubleArgumentMarshaler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.cleancoder.args;
22

3-
import static com.cleancoder.args.ArgsException.ErrorCode.*;
3+
import static com.cleancoder.args.InvalidArgumentException.ErrorCode.*;
44

55
import java.util.*;
66

77
public class DoubleArgumentMarshaler implements ArgumentMarshaler {
88
private double doubleValue = 0;
99

10-
public void set(Iterator<String> currentArgument) throws ArgsException {
10+
public void set(Iterator<String> argumentValue, final char argument) {
1111
String parameter = null;
1212
try {
13-
parameter = currentArgument.next();
13+
parameter = argumentValue.next();
1414
doubleValue = Double.parseDouble(parameter);
1515
} catch (NoSuchElementException e) {
16-
throw new ArgsException(MISSING_DOUBLE);
16+
throw new InvalidArgumentException(MISSING_DOUBLE, argument);
1717
} catch (NumberFormatException e) {
18-
throw new ArgsException(INVALID_DOUBLE, parameter);
18+
throw new InvalidArgumentException(INVALID_DOUBLE, argument);
1919
}
2020
}
2121

src/com/cleancoder/args/IntegerArgumentMarshaler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.cleancoder.args;
22

3-
import static com.cleancoder.args.ArgsException.ErrorCode.*;
3+
import static com.cleancoder.args.InvalidArgumentException.ErrorCode.*;
44

55
import java.util.*;
66

77
public class IntegerArgumentMarshaler implements ArgumentMarshaler {
88
private int intValue = 0;
99

10-
public void set(Iterator<String> currentArgument) throws ArgsException {
10+
public void set(Iterator<String> argumentValue, final char argument) {
1111
String parameter = null;
1212
try {
13-
parameter = currentArgument.next();
13+
parameter = argumentValue.next();
1414
intValue = Integer.parseInt(parameter);
1515
} catch (NoSuchElementException e) {
16-
throw new ArgsException(MISSING_INTEGER);
16+
throw new InvalidArgumentException(MISSING_INTEGER, argument);
1717
} catch (NumberFormatException e) {
18-
throw new ArgsException(INVALID_INTEGER, parameter);
18+
throw new InvalidArgumentException(INVALID_INTEGER, argument);
1919
}
2020
}
2121

0 commit comments

Comments
 (0)