Tuesday, February 27, 2007

Pattern Class

Finite Automata ! Remember ! We used to make a lot of Automata at college time. Making Pattern is one of the most tricky jobs and we need to take care of lot of things.Best Part of the whole story is JDK 1.4.2 onwards support Pattern Class and believe me its simply awesome.
So, I decided to make a check for Class name, if a class name starts with Caps its a perfect one else not as per Java coding guideline. So, here is my simple class which I need to check :

class box {
int width = 0;
int height = 0;
}

Now look at the checks (all these are valid )

class Box
class (space) Box
(space) class
Box
public class Box

So, Decided to make a pattern ".*class[ \t\n\f\r]+[A-Z].*" means anything before the String class is fine, there can be space,tab in between class and classname(more than one,so +) , classname should start with a Caps. I am not checking any other character except the first one, with the assumption that file is already a compiled class.

There can be a lot more optimized pattern and more you optimize,more you feel happy :). Here goes my simple,ugly written code that checks the coding standard(only for class name, and that even for one class only :D) box.java file.

See hoe easy to make a option say java -checkstandard which checks for the correct coding guideline in .java file. And if its not according to coding guideline, we can throw the error and why to throw the error, we can replace it with right name(right according to Java Standard). So the code will change

class box {
int width = 0;
int height = 0;
}

into

class Box {
int width = 0;
int height = 0;
}


// Here goes the code //

import java.io.*;
import java.util.regex.*;

class ClassNameChecker {
public static void main(String[] args)
{
String line;
String sub;
try {
BufferedReader in = new BufferedReader(new FileReader(" box.java"));
line = in.readLine();
Pattern p = Pattern.compile(".*class[ \t\n\f\r]+[A-Z].*");
Matcher m = p.matcher(line);
if(m.matches())
{
System.out.println("Class Name follow Java Coding Guidelines");
}
else
{
System.out.println("Not a Java coding Standard");
}
}
catch(Exception e)
{
//something something
}
}
}

Tuesday, February 13, 2007

Next Decade is challenging ...

Revolution in Science is most unlikely in last 2-3 decade. I guess adoption of Internet was one and the only revolution of last few decades. But if we consider optimization, we did a great job ! From maintaining diary to writing blogs, from railway counter reservation to e-reservation. Very well depicted by this youtube video of Web 2.0



But I have a gut feeling that in the upcoming decade we are going to see some of the revolutions. And here I get the first news: "First Quantum Computer Tuesday". British Columbia-based D-Wave will demonstrate the world's first quantum computer this Tuesday. D-Wave is doing a good job in making Quantum Computers commercial but in spite of hard work they are fighting with Financial optimization.

Saturday, February 10, 2007

File Transfer ...

I was looking at all the possibilites of transferring file from one system(source system(s)) to other(target system) through pure java code. Socket programming is one of the ways suggested by my friend. But it seems to be a tedious job... opening connection, sending request, closing connection. And the base line truth is that I don't know how to do these all :).

I was searching in Java Library for file transfer and I saw Java FTP client Library support. We can upload or download files from remote computer running an FTP server simply with some method calls.

Look at this code:

FTPClient objectClient = new FTPClient();
objectClient.connect("SystemName","username","password");
objectClient.download("D:\\Myfile\\","RequiredFile.txt");
objectClient.disconnect();

FTPClient Package was supported by Sun, its never be a part of jdk package and I guess it was deprecated now :((
Any simple idea to do file transfer ?

Friday, February 02, 2007

Dead or Alive !!

Ahh this time its cool. Again a good conversion but this time I guess Java is going pretty cool. Work is to write a script (no script, program :D ) which checks the status(Dead or Alive) of Network machines. Yes an easy job for scripting language. Believe me, much easy in java as well. And here it goes:

import java.io.*;
import java.net.InetAddress;
public class MainClass {
public static void main(String[] args) throws IOException {
try {
int timeout = 2000;
BufferedReader br = new BufferedReader(new FileReader("machine.txt"));
String machineName = null;
while ((machineName = br.readLine()) != null) {
InetAddress address = InetAddress.getByName(machineName.trim());
if (address.isReachable(timeout))
System.out.println(address + " is Alive");
else
System.out.println(address + " is Dead");
}
} catch (Exception e) {
System.out.printf("Unknown host");
}
}
}

Thanks to my team member to add this reading file code. If you remove file reading lines, code is not more than 4-5 lines. machine.txt contains name of the machines like
Machine1
Machine2
Machine3
Dabba4

I have compiled this code on JDK 1.5 but it should run on the lower version of JDK's as well (hopefully).