How to find latest jar version of jars by java program? - java

In my project has 40 to 50 jar files available, It takes lot of time to find out latest version of each jar at every time. Can u any one help me to write a java program for this?

You may want to just use maven : http://maven.apache.org/
Or an other dependencies manager, like Ivy.

At the time of ant-build please call this method
public void ExpungeDuplicates(String filePath) {
Map<String,Integer> replaceJarsMap = null;
File folder = null;
File[] listOfFiles = null;
List<String> jarList = new ArrayList<String>();
String files = "";
File deleteFile = null;
Iterator<String> mapItr = null;
//String extension ="jar";
try {
folder = new File(filePath);
listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
jarList.add(files);
}
}
if (jarList.size() > 0) {
replaceJarsMap = PatternClassifier.findDuplicatesOrLowerVersion(jarList);
System.err.println("Duplicate / Lower Version - Total Count : "+replaceJarsMap.size());
mapItr = replaceJarsMap.keySet().iterator();
while (mapItr.hasNext()) {
String key = mapItr.next();
int repeat = replaceJarsMap.get(key);
System.out.println( key +" : "+repeat);
for (int i = 0; i <repeat; i++) {
deleteFile = new File(filePath + System.getProperty ("file.separator")+key);
try{
if (deleteFile != null && deleteFile.exists()){
if(deleteFile.delete()){
System.err.println(key +" deleted");
}
}
}catch (Exception e) {
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
You only need to give the path of your Lib to this function.This method will find all the duplicate or lower version of of file.
And the crucial function is given below...Which finds out the duplicates from the list of files you provided.
public static Map<String,Integer> findDuplicatesOrLowerVersion(List<String> fileNameList) {
List<String> oldJarList = new ArrayList<String>();
String cmprTemp[] = null;
boolean match = false;
String regex = "",regexFileType = "",verInfo1 = "",verInfo2 = "",compareName = "",tempCompareName = "",tempJarName ="";
Map<String,Integer> duplicateEntryMap = new HashMap<String, Integer>();
int count = 0;
Collections.sort(fileNameList, Collections.reverseOrder());
try{
int size = fileNameList.size();
for(int i = 0;i<size;i++){
cmprTemp = fileNameList.get(i).split("[0-9\\._]*");
for(String s : cmprTemp){
compareName += s;
}
regex = "^"+compareName+"[ajr0-9_\\-\\.]*";
regexFileType = "[0-9a-zA-Z\\-\\._]*\\.jar$";
if( fileNameList.get(i).matches(regexFileType) && !oldJarList.contains(fileNameList.get(i))){
for(int j = i+1 ;j<size;j++){
cmprTemp = fileNameList.get(j).split("[0-9\\._]*");
for(String s : cmprTemp){
tempCompareName += s;
}
match = (fileNameList.get(j).matches(regexFileType) && tempCompareName.matches(regex));
if(match){
cmprTemp = fileNameList.get(i).split("[a-zA-Z\\-\\._]*");
for(String s : cmprTemp){
verInfo1 += s;
}
verInfo1 += "000";
cmprTemp = fileNameList.get(j).split("[a-zA-Z\\-\\._]*");
for(String s : cmprTemp){
verInfo2 += s;
}
verInfo2 += "000";
int length = 0;
if(verInfo1.length()>verInfo2.length()){
length = verInfo2.length();
}else{
length = verInfo1.length();
}
if(Long.parseLong(verInfo1.substring(0,length))>=Long.parseLong(verInfo2.substring(0,length))){
count = 0;
if(!oldJarList.contains(fileNameList.get(j))){
oldJarList.add(fileNameList.get(j));
duplicateEntryMap.put(fileNameList.get(j),++count);
}else{
count = duplicateEntryMap.get(fileNameList.get(j));
duplicateEntryMap.put(fileNameList.get(j),++count);
}
}else{
tempJarName = fileNameList.get(i);
}
match = false;verInfo1 = "";verInfo2 = "";
}
tempCompareName = "";
}
if(tempJarName!=null && !tempJarName.equals("")){
count = 0;
if(!oldJarList.contains(fileNameList.get(i))){
oldJarList.add(fileNameList.get(i));
duplicateEntryMap.put(fileNameList.get(i),++count);
}else{
count = dupl icateEntryMap.get(fileNameList.get(i));
duplicateEntryMap.put(fileNameList.get(i),++count);
}
tempJarName = "";
}
}
compareName = "";
}
}catch (Exception e) {
e.printStackTrace();
}
return duplicateEntryMap;
}
What findDuplicatesOrLowerVersion(List fileNameList) function task - Simply it found the duplicates and passting a map which contains the name of the file and number of time the lower version repeats.
Try this. The remaining file exist in the folder should be latest or files with out duplicates.Am using this for finding the oldest files.on the basis of that it will find the old and delete it.
This am only checking the name..Futher improvement you can made.
Where PatternClassifier is a class which contains the second method given here.

Related

How to compare two set then filter to new set with combination string?

I'm build some short code for compare 2 hashset.
SET 1 = noRek : [1234567892, 1234567891, 1234567890]
SET 2 = Source : [1234567890U0113, 1234567894B0111, 1234567890U0112,
1234567891B0111, 1234567890U0115, 1234567890U0114, 1234567892B0113,
1234567893B0111, 1234567890U0111, 1234567890B0111, 1234567892B0112,
1234567892B0111]
public class diff {
public static void main(String args[]) {
String filename = "C:\\abc.txt";
String filename2 = "C:\\xyz.txt";
HashSet<String> al = new HashSet<String>();
HashSet<String> al1 = new HashSet<String>();
HashSet<String> source = new HashSet<String>();
HashSet<String> noRek = new HashSet<String>();
HashSet<String> diff1 = new HashSet<String>();
HashSet<String> diff2 = new HashSet<String>();
String str = null;
String str2 = null;
Integer digitRek = 10;
Integer digitTransaksi = 15;
//GET REKDATA FROM TARGET
try {
String message = new Scanner(new File(filename2)).useDelimiter("\\Z").next();
for (int i = 0; i < message.length(); i += digitRek) {
noRek.add(message.substring(i, Math.min(i + digitRek, message.length())));
}
System.out.println("noRek : " + noRek);
} catch (Exception e) {
e.printStackTrace();
}
try {
String message2 = new Scanner(new File(filename)).useDelimiter("\\Z").next();
for (int i = 0; i < message2.length(); i += digitTransaksi) {
source.add(message2.substring(i, Math.min(i + digitTransaksi, message2.length())));
}
System.out.println("Source : " + source);
} catch (Exception e) {
e.printStackTrace();
}
for (String str3 : source) {
if (source.contains(noRek.substring(digitRek)) {
diff1.add(str3);
}
}
System.out.println("Final : " + diff1);
}
I excpet the output of the set diff1 is like this
SET 3 = [1234567890U0111, 1234567890U0112, 1234567890U0113,1234567890U0114, 1234567890U0115, 1234567890B0111, 1234567891B0111, 1234567892B0113, 1234567892B0112, 1234567892B0111]
but actual output is same like SET 2.
In simple way I need compare SET 2 with combination, first 10 digit is account number, then next charachter 1 digit is code, then the rest of number is auto generated. That's mean the length combination SET 2 is 15 digit, and combination SET 1 is 10 digit, then set 1 is data of account number, I need get all transaction from account number in set 2.
SET 1 is data all of account and
SET 2 is data of transaction combination
You can solve this by using stream and filter
Set<String> diff1 = source.stream().filter(str -> {
if (str.length() > 10) {
String account = str.substring(0, 10);
return noRek.contains(account);
}
return false;
}).collect(Collectors.toSet());

How to divide a sentence to words and compare with another string?

I have saved the units in .txt file. These I am getting in an array list. Now I want to check if any of the units present in the string.
List contains :
"units", "kg", "kilogms", "kilo", "literes",
"Liter", "packets", "packet", "gms", "grams", "half kg"
Like, if I have a string - 1kg rice, I want to get numbers from this string and I want to divide this sentence to words and want to compare with each item from array list of units. If it is present I want to save it. So I want to store 1kg and rice separately. string may contain any spaces I want to trim all those spaces and check compare.
Getting text file in an array list.
public class ReadTextFiles {
public static List<String> readItemNamesFile(Context context) {
String sText = null;
List<String> stringList;
try{
InputStream is = context.getResources().openRawResource(R.raw.item_names);
//Use one of the above as per your file existing folder
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
sText = new String(buffer, "UTF-8");
String[] sTextArray = sText.replace("\"", "").split(",");
stringList = new ArrayList<String>(Arrays.asList(sTextArray));
System.out.print(stringList);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return stringList;
}
}
public void getUnits()
{
List<String> units = new ArrayList<>();
units = ReadTextFiles.readUnitsFile(getActivity());
System.out.print(units.size());
}
Now I want to compare string suppose its "1 kg potato".Then should find potato from the array list. Also it should be case insensitive.
This is the full solution of your requirement as I understood:
String measuring = "\"units\", \"kg\", \"kilogms\", \"kilo\", \"literes\", \"Liter\", \"packets\", \"packet\", \"gms\", \"grams\", \"half kg\"";
String items = "\"Potato\", \"rice\", \"Eggs\", \"Maggi\", \"Dryfruits\", \"Maza\", \"cold drink\", \"sauce\", \"catchup\", \"coconut oil\"";
String matching = "Kg500 Potato";//"Potato 1 kg";
String item = "", measuringUnit = "", quantity = "";
private void findOut() {
String[] sMeasuringArray = measuring.replace("\"", "").split(", ");
ArrayList<String> measuringList = new ArrayList<String>(Arrays.asList(sMeasuringArray));
String[] sItemsArray = items.replace("\"", "").split(", ");
ArrayList<String> itemsList = new ArrayList<String>(Arrays.asList(sItemsArray));
String[] sMatchingArray = matching.split(" ");
matching = matching.toUpperCase();
for (int i = 0; i < measuringList.size(); i++) {
if (matching.contains(measuringList.get(i).toUpperCase())) {
measuringUnit = measuringList.get(i).trim();
break;
}
}
for (int i = 0; i < itemsList.size(); i++) {
if (matching.contains(itemsList.get(i).toUpperCase())) {
item = itemsList.get(i).trim();
break;
}
}
if (matching!= null) {
String[] part = matching.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
for (int k = 0; k < part.length; k++) {
try {
Integer.parseInt(part[k]);
quantity = part[k];
break;
} catch (Exception ex) {
continue;
}
}
}
/*if (sMatchingArray != null) {
if (sMatchingArray.length == 3) {
for (int j = 0; j < sMatchingArray.length; j++) {
if (measuringUnit.trim().equals(sMatchingArray[j].trim())) {
quantity = sMatchingArray[j - 1].trim();
break;
}
}
} else if (sMatchingArray.length == 2) {
String[] part = matching.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
for (int k = 0; k < part.length; k++) {
try {
Integer.parseInt(part[k]);
quantity = part[k];
break;
} catch (Exception ex) {
continue;
}
}
}
}*/
Log.e("Solution: ", "item = " + item + ", measuringUnit = " + measuringUnit + ", quantity = " + quantity);
}
I'm gonna be using algorithmic approach for the answer. So here it goes:
strItem = "1kg rice";
//Run a loop through the list of units and for each unit check this
if (strItem.contains(list.get(index)))
//Do the needful and break

String index out of Bounds Exception error

I am making a program to read .java files and extract all comments from it and write it to an html file. The file works mostly but i am having trouble extracting the names of each method as it confuses "Exception{" as a separate class/method.
So far this is the code i have and i believe it is almost done.
http://pastebin.com/qrbJAaW3
public class ParseDoc {
static String fileName = null;
static String outputR = "";
static String inputR = "";
static String[] lines;
static String [] classnames;
static StringBuilder classn = new StringBuilder();
static String classnam = "";
/**
* This Method asks the user for path to input and output file
* then it reads the file and places it on a string for easier sorting.
* The string is sorted line by line into an array and cleaned.
* #return Array of DOC comments
* #throws Exception
*/
public static String[] scanread() throws Exception{
System.out.println("NOTICE: If a valid file path is not entered, the program will not return a DOCHTML document.");
Scanner inputReader = new Scanner(System.in);
System.out.println("Please enter path to input file (ex: C:/Program Files/Code.java : " );
inputR = inputReader.nextLine();
System.out.println("Please enter path to html output file (ex: C:/Program Files/Output.html : " );
outputR = inputReader.nextLine();
inputReader.close();
FileReader file = new FileReader(inputR);
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
//NAME OF THE SOURCE FILE
int index = inputR.lastIndexOf("/");
fileName = inputR.substring(index + 1);
int z = 0;
//This loop turns the input file into an String for easier access
while (line!= null){
line = reader.readLine();
z += 1;
}
reader.close();
file.close();
FileReader file2 = new FileReader(inputR);
BufferedReader reader2 = new BufferedReader(file2);
String line2 = reader2.readLine();
lines = new String[z];
int j = 0;
while(line2 != null)
{
line2 = line2.trim();
lines[j] = line2;
line2 = reader2.readLine();
j += 1;
}
reader2.close();
file2.close();
return lines;
}
/**
* Removes all the comments from the Array containing strings
* #param lines contains strings made of input file
* #return Array with removed strings
*/
public static String[] removeComments(String[] lines){
for (int i=0; i <lines.length;i++){
if (lines[i].startsWith("//"))
{
lines[i]="";
}
}
return lines;
}
/**
* This method scans the entire code for name of the classes and methods
* along with their parameters and stores them in an Array for further use
* #param lines
* #return lines array without changing any content
*/
public static String[] classNames(String[] lines)
{
int total = 0;
String[] matches = new String[] {"public ", "class ","private "};
for(int b = 0; b <lines.length;b++)
{
Matcher num = Pattern.compile("\\S+\\s*\\(([^)]*)\\)").matcher(lines[b]);
for (int n = 0; n < 3 ;n++)
{
if (lines[b].contains(matches[n]))
{
while(num.find())
{
total += 1;
}
}
}
}
classnames = new String[total];
for(int z = 0; z<lines.length;z++)
{
Matcher mzz = Pattern.compile("\\w+\\s*\\{").matcher(lines[z]);
for (int k = 0; k < 3; k++)
{
if (lines[z].contains(matches[k])&& !(lines[z].contains("throws "))) //&& !(lines[z].contains("throws "))
{
while(mzz.find())
{
classn.append( mzz.group()+"break");
}
}
}
}
for(int z = 0; z <lines.length;z++)
{
//This matcher with Regex looks for class/method names along with any parameters inside
Matcher m = Pattern.compile("\\S+\\s*\\(([^)]*)\\)").matcher(lines[z]);
int i = 0;
for (int k = 0; k < 3; k++)
{
if (lines[z].contains(matches[k]) )
{
while(m.find())
{
classn.append( m.group()+"break");
continue;
}
}
}
}
classnam = classn.toString();
classnames = classnam.split("break");
/*for(int step = 0; step<classnames.length;step++)
{
System.out.println(classnames[step]);
}*/
return lines;
}
/**
* This method removes all the code from the Array and leaves on the
* Doc comments intact.
* #param lines
* #return lines array with only comments remaining ( code is removed )
*/
public static String[] removeCode(String[] lines)
{
int rep = 0;
while ( rep <lines.length){
lines[rep] = lines[rep].replaceAll("\\*", "Z");
if(!(lines[rep].startsWith("Z") || (lines[rep].startsWith("/")))){
lines[rep]="";
}
lines[rep] = lines[rep].replaceAll("Z", "\\*");
rep += 1;
}
for(int num = 0; num <lines.length; num++)
{
if(lines[num].isEmpty()){
lines[num] = null;
}
}
return lines;
}
/**
* This method removes the remaining stars, slashes and properly formats each comment
* before printing it.
* #param lines The array contains parsed Java Doc comments
* #return
* #throws Exception
*/
public static String[] writeTo(String[] lines) throws Exception
{
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(outputR));
StringBuilder writeTo = new StringBuilder();
writeTo.append("<html>\n<body>\n<h2><mark> JAVA DOC COMMENTS</mark> </h2>\n<pre>\n"
+"<big><b>Source File:</b> </big>" +"<big>"+ fileName+"</big>" + "\n\n");
for(int step = 0; step<lines.length;step++)
{
if(!(lines[step] == null))
{
lines[step] = lines[step].replace("#author", "<b>Author: </b>\n&nbsp&nbsp&nbsp");
lines[step] = lines[step].replace("#since", "<b>Since: </b>\n&nbsp&nbsp&nbsp");
lines[step] = lines[step].replace("#version", "<b>Version: </b>\n&nbsp&nbsp&nbsp");
lines[step] = lines[step].replace("#param", "<b>Parameter: </b>\n&nbsp&nbsp&nbsp");
lines[step] = lines[step].replace("#return", "<b>Return: </b>\n&nbsp&nbsp&nbsp");
//lines[step] = lines[step].replace("*", "");
}
}
outputWriter.write(writeTo.toString());
//write to HTML
int countz = 0;
int comcount = 0;
//classnames[]
for(int resum = 0; resum<lines.length;resum++)
{
if(lines[resum] != null)
{
if( lines[resum].charAt(0) == '*' )
{
lines[resum] = lines[resum].replace("*","");
}
}
}
for(int i = 0; i < classnames.length; i++)
{
System.out.println(classnames[i]);
}
for(int resum = 0; resum<lines.length;resum++)
{
if(lines[resum] != null)
{
if( lines[resum].charAt(0) == '/' )
{
if(lines[resum].endsWith("*"))
{
lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
countz++;
}
}
if( lines[resum].charAt(0) == '/' )
{
lines[resum] = lines[resum].replace("/","\n");
}
}
}
/*for(int resum = 0; resum<lines.length;resum++)
{
}*/
for(int f = 0; f<lines.length;f++)
{
if(lines[f] != null)
{
/*if(lines[f].startsWith("//") && lines[f].length() == 2)
{
lines[f] = "TEEEST";
countz++;
}*/
outputWriter.write(lines[f]+"\n");
}
}
outputWriter.close();
return null;
}
}
Console:
Please enter path to input file (ex: C:/Program Files/Code.java :
I:\ICS4U0\DocParse\src\java_doc_parse\ParseDoc.java
Please enter path to html output file (ex: C:/Program Files/Output.html :
I:\ICS4U0\DocParse\src\java_doc_parse\ParseDochh.html
ParseDoc {
scanread()
removeComments(String[] lines)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at java_doc_parse.ParseDoc.writeTo(ParseDoc.java:285)
at java_doc_parse.Execute.main(Execute.java:14)
classNames(String[] lines)
removeCode(String[] lines)
writeTo(String[] lines)
I am not sure what is causing this error. Is there a way to fix it or should i just give up on adding class names to comments altogether?
Sorry if i am lacking some information, but i am quite confused myself.
The error seems to be because you call charAt(0) on what appears to be an empty string.
You already have a null check above, I don't know if it's valid (can your lines ever be null?), but I would change that to a length check, possibly combined with the existing null check.
if (lines[resum] != null && lines[resum].length > 0) {
I think this is where your error resides:
for(int resum = 0; resum<lines.length;resum++)
{
if(lines[resum] != null)
{
if( lines[resum].charAt(0) == '/' )
{
if(lines[resum].endsWith("*"))
{
lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
countz++;
}
}
if( lines[resum].charAt(0) == '/' )
{
lines[resum] = lines[resum].replace("/","\n");
}
}
}
Try this instead, move the null check before entering the for loop:
if(lines[0] != null)
{
for(int resum = 0; resum<lines.length;resum++)
{
if( lines[resum].charAt(0) == '/' )
{
if(lines[resum].endsWith("*"))
{
lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
countz++;
}
}
if( lines[resum].charAt(0) == '/' )
{
lines[resum] = lines[resum].replace("/","\n");
}
}
}

Using the JGIT, how can I retrieve the line numbers of added/deleted lines

Assuming the following piece of code is committed to a Git repository:
int test(){
int a = 3;
int b = 4;
int c = a + b;
return c;
}
and is later updated to
int test(){
return 7;
}
I currently have a method which uses the JGit API in order to access the Git repository where the above are committed and outputs a string which is similar to the following:
int test(){
-int a = 3;
-int b = 4;
-int c = a + b;
-return c;
+return 7;
}
Now, my requirements have changed and would like to know the line numbers of the changed lines only. So I would want something like the following:
2 -int a = 3;
3 -int b = 4;
4 -int c = a + b;
5 -return c;
2 +return 7;
Basically, the same information that the GitHub application gives when an update is made.
Any help would be greatly appreciated :)
snippet of how the -/+ lines are computed:
String oldHash = "ee3e216ab5047748a22e9ec5ad3e92834704f0cc";
Git git = null;
try {
//the path where the repo is.
git = Git.open(new File("C:\\Users\\Administrator\\Documents\\GitHub\\Trial"));
} catch (IOException e1) {
e1.printStackTrace();
}
Repository repository = git.getRepository();
ObjectId old = null;
ObjectId head = null;
//a new reader to read objects from getObjectDatabase()
ObjectReader reader = repository.newObjectReader();
//Create a new parser.
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
List<DiffEntry> diffs = null;
try {
//parse a git repository string and return an ObjectId
old = repository.resolve(oldHash + "^{tree}");
head = repository.resolve("HEAD^{tree}");
//Reset this parser to walk through the given tree
oldTreeIter.reset(reader, old);
newTreeIter.reset(reader, head);
diffs = git.diff()//Returns a command object to execute a diff command
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();//returns a DiffEntry for each path which is different
} catch (RevisionSyntaxException | IOException | GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//DiffLineCountFilter d = new DiffLineCountFilter();
//out is the stream the formatter will write to
ByteArrayOutputStream out = new ByteArrayOutputStream();
//Create a new formatter with a default level of context.
DiffFormatter df = new DiffFormatter(out);
//Set the repository the formatter can load object contents from.
df.setRepository(git.getRepository());
ArrayList<String> diffText = new ArrayList<String>();
//A DiffEntry is 'A value class representing a change to a file' therefore for each file you have a diff entry
for(DiffEntry diff : diffs)
{
try {
//Format a patch script for one file entry.
df.format(diff);
RawText r = new RawText(out.toByteArray());
r.getLineDelimiter();
diffText.add(out.toString());
out.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
You need to do the difference between the A line indexes and B line indexes from the diff result:
int linesAdded = 0;
int linesDeleted = 0;
int filesChanged = 0;
try {
repo = new FileRepository(new File("repo/.git"));
RevWalk rw = new RevWalk(repo);
RevCommit commit = rw.parseCommit(repo.resolve("486817d67b")); // Any ref will work here (HEAD, a sha1, tag, branch)
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(repo);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs;
diffs = df.scan(parent.getTree(), commit.getTree());
filesChanged = diffs.size();
for (DiffEntry diff : diffs) {
for (Edit edit : df.toFileHeader(diff).toEditList()) {
linesDeleted += edit.getEndA() - edit.getBeginA();
linesAdded += edit.getEndB() - edit.getBeginB();
}
}
} catch (IOException e1) {
throw new RuntimeException(e1);
}
Just a tip for anyone who might have this problem. I did not manage to get the line numbers of the added and deleted lines but I did manage to get a string which contains only the added and deleted lines without the other lines which were not changed.
This was simply done by adding the line:
df.setContext(0);
in the snippet I provided above right before the line
df.format(diff);
I do it this way but I don't know if it is correct
public void linesChangeInFile(Git git, List<RevCommit> commits, String fileName, String pathRepository) {
try {
List<RevCommit> commitsComparer = new ArrayList<>();
List<String> linesChange = new ArrayList<>();
for (int i = 0; i < commits.size() - 1; i++) {
ObjectId commitIDOld = commits.get(i).getId();
if (Validador.isFileExistInCommit(commits.get(i), getRepository(), fileName)) {
if (i != commits.size() - 1 && !commitsComparer.contains(commits.get(i))) {
ObjectId commitIDNew = commits.get(i + 1);
commitsComparer.add(commits.get(i));
linesChange.add(diff(git, commitIDOld.getName(), commitIDNew.getName(), fileName));
}
try (final FileInputStream input = new FileInputStream(pathRepository + "\\" + fileName)) {
currentLines = IOUtils.readLines(input, "UTF-8").size();
}
}
}
Integer sumLinesAdd = 0;
Integer sumLinesDel = 0;
for (String lineChange : linesChange) {
String[] lChange = lineChange.split(";");
sumLinesAdd += Integer.parseInt(lChange[0]);
sumLinesDel += Integer.parseInt(lChange[1]);
}
System.out.println("Lines Add total:" + sumLinesAdd);
System.out.println("Lines Del total:" + sumLinesDel);
System.out.println("Total lines change:" + (sumLinesAdd + sumLinesDel));
} catch (RevisionSyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String diff(Git git, String commitIDOld, String commitIDNew, String fileName) {
int linesAdded = 0;
int linesDeleted = 0;
DiffFormatter df = null;
try {
AbstractTreeIterator oldTreeParser = prepareTreeParser(getRepository(), commitIDOld);
AbstractTreeIterator newTreeParser = prepareTreeParser(getRepository(), commitIDNew);
List<DiffEntry> diffs = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser)
.setPathFilter(PathFilter.create(fileName)).call();
df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(getRepository());
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
for (DiffEntry entry : diffs) {
// System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to:
// " + entry.getNewId());
// try (DiffFormatter formatter = new DiffFormatter(System.out)) {
// formatter.setContext(0);
// formatter.setRepository(repository);
// formatter.format(entry);
// }
for (Edit edit : df.toFileHeader(entry).toEditList()) {
linesDeleted += edit.getEndA() - edit.getBeginA();
linesAdded += edit.getEndB() - edit.getBeginB();
}
}
} catch (IOException | GitAPIException e) {
System.err.println("Error:" + e.getMessage());
}
return linesAdded + ";" + linesDeleted;
}

word count frequency in document

I have a directory in which I have 1000 txt.files in it. I want to know for every word how many times it occurs in the 1000 document. So say even the word "cow" occured 100 times in X it will still be counted as one. If it occured in a different document it is incremented by one. So the maximum is 1000 if "cow" appears in every single document. How do I do this the easy way without the use of any other external library. Here's what I have so far
private Hashtable<String, Integer> getAllWordCount()
private Hashtable<String, Integer> getAllWordCount()
{
Hashtable<String, Integer> result = new Hashtable<String, Integer>();
HashSet<String> words = new HashSet<String>();
try {
for (int j = 0; j < fileDirectory.length; j++){
File theDirectory = new File(fileDirectory[j]);
File[] children = theDirectory.listFiles();
for (int i = 0; i < children.length; i++){
Scanner scanner = new Scanner(new FileReader(children[i]));
while (scanner.hasNext()){
String text = scanner.next().replaceAll("[^A-Za-z0-9]", "");
if (words.contains(text) == false){
if (result.get(text) == null)
result.put(text, 1);
else
result.put(text, result.get(text) + 1);
words.add(text);
}
}
}
words.clear();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.size());
return result;
}
You also need a HashSet<String> in which you store each unique word you've read from the current file.
Then after every word read, you should check if it's in the set, if it isn't, increment the corresponding value in the result map (or add a new entry if it was empty, like you already do) and add the word to the set.
Don't forget to reset the set when you start to read a new file though.
how about this?
private Hashtable<String, Integer> getAllWordCount()
{
Hashtable<String, Integer> result = new Hashtable<String, Integer>();
HashSet<String> words = new HashSet<String>();
try {
for (int j = 0; j < fileDirectory.length; j++){
File theDirectory = new File(fileDirectory[j]);
File[] children = theDirectory.listFiles();
for (int i = 0; i < children.length; i++){
Scanner scanner = new Scanner(new FileReader(children[i]));
while (scanner.hasNext()){
String text = scanner.next().replaceAll("[^A-Za-z0-9]", "");
words.add(text);
}
for (String word : words) {
Integer count = result.get(word)
if (result.get(word) == null) {
result.put(word, 1);
} else {
result.put(word, result.get(word) + 1);
}
}
words.clear();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.size());
return result;
}

Categories

Resources