Skip to main content


Angular 2 Switch Statement

The ngSwitch directive is an inbuilt directive that lets you hide/show HTML elements depending on an expression.
NgSwitch uses ngSwitchCase keyword to define a set of possible element which will be selected based on the ngSwitchCase
<element ngSwitch="expression">   <element *ngSwitchCase="value"></element>   <element *ngSwitchCase="value"></element>   <element *ngSwitchCase="value"></element> </element> For Example say we want to design a simple Component which renders different kind ofInput fields like Text, TextArea etc based on the parameters we are sending. Then weneed some kind of If/Else or Switch statements to select the case.
Recent posts

Working with Base64 Decoded Image in Angular

We can save images in base64 encoded format in database. It has it's own advantages and disadvantages. But while loading those images in Angular Application, based on different factors like Content Security Policy etc, Angular does not allow to load base64 encoded images in the HTML. It will append unsafe to the url, as a result the HTML does not show the image properly.

For instance if I want to show the following image which is in base64.
data:image/png;base64,aaaiskkAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSAAAgAEl...== I will have something similar in the HTML
<p><img src="data:image/jpeg;base64,/aaaiskk....></p> But due to Content Security policy Angular will not load the above URL. And if you see the source then you will see that unsafe is appended before the base64 URL.

One way to workout in Angular is to Sanitize the URL so that Angular can load the URL. For that we can import DomSantitizer from @angular/platform-browser.

//Angular Controller
import {DomS…

Rest API Design Reference

REST stands for Representational State Transfer. It is an architectural style for developing software.

Following are the Best Practices for Designing and Developing REST

REST Resource Naming Best Practices

Use nouns to represent resources not verbs.To describe names use concrete names (Nouns) rather then verbs. RESTful URI should refer to a resource that is a thing (noun) instead of referring to an action (verb) because nouns have properties as verbs do not - similar to resources have attributes.

Example GET /questions GET /questions/1 Rather then   GET /retrieveQuestions GET /getQuestionWithID/1

URI Case

Use lowercase letters in URIs.  According to RFC3986, URLs are “case sensitive” (except for the scheme and the host). When convenient, lowercase letters should be consistently preferred in URI paths.

Do not use trailing forward slash (/) in URIs

Use hyphens (-) to improve the readability of URIs (Spinal-Case)

Do not use underscores ( _ )

Use forward slash (/) to indicate a hierarchical relationshi…

Java Constructor vs method

Functionally difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.

Constructors can't be called directly. They are invoked implicitly when the new keyword creates an object.
Methods can be called directly on an object that has already been created with new.

Constructors must be named with the same name as the class name.

Constructors cannot  return anything, even void .

Methods must be declared to return something ( or void).

Constructor compiler provide default constructor where as method compiler does't provide.

Constructor cannot be final since it cant be inherited.

Constructor cannot be abstract since it is used to create and initialize object.

Constructor cannot be static since it is used to create instance of object.

Only public, protected & private modifiers are permitted.

immutable objects in java

In software programming, an immutable object is an object whose state cannot be modified after it is created.

How to make Immutable Object in Java

Make the class final.Make all members final, set them explicitly, in a static block, or in the constructor.Make all members private.Do not expose any setter methods.If the instance fields include references to mutable objects, then don't share references to the mutable objects. For getter methods which needs to return mutable objects, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.Do not store references to external mutable objects passed to the constructor. Create copies, and store references to the copies. 

Advantage of Immutable Objects

Immutable Objects are ThreadSafe.
Value Objects are generally considered Immutable. In java String and all the Wrapper Classes like Integer, Boolean are immutable.
It enhances security and less bug when the object we are designing is not sup…

Better ways to implement Singleton in Java

Singleton Design pattern restrict creation of only once instance of a Class. That means inside a single JVM only one instance of the Singleton Class will be created.  In java we have broadly two options to implement Singleton.

2) Lazy Creation of the Object Instance: Here the instance is created on first invocation.

Following example demonstrate how to implement Singleton in Java.

Method 1: Early Creation of Object Instance
publicclassSingletonClass{privatestaticfinal SingletonClass INSTANCE =new SingletonClass(); //Private Constructor private SingletonClass(){ //Do nothing}publicstatic SingletonClass getInstance(){ return INSTANCE;}} Here the instance is created ahead even before calling the Class. We need to provide a private constructor otherwise Java Compiler will add a default Constructor.

Method 2: Lazy Creation of Object Instance
public class SingletonClass { privatestaticvolatile SingletonClass INSTANCE =null;private

Java on Mac

Listing all installed Java
/usr/libexec/java_home -V
Matching Java Virtual Machines (5):     1.8.0_45, x86_64:"Java SE 8"/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home     1.8.0_25, x86_64:"Java SE 8"/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home     1.7.0_51, x86_64:"Java SE 7"/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home     1.6.0_65-b14-466.1, x86_64:"Java SE 6"/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home     1.6.0_65-b14-466.1, i386:"Java SE 6"/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Selecting a particular version /usr/libexec/java_home -v 1.8