Saturday, May 18, 2013

New Date & Time API in JDK8

The existing Java date and time classes are poor, mutable, and have unpredictable performance. There has been a long-standing desire for a better date and time API based on the Joda-Time project. The new API will have a more intuitive design allowing code to better express its intent. The classes will also be immutable which aligns with the multi-core direction of the industry.

package com.lambada.practice;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;

public class EarlyReleaseDemo {

 
 public static void main (String [] args) {
  new EarlyReleaseDemo().dataAndTimeAPI();
 }
 
 
 public void dataAndTimeAPI() {
  Clock clock = Clock.systemUTC();
  System.out.println("Current time in milisecond : " +clock.millis());
  
  LocalDate date = LocalDate.now();
  
  LocalTime localTime = LocalTime.now();

  
  
  ZoneId zoneId = ZoneId.systemDefault();
  
  //console output
  System.out.println("Current Time : " +localTime);
  System.out.println("Current Hour : " +localTime.getHour());
  System.out.println("Current Min. : " +localTime.getMinute());
  System.out.println("Current Second : " +localTime.getSecond());
  System.out.println("Todays date : " +date);
  System.out.println("Year : "+date.getYear());
  System.out.println("Month Value : "+date.getMonthValue());
  System.out.println("Month :"+date.getMonth());
  System.out.println("Day of Month :"+date.getDayOfMonth());
  System.out.println("Day of year : "+date.getDayOfYear());
  System.out.println("System default zone : " +zoneId.normalized());
  System.out.println("Available Zone Id's :" + zoneId.getAvailableZoneIds());

 }
}


//output
Current time in milisecond : 1368893133938
Current Time : 22:05:34.048
Current Hour : 22
Current Min. : 5
Current Second : 34
Todays date : 2013-05-18
Year : 2013
Month Value : 5
Month :MAY
Day of Month :18
Day of year : 138
System default zone : Asia/Almaty
Available Zone Id's :[America/Virgin, Europe/London, Atlantic/Reykjavik, Canada/Eastern, Africa/Ndjamena, America/Grenada, Asia/Saigon, Asia/Muscat, Europe/Sarajevo, America/Belem, Eire, Greenwich, Universal, Asia/Kashgar, ...........
Reference's : http://openjdk.java.net/jeps/150
http://ttux.net/post/java-8-new-features-release-performance-code/

Wednesday, May 08, 2013

Ad hoc polymorphism With Java Example

In programming languages, ad hoc polymorphism[1] is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. It is also known as function overloading or operator overloading. The term "ad hoc" in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system. This is in contrast to parametric polymorphism, in which polymorphic functions are written without mention of any specific type, and can thus apply a single abstract implementation to any number of types in a transparent way. This classification was introduced by Christopher Strachey in 1967.
(ref : http://en.wikipedia.org/wiki/Ad-hoc_polymorphism)

// credit to choychoy
List list = new ArrayList(Arrays.asList(1,2,3));
int v = 1;
list.remove(v);
System.out.println(list); // prints [1, 3]

List list = new ArrayList(Arrays.asList(1,2,3));
Integer v = 1;
list.remove(v);
System.out.println(list); // prints [2, 3]

http://blog.functr.com/2012/08/java-oddities-part-ii.html
http://www.reddit.com/r/programming/comments/z2n9f/javawat_part_i/

Script to Disable Default F5 key using java script

function disableKey(event) {

 if (!event)
  event = window.event;
 if (!event)
  return;

 var keyCode = event.keyCode ? event.keyCode : event.charCode;
 if (keyCode == 116 || (event.ctrlKey && keyCode == 82)) {
   /* window.status = "F5 key detected! Attempting to disabling default
   * response."; window.setTimeout("window.status='';", 2000);
   */
  if (event.preventDefault)
   event.preventDefault();

  //IE (exclude Opera with !event.preventDefault):
  if (document.all && window.event && !event.preventDefault) {
   event.cancelBubble = true;
   event.returnValue = false;
   event.keyCode = 0;
  }

  return false;
 }
a
}

function setEventListener(eventListener) {
 if (document.addEventListener)
  document.addEventListener('keypress', eventListener, true);
 else if (document.attachEvent)
  document.attachEvent('onkeydown', eventListener);
 else
  document.onkeydown = eventListener;
}

function unsetEventListener(eventListener) {
 if (document.removeEventListener)
  document.removeEventListener('keypress', eventListener, true);
 else if (document.detachEvent)
  document.detachEvent('onkeydown', eventListener);
 else
  document.onkeydown = null;
}

setEventListener(disableKey)


Modules of Spring Framework

Introduction :

Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
Spring enables you to build applications from “plain old Java objects” (POJOs) and to apply enterprise services non-invasively to POJOs. This capability applies to the Java SE programming model and to full and partial Java EE.
Examples of how you, as an application developer, can use the Spring platform advantage:
  • Make a Java method execute in a database transaction without having to deal with transaction APIs.
  • Make a local Java method a remote procedure without having to deal with remote APIs.
  • Make a local Java method a management operation without having to deal with JMX APIs.
  • Make a local Java method a message handler without having to deal with JMS APIs.

    (Reference : http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/overview.html)

    Modules  of Spring Framework :

    Core Container

    The Core Container consists of the Core, Beans, Context, and Expression Language modules.
    The Core and Beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.
    The Context module builds on the solid base provided by the Core and Beans modules: it is a means to access objects in a framework-style manner that is similar to a JNDI registry. The Context module inherits its features from the Beans module and adds support for internationalization (using, for example, resource bundles), event-propagation, resource-loading, and the transparent creation of contexts by, for example, a servlet container. The Context module also supports Java EE features such as EJB, JMX ,and basic remoting. The ApplicationContext interface is the focal point of the Context module.
    The Expression Language module provides a powerful expression language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification. The language supports setting and getting property values, property assignment, method invocation, accessing the context of arrays, collections and indexers, logical and arithmetic operators, named variables, and retrieval of objects by name from Spring's IoC container. It also supports list projection and selection as well as common list aggregations.

    Data Access/Integration

    The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules.

    Web

    The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet modules.
    Spring's Web module provides basic web-oriented integration features such as multipart file-upload functionality and the initialization of the IoC container using servlet listeners and a web-oriented application context. It also contains the web-related parts of Spring's remoting support.

    AOP and Instrumentation

    Spring's AOP module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated. Using source-level metadata functionality, you can also incorporate behavioral information into your code, in a manner similar to that of .NET attributes.
    The separate Aspects module provides integration with AspectJ.
    The Instrumentation module provides class instrumentation support and classloader implementations to be used in certain application servers.

    Test

    The Test module supports the testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.

Wednesday, May 01, 2013

Drop Down List in GWT

To achieve this, we need to work on ListBox of GWT.
public class ListBoxExample implements EntryPoint {

  public void onModuleLoad() {
    // Make a new list box, adding a few items to it.
    ListBox lb = new ListBox();
    lb.addItem("foo");
    lb.addItem("bar");
    lb.addItem("baz");
    lb.addItem("toto");
    lb.addItem("tintin");

    // Make enough room for all five items (setting this value to 1 turns it
    // into a drop-down list).
    lb.setVisibleItemCount(1);

    // Add it to the root panel.
    RootPanel.get().add(lb);
  }
}