KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jester > SimpleIntCodeMangler


1 package jester;
2
3 public class SimpleIntCodeMangler extends SimpleCodeMangler {
4     public SimpleIntCodeMangler(ClassSourceCodeChanger sourceCodeSystem) {
5         super(sourceCodeSystem);
6     }
7     static int charAsInt(char c) {
8         return c - '0';
9     }
10     String JavaDoc incremented(int i) {
11         if (i == 9) {
12             return "0";
13         }
14         return String.valueOf(i + 1);
15     }
16     int indexOfNumber(IgnoreListDocument s) {
17         int startIndex = getIndexOfLastChange() == -1 ? 0 : getIndexOfLastChange() + 1;
18         for (int index = startIndex; index < s.length(); index++) {
19             char characterToCheck = s.charAt(index);
20             if (Character.isDigit(characterToCheck)){
21                 if(index > 0){
22                     char precedingCharacter = s.charAt(index - 1);
23                     boolean ignoreThisBecauseItIsEitherWrongToMutateOrTheNumberHasAlreadyBeenMutated = Character.isLetterOrDigit(precedingCharacter);
24                     if(ignoreThisBecauseItIsEitherWrongToMutateOrTheNumberHasAlreadyBeenMutated){
25                         continue;
26                     }
27                 }
28                 return index;
29             }
30         }
31         return -1;
32     }
33     boolean makeSomeChangeToFileSource() throws SourceChangeException {
34         int index = indexOfNumber(getOriginalContents());
35         if (index == -1) {
36             return false;
37         }
38
39         char valueCharacterChangedFrom = getOriginalContents().charAt(index);
40         int originalNumber = charAsInt(valueCharacterChangedFrom);
41         String JavaDoc valueCharacterChangedTo = incremented(originalNumber);
42
43         return makeChange(index, String.valueOf(valueCharacterChangedFrom), valueCharacterChangedTo);
44     }
45 }
Popular Tags