Monday, August 30, 2010

What to do with java annotation ??


Java -1.6 annotation features supports annotation which is mainly used to develop a simple Aspect Oriented programs. Apart from the primitive informations we can put more information on a class and on it's members using annotation.

Problem with Object Oriented Programming.
If you expose your interface in an object oriented model, then those clients who are using it need to understand your object model, they have to design their object , they may need to inherit your functionality. they may not have a loosely coupled design.


For example,
Lets take an example of any object oriented api like SAX parser, here you need to inherit some of the abstract class like ContentHandler to serialize/deserialize your objects to XML string.
So first, you start with some abstract definitions which will be tightly coupled to the java interface. What is the intention of you to do it ?, You want to generalize some thing on top of your design. This is good with respect to an Object Oriented Programming.

Thats why JAXB is introduced, You does not have to extend any interface on top of your design. you need only a POJO object with some of its member variables marked with annotation.

Lets take an example of Employee Object

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="employee" propOrder = {"name", "salary"})
public class Employee {

@XmlElement(name="name", required=true)
String name;

@XmlElement(name="salary", required=true)
double salary;
}

Now you can think of doing it without annotation by ContentHandler interface, it is very difficult.

Note: Recently all the java apis comes with annotation support, example like hibernate, JPA, EJB-3.0, Google GUICE, all Spring. Even Groovy script has annotation like structure.


Web Graphics, SVG, CTM

Scalable Vector Graphics

I have been working with SVG last 2 years, i found it very useful to display the network topological graph view in Web UI, also i have designed rich Web UI tools out of that. Also i developed many draggable components, node-link structure.

Some background on SVG:

As it is a W3 spec similar to HTML spec, it is supported by many programming technologies, mainly supported by all browsers other than IE supports, In IE SVG support is not there, but we can install adobe SVG which provides the support.

Adobe Support.

Acrobat PDF supports SVG if you had installed it's adobe svg viewer, Also you can manipulate the SVG Dom by javascript, inside Acrobat PDF as slide view. You can also manipulate it using the acrobat javascript support.

Java supports.

In java, batik library provides SVG supports and it's w3 API, using that you can manipulate the SVG Dom. You can also export the SVG Graphics2D java object into java awt image.

SVG CTM (current Transformation Matrix ):
CTM is an interesting topic in any graphics tool, Each pixel inside has a transformation matrix, when it is translated to a new X and Y co-ordinate, or rotated by an angle, or scaled by X and Y ratio, then this CTM is needed to calculate the new X and Y co-ordinate.

Lets take a point A present on (2, 2) is translated to a distance of 2 pixel on x - axis and 2 pixel on y -axis, then the new X and Y co-ordinate of A will be (4, 4).

Since it is a simple X and Y translation, the Transformation matrix of A is like this.

1 0 2
0 1 2
0 0 1

so the formula is this

3-by-3 transformation matrix


For more information
http://www.w3.org/TR/2003/REC-SVG11-20030114/coords.html#EstablishingANewUserSpace

Object Oriented JavaScript

How to write Object Oriented javascript ?
I explain simple steps to convert your plain javascript code to an object oriented javascript code.

1.) I will guide you to start with a simple function to the object oriented function at last.
2.) How to do multiple inheritance like in C++, Multi level inheritance like in Java
3.) Friend function like C++
4.) what do you mean by Redefinition in Javascript?
5.) How to design package ?
6.) how to use __proto__, instanceof ?
7.) Singleton design pattern
8.) Enum function
9.) what is prototype.constructor and when to use it ?

step-1: Look at this simple Function.

function add(a, b) {
return a + b;
}

function sub(a, b) {
return a – b;
}

var sum = add(2, 3);
var diff = sub(2, 3);

step-2: Another way by anonymous function

var add = function(a, b) {
return a + b;
}

var sub = function(a, b) {
return a – b;
}

var sum = add(2, 3);
var diff = sub(2, 3);

step-3: By anonymous function, executing it immediately

var sum = function(a, b) {
return a + b;
} (2, 3);

var diff = function(a, b) {
return a – b;
} (2, 3);

step-4: By Object level functions

var math = new Object();

math.add = function(a, b) {
return a + b;
}

math.sub = function(a, b) {
return a – b;
}

var sum = math.add(2, 3);
var diff = math.sub(2, 3);

//or

var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);

step-5: By Function Map

var math = {
“add” : function(a, b) {
return a + b;
},

“sub”: function(a, b) {
return a – b;
}
};

var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);
//or
var sum = math.add(2, 3);
var diff = math.sub(2, 3);

step-6: Now look at how to make it much more object oriented

function Math() {
this.add = function(a, b) {
return a + b;
};

this.sub = function(a, b) {
return a – b;
};
return this;
}

var math = new Math();

var sum = math.add(2, 3);
var diff = math.sub(2, 3);
//or
var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);

//drawback: You cannot redefine the member functions commonly

step-7: Now, i am introducing 'prototype', prototype is like a map which holds the the object member definition of any Function

function Math() {
return this;
}

Math.prototype.add = function(a, b) {
return a + b;
}

Math.prototype.sub = function(a, b) {
return a – b;
}

var math = new Math();
var sum = math.add(2, 3);
var diff = math.sub(2, 3);

//or

var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);

Note: ‘prototype’ is present inbuilt in all normal function

step-8: Another way of implementing it as like a map

function Math() {
return this;
}

Math.prototype = {
add: function(a, b) {
return a + b;
},

sub: function(a, b) {
return a – b;
}
};

var math = new Math();
var sum = math.add(2, 3);
var diff = math.sub(2, 3);

//or

var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);

step-9: By anonymous prototype functions

var Math = function() {
return this;
}

Math.prototype = {} //Note: ‘prototype’ is not present in anonymous function

Math.prototype.add = function(a, b) {
return a + b;
}

Math.prototype.sub = function(a, b) {
return a – b;
}

var math = new Math();
var sum = math.add(2, 3);
var diff = math.sub(2, 3);

//or

var sum = math[“add”](2, 3);
var diff = math[“sub”](2, 3);



Inheritance : Look at this example

function Address() {
return this;
}

Address.prototype.setStreet = function(_street) { this.street = _street; }
Address.prototype.getStreet = function() { return this.street; }
Address.prototype.setCity = function(_city) { this.city = _city; }
Address.prototype.getCity = function() { return this.city;}

function Person() {
return this;
}
Person.prototype = new Address();

function Employee() {
return this;
}
Employee.prototype = new Person();
Employee.prototype = new Account(); //this throws error

Drawback: cannot do multiple inheritance like C++

Lets see how to do it, this is what most of the javascript library is doing,



‘extend’

function extend(childclass, parentclass) {
var _clazz = function() { };
_clazz.prototype = parentclass.prototype;
childclass.prototype = new _clazz();
childclass.prototype.constructor = childclass;
childclass.super = parentclass.prototype;
}

function Person() {
return this;
}

function Address() {
return this;
}

function Employee() {
return this;
}

extend(Employee, Person);
extend(Employee, Address);



Now you need to understand 'instanceof' and '__proto__'

Instanceof : It works like java instanceof

var emp = new Employee();
emp instanceof Employee // returns true

__proto__: It is used to call super class method

function Shape() {
return this;
}

Shape.prototype.draw = function() { …}

function Square() {
return this;
}

Square.prototype = new Shape();
Square.prototype.draw = function() { this.__proto__.draw(); … }

function Cube() {
return this;
}

Cube.prototype = new Square();
Cube.prototype.draw = function() {
this.__proto__.draw();
//.....
}

var c = new Cube();
c.__proto__.draw(); //calls the Square’s draw function
c.__proto__.__proto__.draw(); //calls the Shape’s draw function



What is redefinition ?
Redefinition: Like java invocation handler

function Circle() {
return this;
}

Circle.prototype.draw = function(g) {
g.paint(“circle”, this);
}

var _circle_draw_func = Circle.prototype.draw;
Circle.prototype.draw = function(g) {
//do before ….
_circle_draw_func.call(this, g); //This is like friend function in C++
//do after ….
}

If you don’t do it like this, then you will be replacing the original function with your new function definition



How to construct package ?

if(typeof(com) != “undefined”) {
throw “package ‘com’ is already defined”
}
var com = {};

if(typeof(com.tejas) != “undefined”) {
throw “package ‘com.tejas’ is already defined”
}
com.tejas = {};

if(typeof(com.tejas.lang) != “undefined”) {
throw “package ‘com.tejas.lang’ is already defined”
}
com.tejas.lang = {};

note: you should throw error, otherwise all your definitions on first js file will be replaced by the second js file if both using same packages

Creating function definition on package

com.tejas.web.js.Circuit = new function() {
this.sncs = [];
return this;
}

com.tejas.web.js.Circuit.prototype = { }

com.tejas.web.js.Circuit.prototype.addSncs = function(snc) {
this.sncs.push(snc);
}

Note: Anonymous functions will not have ‘prototype’ map by default.



how to implement Design patterns ?

Singleton

var shapeFactory = function() {
this.createCircle = function() {
return new Circle();
};

this.createSquare = function() {
return new Square();
};
} ();

//but if you need it to be a child of some other function

var shapeFactory;
function ShapeFactory() {
if(!shapeFactory) {
shapeFactory = this;
}
return shapeFactory;
}

ShapeFactory.prototype.createCircle = function() { return new Circle(); }
ShapeFactory.prototype.createSquare = function() { return new Square(); }



How to derive Enum ?

Enum: You have to define it like a singleton object …

com.tejas.lang.TimeUnit = function() {
if(!com.tejas.lang.TIMEUNIT) {
com.tejas.lang.TIMEUNIT = this;
}
return com.tejas.lang.TIMEUNIT;
}

com.tejas.lang.TimeUnit.prototype = {}

com.tejas.lang.TimeUnit.prototype.WEEK = new _TimeUnit(7 * 24 * 60 * 60 * 1000);
com.tejas.lang.TimeUnit.prototype.DAY = new _TimeUnit(24 * 60 * 60 * 1000);
com.tejas.lang.TimeUnit.prototype.HOUR = new _TimeUnit(60 * 60 * 1000);
com.tejas.lang.TimeUnit.prototype.MIN = new _TimeUnit(60 * 1000);
com.tejas.lang.TimeUnit.prototype.SECONDS = new _TimeUnit(1000);

function _TimeUnit(_inSeconds) {
this.seconds = _inSeconds;
return this;
}

_TimeUnit.prototype.convert = function(milli_seconds) { ….. }

new com.tejas.lang.TimeUnit(); //Using: var x = com.tejas.lang.TIMEUNIT.HOUR.seconds;



Constructor

It is a built-in property of the ‘prototype’

Ex: Employee.prototype.constructor

It actually has the reference of the constructing function of the Function

var emp = new Employee.prototype.constructor(); is same like
var emp = new Employee();

Usage: It is used like java reflection kind of feature



Thanks for reading this ....

Saturday, August 14, 2010

About me and my profile

I have been doing some interesting development works and as a NMS Lead Engineer in NMS/EMS in optical networking domain during last few years, around 3 years in IBM, current 2.2 years in Tejas networks.
I mainly do coding, designing work in many java technologies, TMF, corba, snmp, TL1, latest web technologies, web-2.0, javascript, messaging queues like Rational MQ series, oops, aops, ....


  • Java: java-1.6, swing, applet, jaxb, jaxp, xstream, Itext, Jdbc, bdb, jasper, javamail, jgraph, jchart, and JavaCC, HttpConnect, quartz, JavaFX, Groovy,batik.

  • Web Development: REST, Jsp, Servlet, Ajax, javascript, Json, dhtmlx, spring web flow, adobe flex-3, LiveConnect, Svg, dsvg, google guice, Google App Engine, nodejs, Cloud computing.

  • XML: XSL, FO, XSLT, XQuery, Xlink, Saxonica, XForm.

  • J2EE: JMS, EJB3, JPA, RMI, JMS, Active MQ, struts, spring, JTS, JTA.

  • Database: Oracle 9i, DB2 SQL, mysql, hibernate.

  • Applicaton server: Weblogic, Jboss, Websphere

  • Messaging: Active MQ, Websphere MQ

  • Web service: Tomcat, wsdl, SOAP, Axis-2, Artix, JAX-WS, JAX-RPC, Adobe Flex-3, action script, groovy script.

  • Scripting: Object oriented javascript, dhtmlx, shell script, batch script, acro javascript, PDF.

  • CORBA: Jacorb, Visibroker, Orbix, Open Fusion notification service, omg Idl.

  • SNMP: V1, V2c, V3. Ireasoning, sun jdmk, mib browser.

  • Telecom: NMS/EMS, CORBA, SNMP, TL1, Optical networking, SONET, SDH, MPLS, DWDM, TMF-814, FCAPS, ...

  • Operating Systems: Windows 2000, XP, Unix AIX, Sun solaris, Linux.

  • Others: C, C++, maven, ANT, make, CVS, Sablime, design patterns, OOAD, UML, rational rose, eclipse, Star UML, Eclipse debugging