Wednesday, November 12, 2014

Polymorphism Example

package InheritancePolymorphism;

public class InheritancePolymorphism {
public void Method1(){
System.out.println("supermethod");

}


}
package InheritancePolymorphism;

import org.junit.Test;




public class Main {
@Test
public void Method1() {
InheritancePolymorphism[] tests={new SubClass1(),new SubClass2()};
for(InheritancePolymorphism test:tests){
test.Method1();
}
}

}
package InheritancePolymorphism;

public class SubClass1 extends InheritancePolymorphism{

public SubClass1() {
}
public void Method1(){
System.out.println("subclass1");
}

}
package InheritancePolymorphism;

public class SubClass2 extends InheritancePolymorphism{

public SubClass2() {
}
public void Method1(){
System.out.println("subclass2");
}

}

Inheritance Example

package Inheritance;

public class GeneralClass {
Version1Program version1=new Version2Program();

private Object test;
GeneralClass(Object test){
this.test=test;


}
void GenaralMethod(){
version1.PrintCommom3();

}




}

package Inheritance;

import java.math.BigDecimal;

import org.junit.Test;


public class Main {
@Test
public void Method1() {
GeneralClass generalclass=new GeneralClass(new Object());
generalclass.GenaralMethod();
}

}
package Inheritance;

import java.math.BigDecimal;

public class Version1Program {

private String common1;
private String common2;
private BigDecimal common3;
@SuppressWarnings({ "unused", "rawtypes" })
private Class[] test;

Version1Program() {
common1="common1default";
common2="common2default";
common3=new BigDecimal(0);
}
Version1Program(String common1,String common2, BigDecimal common3){
this.common1=common1;
this.common2=common2;
this.common3=common3;
}
Version1Program(Class<?> test){
this(new Class []{test});
}
Version1Program(Class<?> test[]){
this.test=test;
}

Version1Program(String common2)
{
this(null,common2,null);
}
Version1Program(BigDecimal common3)
{
this(null,null,common3);
}

public void PrintCommom1() {
System.out.println(common1);

}
public void PrintCommom2() {
System.out.println(common2);

}
public void PrintCommom3() {
System.out.println(common3);

}


}
package Inheritance;

import java.math.BigDecimal;

public class Version2Program extends Version1Program{
private String common11;
private String common22;
private BigDecimal common33;
@SuppressWarnings({ "unused", "rawtypes" })
private Class[] array;

Version2Program() {
common11="common11default";
common22="common22default";
common33=new BigDecimal(00);
}
Version2Program(String common1,String common2, BigDecimal common3){
super(common1,common2,common3);
this.common11=common1;
this.common22=common2;
this.common33=common3;
}
Version2Program(String common11)
{
this(common11,null,null);
}

Version2Program(BigDecimal common33)
{
this(null,null,common33);
}
Version2Program(@SuppressWarnings("rawtypes") Class[] array){
this.array=array;
}

public void PrintCommom11() {
System.out.println(common11);

}
public void PrintCommom22() {
System.out.println(common22);

}
public void PrintCommom33() {
System.out.println(common33);

}
public void PrintCommom3() {
System.out.println("I love you ............");

}


}

Generics Example

package Generics;

import java.math.BigDecimal;

import org.junit.Test;

class GenericsTest<T > {
private T t;

GenericsTest(T t) {
this.t = t;
}

void Method(String test) {
if (t.equals(test)) {
System.out.println("iiiiiiiiiiiii");

}

}

}
class GenericsTest1 <T extends Object > {
private T t;

GenericsTest1(T t) {
this.t = t;
}

void Method(String test) {
if (t.equals(test)) {
System.out.println("iiiiiiiiiiiii");

}

}

}

public class Generics {
@Test
public void M1(){
GenericsTest<BigDecimal> generics = new GenericsTest<BigDecimal>(new BigDecimal(0));
generics.Method("test");

}

}

Framework designing Tips

// if you are writing framework then the object identifatication level should be there for framework .For example Object Class is super for java .So,in framework specific we need a minimium definition of object how it should look like.Like for example of thee are suppose when loading in tomcat there are 2000 objects then 2000 object should have a common behaviour that is not default object class behaviour but our system specific behavior .Like Comapring is the object is null.Like a method check Object(Interface definition){

//interface.isNull(){
// so it works for all the objects of the system .So this functionality can be provided via frameork part
//}
 abstract class AbstractTest {
Object test;
void MethodSuper(){

}
AbstractTest(Object test){
this.test=test;

}
void Method2Super1(){

}
void Method2Super2(){

}
abstract void SubClassSpecified();



}
 abstract class AbstractTest1 {
Object test;
void MethodSuper2(){

}
AbstractTest1(Object test){
this.test=test;

}
void Method2Super2(){

}
abstract void SubClassSpecified();



}



public class AddBusineessLogic implements SuperCommonTask {

@Override
public void display() {
System.out.println("add result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Add case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Add case");

}

@Override
public void validatenput() {
}

@Override
public void ShowError() {
}

@Override
public void IsErrorPresent() {
}

}


public class BusinessLogic {
public int Add(int a,int b){
return a+b;
}
int Subtract(int a,int b){
return a-b;
}
int Divide(int a,int b){
return a%b;
}

}



public class ClientClass {
SuperCommonTask add = new AddBusineessLogic();
SuperCommonTask multiply = new MultiplyBusineessLogic();
SuperCommonTask divide = new DivideBusineessLogic();
CommonPointOfOperation CommonPointOfOperationadd = new CommonPointOfOperation(
add);
BusinessLogic businesslogic=new BusinessLogic();
void EntryPoint(int a ,int b){
add.validatenput();
}

}

public class CommonPointOfOperation {
SuperCommonTask SuperCommon;

CommonPointOfOperation(SuperCommonTask SuperCommon) {
this.SuperCommon = SuperCommon;
}

void ShowError() {
SuperCommon.appliationErro();
}

void ShowApplicationError() {
SuperCommon.appliationErro();
}

void ShowsystemError() {
SuperCommon.systemErro();

}
}

public class DivideBusineessLogic implements SuperCommonTask {
@Override
public void display() {
System.out.println("Multiply result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Multiply case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Multiply  case");

}

@Override
public void validatenput() {
// TODO Auto-generated method stub
}

@Override
public void ShowError() {
// TODO Auto-generated method stub
}

@Override
public void IsErrorPresent() {
// TODO Auto-generated method stub
}

}


public class InheritancePolymorphism {
void Method1(){
System.out.println("supermethod");
}

}


 class Client {
private State1 state;

public State1 getState() {
return state;
}

public void setState(State1 state) {
this.state = state;
}
}




public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Client client=new Client();
client.setState(State1.cancel);
State1 state1=client.getState();
System.out.println(state1+ state1.getTest());
State1[] test=State1.values();
for(State1 state:test){
System.out.println(state.getTest()+state);;
}

}
}


public class MultiplyBusineessLogic implements SuperCommonTask {
@Override
public void display() {
System.out.println("Multiply result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Multiply case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Multiply  case");

}

@Override
public void validatenput() {
// TODO Auto-generated method stub
}

@Override
public void ShowError() {
// TODO Auto-generated method stub
}

@Override
public void IsErrorPresent() {
// TODO Auto-generated method stub
}

}

public class State1{
public static enum Test{
create("1")
,cancel("3")
,read("3")
,update("2")
,initread("3")
,initcancel("3")
,initupdate("3");

Test(String test){
this.test=test;
}
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}

}
}
public interface SuperCommonTask {
void display();

void appliationErro();

void systemErro();
void validatenput();
void ShowError();
void IsErrorPresent();

}


Tuesday, November 11, 2014

Garbage Collection Analysis


public class GarbageTest {
public static void main(String args[]){
int y = 1;


// for (int x=0; y<10; x++)
// {
// System.out.println("test");
// }
for (int x=0; x<100000000; x++)
{
System.out.println("test");
}


}

}

Executor Framework Example

mport java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Test {
    private static final int NTHREDS = 10;

    public static void main(String[] args) {
      final Counter counter = new Counter();
      List<Future<Integer>> list = new ArrayList<Future<Integer>>();

      ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
      for (int i = 0; i < 500; i++) {
        Callable<Integer> worker = new  Callable<Integer>() {
          @Override
          public Integer call() throws Exception {
            int number = counter.increment();
            System.out.println(number);
            return number ;
          }
        };
        Future<Integer> submit= executor.submit(worker);
        list.add(submit);

      }
   
   
      // This will make the executor accept no new threads
      // and finish all existing threads in the queue
      executor.shutdown();
      // Wait until all threads are finish
      while (!executor.isTerminated()) {
      }
      Set<Integer> set = new HashSet<Integer>();
      for (Future<Integer> future : list) {
        try {
          set.add(future.get());
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        }
      }
      if (list.size()!=set.size()){
        throw new RuntimeException("Double-entries!!!");
      }

    }


Wednesday, October 1, 2014

Byte Code Engineering Example

package compare;

public class ImplementRunnable1 implements Runnable1{


@Override
public void method() {
System.out.println("Test");

}
void Method1(){
System.out.println("hhhhh");
method() ;


}

}


package compare;


import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

import org.objectweb.asm.*;
import org.objectweb.asm.attrs.*;

public class ImplementRunnable1Dump implements Opcodes {
// public static void main(String[] args) throws Exception {
//
// }

public static byte[] dump() throws Exception {

ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;

cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "compare/NoRunnable",
null, "java/lang/Object", new String[] { "compare/Runnable1" });

cw.visitSource("NoRunnable.java", null);

{
fv = cw.visitField(ACC_PRIVATE, "x", "Ljava/lang/String;", null,
null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
"()V");
mv.visitInsn(RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "Lcompare/NoRunnable;", null,
l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "method", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("Test");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitInsn(RETURN);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "Lcompare/NoRunnable;", null,
l0, l2, 0);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(0, "Method1", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("hhhhh");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "compare/NoRunnable",
"method", "()V");
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitInsn(RETURN);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLocalVariable("this", "Lcompare/NoRunnable;", null,
l0, l3, 0);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();

return cw.toByteArray();
}
}


package compare;

public class NoRunnable {
void Method1(){
System.out.println("i");
}

}


package compare;

public interface Runnable1 {
void method();

}


package compare;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class TestP {
@Test
public void  MethodTest() throws IOException, Exception{
ImplementRunnable1Dump d=new ImplementRunnable1Dump();
  File outputDir=new File("bin/compare");
       DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File(outputDir,"NoRunnable.class")));
       dout.write(d.dump());
NoRunnable y=new NoRunnable();
y.Method1();
}

}