Wednesday, October 16, 2013

Immutable class

Immutable class is the one in java whose contents can not be changed once initialized. e.g all the Wrapper classes in java e.g String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger are immutable.

Creating an immutable class:-
  • Declare the class as final.


Features:-
  • They are thread safe automatically so no need to synchronize.
  • Once created the state of the object can not be changed so there is no possibility of going to inconsistent state.
  • The reference can be easily shared and cached without the need to copy them
  • They can be used as keys for the map

Sunday, October 13, 2013

ANT

Apache Ant is a Java based build tool to automate the manual steps used in compiling and deploying a JAVA application

It involves a build file that should be inside the project directory. This build file must have
  • A project element
  • At least one target element

<?xml version="1.0"?> 
 <project name="PROJECT_NAME" default="info" basedir="BASE_DIRECTORY"> 

 <property name="PROPERTY_NAME" value="PROPERTY_VALUE" /> 
 <property file="PROPERTY_FILE_NAME"/>

<patternset id="PATTERN_SET_ID"> 
<include name="PATTERN_OF_FILES_TO_BE_INCLUDED"/>  
<exclude name="PATTERN_OF_FILES_TO_BE_EXCLUDED"/>
 </patternset>


<filelist id="FILELIST_ID" dir="DIRECTORY_OF_FILES"> 
 <file name="FILE_l"/>  
<file name="FILE_2"/> 
 </filelist>
 
 <target name="TARGET_NAME" depends="TARGET_1, TARGET_2, TARGET_3,.." description="TARGET_DESCRIPTION" if="IF_CONDITION" unless="UNLESS_CONDITION"> 

 <echo>Hello World - Welcome to Apache Ant! $(PROPERTY_NAME)</echo> 

<delete>
<fileset dir="${PROPERTY_NAME}" casesensitive="YES_NO">  
<include name="PATTERN_OF_FILES_TO_BE_INCLUDED"/>  
<exclude name="PATTERN_OF_FILES_TO_BE_EXCLUDED"/> 
 </fileset>
 </delete>


<delete>
<fileset dir="DIRECTORY_OF_FILES" casesensitive="yes"> 
 <patternset refid="PATTERN_SET_ID"/> 
 </fileset>
</delete>
 </target>

 <target name="TARGET_NAME">
 <zip destfile="DESTINATION_FILE_NAME" >
 <filelist refid="
FILELIST_ID" />
 </zip>
 </target








   <target name="TARGET_NAME">
   <copy todir="DESTINATION_DIRECTORY">
   <fileset dir="SOURCE_DIRECTORY" includes="FILTER_PATTERN"></fileset>   
   <filterset>
   <filter token="TOKEN" value="REQUIRED_VALUE_OF_TOKEN"></filter>
   </filterset>
   </copy>
   </target>


  </project>

PROJECT_NAME:- is optional and is the name of the project.
PROPERTY_NAME: is optional and is used to set the name for a variable/property.
PROPERTY_VALUE: is optional and is used to set the value for a variable/property.
PROPERTY_FILE_NAME: is optional and is used to set the name for properties file. 
DEFAULT:- is mandatory and is used to set the default target 
BASE_DIRECTORY:- is optional and used to set the base directory for the project.
TARGET_NAME: is madatory and used to set the name of the target.
TARGET_1, TARGET_2, TARGET_3,..; are optional and are comma separated list of targets on which the TARGET_NAME depends.
TARGET_DESCRIPTION:- is optional and is used to describe a target
DIRECTORY_OF_FILES:-It is used to set the directory where the filelist or fileset is to be generated.
PATTERN_OF_FILES_TO_BE_INCLUDED:- It is used to set the pattern of the files to be included in the datatype.
PATTERN_OF_FILES_TO_BE_EXCLUDED:- It is used to set the pattern of the files to be exceluded in the datatype.
PATTERNS can be set as:-
  1. To match one character we can use ?.java which will include files like A.java, B.java but not AB.java
  2. To match or or more charates we can use  *.java which will includes ny file with extension .java
  3. To match zero or more directories recursively we can use ** 
PATTERN_SET_ID:- It can be used to provide a name to a pattern set so that it may be used again and again in build file.
FILELIST_ID:- It can be used to provide a name to a file list so that it may be used again and again in build file.
TOKEN:- It can be string in the file that is surrounded by separators @ e.g. @VERSION@. It is replaced by the value as given in the attribute value of the filter tag.
Predefined properties in ANT:-
  1. ant.file = The full location of the build file.
  2. ant.version = The version of the Apache Ant installation.
  3. basedir = The basedir of the build, as specified in the basedir attribute of the project element.
  4. ant.java.version = The  version of the JDK that is used by Ant.
  5. ant.project.default-target = The default target of the current project
  6. ant.project.invoked-targets = Comma separated list of the targets that were invoked in the current project
  7. ant.core.lib = The full location of the ant jar file
  8. ant.home = The home directory of Ant installation
  9. ant.library.dir = The home directory for Ant library files - typically ANT_HOME/lib folder.
Data Types in ANT:- Data types here mean the services that are provided by ANT by default
  1. FileSet:- It is used to define a collection of files using pattern set or wild cards .
  2. PatternSet :- It is used to create a pattern of files to be used in fileset.
  3. FileList :- It is used to define a collection of files using the explicit names and wild cards can not be used.
  4. FilterSet:-
  5. Path
Note:- Pattern set can be used inside fileset but filelist can not be.

To run a target go to the location of build file in command prompt and type 
ant TARGET_NAME

Features:-
  • It automates the;-
  1. Compilation of code
  2. Packaging of binaries
  3. Deployment to the server
  4. Test the change.
  • It is platform independent.

Wednesday, October 2, 2013

Inheritance in JAVA

Access Specifiers in JAVA

Access Specifiers in JAVA :-


Class Package Subclass in Same Package Subclass in Different Package World
public Y Y Y Y Y
protected Y Y Y Y
default Y Y Y
private Y


Public is accessible everywhere.
Protected can not be assessed in outside the subclasses or the same package.
Default can not be accessed in subclasses in different packages.
Private can be accessed in the class only.

The difference between the protected and default is that the protected can be accessed in subclasses in different class but default can not be.

Example

package com.anshul.projects.iwts.tutor.accessspecifier;

public class MyClass {
    public final static int public_variable= 5;
    protected final static int protected_variable = 5;
    final static int default_variable = 5;
    private final static int private_variable = 5;
  
    public void public_method(){
        System.out.println("public_method");
    }
  
    protected void protected_method(){
        System.out.println("protected_method");
    }
  
    void default_method(){
        System.out.println("default_method");
    }
  
    private void private_method(){
        System.out.println("private_method");
    }


   public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }
}


package com.anshul.projects.iwts.tutor.accessspecifier;

public class Package {
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

package com.anshul.projects.iwts.tutor.accessspecifier;

public class SubClass extends com.anshul.projects.iwts.tutor.accessspecifier.MyClass{
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

package com.anshul.projects.iwts.tutor.accessspecifier.subpackage;
import com.anshul.projects.iwts.tutor.accessspecifier.MyClass;

public class OtherPackageSubClass extends MyClass{
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }  }

package com.anshul.projects.iwts.tutor.accessspecifier.subpackage;
import com.anshul.projects.iwts.tutor.accessspecifier.MyClass;

public class World {
    public static void main (String args[]){
        MyClass mclass = new MyClass();
        System.out.println(mclass.public_variable);
        System.out.println(mclass.protected_variable);
        System.out.println(mclass.default_variable);
        System.out.println(mclass.private_variable);
        mclass.public_method();
        mclass.protected_method();
        mclass.default_method();
        mclass.private_method();
    }}

All the red highlighted lines will be giving compile time error "Method" or "Variable" is not visible



SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...