Tuesday, 13 September 2016

Java Enum

The purpose of using enum is to constrain user inputs from a limited set of instances. 

Implicitly, Enum is a final class that extends from a Java abstract Enum class.  The enum has some implicit operations done by java compiler.  The compiler adds a static block in order to initialise all public static constants. By default, the enum class inherits a two-argument constructor from its superclass, and it includes the name and ordinal of the constant. Enum name() and toString() gives the same values, and Enum instance is ordered by its definition, and thereby constants cannot be sorted by an external sorting algorithm. 

Java enum cannot extend from other classes; for it implicitly extends abstract java.lang.Enum class. However, a Java enum can implement many interfaces. Java enum cannot be extended, because it is defined as a final class implicitly. 

Enum name(), hashCode() method is a final method, but toString() is not, so toString is allowed to be overridden. 

the enum allows adding extra attributes for each constant.
the enum allows private or default-access level constructors.
the enum cannot define constructors with a public or protected access level.
the enum allows set/get methods to access extra attributes on each constant.

An enum can define an abstract method. just ensure to override it for all your enum constants.
The enum method values are initialised in its super-class static block, it contains all defined enum constants.

Constants may not be followed by a semicolon if there is no other variables or methods defined inside this enum body. 

Here I created a simple example, which defines all directions, i.e. east, west, south, north; and I give each direction an extra attribute, i.e. degree. It can be initialized by its private constructor.

The following example shows that Enum value and attributes can be initialized by its constructors.
The toString() method can be overridden; it offers a convenient way to show each value and its attributes by one kick. 

One interesting thing, when I use Enum in the switch body, it doesn't need Enum identifier to refer to its own values. I guess the java compiler is becoming smarter and smarter. It can infer from switch input argument type itself. Once it finds an Enum type, then it will ignore the Enum identifier.

A constant specific class body defines overriding methods for a particular enum constant. 

Where can you define an enum? 
Enum can be independent, or a member of a class or an interface. However, Enum cannot be local within a method.



/*
 * In Java, Enum is a class type. 
 * 
 * 
 */
package Enum;

import java.util.Arrays;

/**
 *
 * @author YNZ
 */
public enum Direction {
    EAST(10), SOUTH(20), WEST(40), NORTH(30);
    
    //Enum constructor must be private or pakage-private(default access modifier)
    private Direction(double degree) {
        this.degree = degree;
    }

    private Direction() {
        this.degree = 0;
    }

    public static Direction getEAST() {
        return EAST;
    }

    public static Direction getNORTH() {
        return NORTH;
    }

    public static Direction getWEST() {
        return WEST;
    }

    public double getDegree() {
        return degree;
    }

    public static Direction getSOUTH() {
        return SOUTH;
    }

    @Override
    public String toString() {
        return this.name() + " " + this.getDegree();
    }
    
    //init. once as it is contructed. 
    private final double degree;
}


class UsingEnum {

    public static void main(String[] args) {
        System.out.println(Direction.EAST);
        Direction d = Direction.valueOf("EAST");
        System.out.println("" + d);
        System.out.println(Arrays.toString(Direction.values()));
        
        tellDirection(Direction.EAST);
        tellDirection(Direction.NORTH);
        
        //adding get method to return client attribute. 
        System.out.println(""+Direction.EAST.getDegree());
        
        //how to traverse Enum members by a loop
        for(Direction direction: Direction.values()){
            System.out.println(direction.name() + " " +direction.getDegree());
        }
        //overriding toString() to print out enum member properties.
        System.out.println(" "+ Arrays.toString(Direction.values()));
                
        
    }

    public static void tellDirection(Direction direction) {
        //Enum value can be inferred without using identifier. 
        switch (direction) {
            case EAST:
                System.out.println("Goto East.");
                break;
            case SOUTH:
                System.out.println("Goto South");
                break;
            case WEST:
                System.out.println("Goto West");
                break;
            case NORTH:
                System.out.println("Goto North");
                break;
            default:
                System.out.println("Goto no where");

        }

    }
}


Thursday, 8 September 2016

Time Complexity

Time complexity is a  way to evaluate different algorithm performance. This is a relative method, which evaluates the order of input numbers ref. to the steps to be consumed. So the best performance is independent of input numbers, i.e, n power of zero;  The worst case, the performance is a result of n square. 

Types of Time Complexity: 
the worst case, the best case, and average case. 

Assumption: 
We have infinite memory. 
No matter what kind of operations, we assume it takes one unit of time. 

List implementations:
                      get  add  contains next remove(0) iterator.remove
ArrayList             O(1) O(1) O(n)     O(1) O(n)      O(n)
LinkedList            O(n) O(1) O(n)     O(1) O(1)      O(1)
CopyOnWrite-ArrayList O(1) O(n) O(n)     O(1) O(n)      O(n)
Set implementations:
                      add      contains next     notes
HashSet               O(1)     O(1)     O(h/n)   h is the table capacity
LinkedHashSet         O(1)     O(1)     O(1) 
CopyOnWriteArraySet   O(n)     O(n)     O(1) 
EnumSet               O(1)     O(1)     O(1) 
TreeSet               O(log n) O(log n) O(log n)
ConcurrentSkipListSet O(log n) O(log n) O(1)
Map implementations:
                      get      containsKey next     Notes
HashMap               O(1)     O(1)        O(h/n)   h is the table capacity
LinkedHashMap         O(1)     O(1)        O(1) 
IdentityHashMap       O(1)     O(1)        O(h/n)   h is the table 
EnumMap               O(1)     O(1)        O(1) 
TreeMap               O(log n) O(log n)    O(log n) 
ConcurrentHashMap     O(1)     O(1)        O(h/n)   h is the table 
ConcurrentSkipListMap O(log n) O(log n)    O(1)
Queue implementations:
                      offer    peek poll     size
PriorityQueue         O(log n) O(1) O(log n) O(1)
ConcurrentLinkedQueue O(1)     O(1) O(1)     O(n)
ArrayBlockingQueue    O(1)     O(1) O(1)     O(1)
LinkedBlockingQueue   O(1)     O(1) O(1)     O(1)
PriorityBlockingQueue O(log n) O(1) O(log n) O(1)
DelayQueue            O(log n) O(1) O(log n) O(1)
LinkedList            O(1)     O(1) O(1)     O(1)
ArrayDeque            O(1)     O(1) O(1)     O(1)
LinkedBlockingDeque   O(1)     O(1) O(1)     O(1)

Wednesday, 7 September 2016

SpringBoot Unserstanding

It is a wonderful framework.

Spring boot is not a new version spring framework, but a cover layer residing on the spring framework techniques. Before it, although Spring can do a lot for developers, it requires a lot.

The purpose of boot is to release developer from doing trivial, for instance, a lot of XML, annotation configurations and dependency setups. Thus, developers may focus on implementing Java code and business logic.

Developer focus on coding functionalities rather than configuring spring framework. SpringBoot auto-configuration may consider what dependencies in the classpath, via which to infer what needs to be configured. It is very impressive as developing with SpringBoot for it added two ways of development automation, i.e. starter dependencies and automatic configurations.  Consequently, it is pretty fast to build up a web application now.

Instead of auto-configuration, what if you want to configure SpringBoot differently? SpringBoot allows overriding auto-configuration as needed to achieve the goals of your applications.


Conditional loading:

  • @ConditionalOnClass
  • @ConditionalOnBean
  • @COnditonalOnProperty
  • @ConditonalOnMissingBean

 Properties based: 

Properties are also a big part of the default configuration for Spring Boot. 

  • Preconfigured "default" properties for AutoConfiguration classes. 
  • @EnableConfigurationProperties specifies the default property set.
  • Properties can always be  overridden

Additional conditionals:

Application type based
Resource (or file) based 
Expression based


Configuring Spring boot

Property-based configurations:  it is the common way
applcation.properties and application.yml

  • Environment variables. (runtime environment) 
  • Command-line injections
  • Cloud configurations(config server,valut,consul) 

Bean configuration: 

  • Adding beans to the default application class??
  • Adding beans to separate configuration classes.
  • Importing XML-based configurations.
  • Component scanning





Tuesday, 6 September 2016

Arrays.asList returns an unmodified array list


Arrays.asList returns an un-modified ArrayList instance, i.e. read only .
        List list = Arrays.asList(12,10,129);
        list.add(15);
It throws a runtime exception, java.lang.UnsupportedOperationException

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...