Skip to content

Instantly share code, notes, and snippets.

@TKolbusz
Last active August 8, 2018 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TKolbusz/1862bfa57b506a7b5e0afde4ea3e371f to your computer and use it in GitHub Desktop.
Save TKolbusz/1862bfa57b506a7b5e0afde4ea3e371f to your computer and use it in GitHub Desktop.
//setup
// dir for script to work in
String DIR = "/your/project/dir";
// character that have to occur before target variable
List<Character> charactersBeforeVariable = Arrays.asList(
' ',
'(',
'.',
'!',
'-',
',',
'[',
'=',
'*',
'+',
';',
'{',
'|',
'>',
'<',
'}',
')'
);
// variable prefixes(mVariable and sStatic)
List<String> prefixes = Arrays.asList("m", "s");
// main function
public void removeHungarianNotation() throws Exception {
File targetDir = new File(DIR);
if(!targetDir.exists())
throw new IllegalArgumentException("Given directory " + DIR + " does not exist");
// all files in dir
Collection<File> files = getAllFilesInDir(targetDir);
// files we will work on
Collection<File> filteredFiles = files.stream()
// filter out unwanted files
.filter(file -> {
String absolutePath = file.getAbsolutePath();
return absolutePath.endsWith(".java") &&
!absolutePath.contains("build") &&
!absolutePath.contains(".git");
})
.collect(Collectors.toList());
for (File file : filteredFiles) {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
StringBuilder newLinesOfAFile = new StringBuilder();
String newLine;
// loop through all lines in a given file
while ((newLine = bufferedReader.readLine()) != null) {
String lineBeforeChange = newLine;
// check for all characters before variable
for (Character characterBeforeVariable : charactersBeforeVariable) {
// check all prefixes
for (String prefix : prefixes) {
newLine = removeHungarianNotationFromLine(
newLine,
characterBeforeVariable,
prefix
);
}
}
if (!lineBeforeChange.equals(newLine)) {
// log changes to console
System.out.println(lineBeforeChange + " has been replaced with " + newLine);
}
newLinesOfAFile.append(newLine);
newLinesOfAFile.append("\n");
}
// replace old file content with new content
FileWriter fw = new FileWriter(file, false);
fw.append(newLinesOfAFile.toString());
fw.close();
}
}
public Collection<File> getAllFilesInDir(File dir) {
Set<File> fileTree = new HashSet<>();
if (dir == null || dir.listFiles() == null) {
return fileTree;
}
for (File entry : dir.listFiles()) {
if (entry.isFile()) {
fileTree.add(entry);
} else {
fileTree.addAll(getAllFilesInDir(entry));
}
}
return fileTree;
}
public String removeHungarianNotationFromLine(String oldLine, Character characterBeforeVariable, String prefix) {
String newLine = oldLine;
// loop through characters in line
for(int index = 0; index < oldLine.length(); index += 2){
// look for target in given line
index = oldLine.indexOf(characterBeforeVariable + prefix, index);
// mVariable have to exist and have to be at least 3 characters long(counting with characterBeforeVariable)
if (index!=-1 && index + 3 < oldLine.length()) {
// index 0 is characterBeforeVariable, index 1 is m or s
char character = oldLine.charAt(index + 2); // potential capital letter in variable
if (character >= 'A' && character <= 'Z') { // check if capital letter
// replace ' mVariable' with ' variable'
newLine = oldLine.replace(
characterBeforeVariable + prefix + character,
characterBeforeVariable + Character.toString(character).toLowerCase()
);
// remove recursively (imagine mCount = mCount + 15)
return removeHungarianNotationFromLine(
newLine,
characterBeforeVariable,
prefix
);
}
}
// test condition before adding 2 to index
if(index == -1)
break;
}
return newLine;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment