Skip to main content

Posts

array-rotation-to-left-by-given-distance-d

array-rotation-to-left-by-given-distance-d Array left rotation by a given number d Array left rotation by a given number d Step 1: Take two pointers 1. i = 0 2. j = arr[length] Step 2: 1. Swap index of all elements from i to d. 2. Swap index of all elements from d+1 to arr[length]. Step 3: Swap index of all elements from i to arr[length].

Git : Ignore changes to a file that is already tracked in repository

If you are trying to ignore changes to a file that's already tracked in the repository (e.g. a dev.properties file that you would need to change for your local environment but you would never want to check in these changes) than what you want to do is: git update-index --assume-unchanged If you wanna start tracking changes again git update-index --no-assume-unchanged Reference Reading If you are trying to ignore changes to a file that's already tracked in the repository (e.g. a dev.properties file that you would need to change for your local environment but you would never want to check in these changes) than what you want to do is: ``` git update-index --assume-unchanged ``` If you wanna start tracking changes again ``` git update-index --no-assume-unchanged ``` [Reference Reading](https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html)

Angular : How to start angular CLI on custom port

How to start Angular 2 Application on a Custom port other than 4200 You can start the angular project using the angular cli using command ng serve The above command will start the project on port 4200 . If you want to start the application on a prot different that 4200 then you can start the project using the following command. ng serve --port <port-number> where <port-number> can be any valid port that is not in use.

Angular : How to Inject Nested Service in Component

How to Inject Nested Services in Component Let's create a service url.service.ts that will will be injected in other services. import { Injectable } from '@angular/core' ; @Injectable () export class UrlService{ // business logic } Now lets create another service http.service.ts that will Inject this service. import { UrlService } from './url.service.ts' ; export class HttpService extends UrlService{ // business logic } Now, we need to inject the HttpService in some component to make http calls. So now we can inject the HttpService in component as shown below. import { Component ,OnInit } from '@angular/core' ; import { UrlService } from './url.service.ts' ; import { HttpService } from './http.service.ts' ; @Component ({ selector : 'some-selector' , templateUrl : 'some.html' , providers: [HttpService, UrlService]...

Angular : Getting started with Angular with Angular CLI, Installation and Hello World Example

Install and Setup Angular Before we start, lets install the angular cli with the following command npm install -g @angular/cli Angular CLI is a tool that can make a new project, compile your typescript files, configure your typescript compiler run your code with live preview, build and package your project. Note Make sure you have latest node and npm installed, at least node version 6.9.x and npm version 3.x.x . To see the versions you can run commands node -v and npm -v for node and npm versions respectively. Warning While installation you might get the following warning npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/@angular/cli/node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":...

Node : How to uninstall a nodejs global dependency

Uninstall global node dependency To uninstall a node dependency installed with -g command you can use the following command. npm uninstall -g <dependency> The above command will remove that dependency from your system. Uninstall node dependency To remove a dependency from your project you can use the following command. npm uninstall <dependency> This will remove the dependency and remove its entry from the package.json file.

Node : How to Upgrade NodeJs and Npm and How to install latest NodeJs on Linux, Ubuntu

Points to Remember If you are installing node for the first time then, use the NodeSource PPA and first choose the version of node you want to install. Following are the sources for the node versions for Node.js v4 curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - for Node.js v5 curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - for Node.js v6 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - for Node.js v7 curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - Once this is done, then update your repositories by command sudo apt-get update Now, finally install NodeJs and Npm using the following command sudo apt-get install nodejs sudo apt-get install npm To check the version of node, try command node -v and npm version by command npm -v Upgrading NodeJs and Npm If you already have node and npm install and...

Groovy : What is Identity Operator and difference between == in Java and Groovy

What is Identity == operator In Java, == operator is used for checking the reference of objects. In Groovy, it is used to check if the object are equals, i.e it is the implementation of equals() method in java. == operator in In java, checks the reference of the objects. In groovy, == is equivalent to .equals() and checks if the two objects are equal is operator In java, does not exist In groovy, checks the reference of the objects which is == in java equals() method In java, checks if the two objects are equal In groovy, checks if the two objects are equal Let's have a look at the following examples class A { String name; boolean equals(A a){ this .name == a.name } } A a1 = new A( name : 'ekiras' ) A a2 = new A( name : 'ekiras' ) println (a1 == a2) println (a1.equals(a2)) The above ...

Groovy : What is Coercion operator and how to use it

Point to Remember Coercion Operator is used for casting Custom Coercion rules can be applied to a class by defining asType method in a class. Syntax of coercion operator is as What is Coercion Operator Coercion operator is used to do type casting of objects in groovy. It converts objects of one type to another type. Lets take a look at the following examples int a = 123 String s = (String) a The the above example, the assignment of a to s will give a ClassCastException in java but when you run the same in groovy it will not give an error since Groovy as defined how to typecast integer to string. Coercion version of the above example is as follows int a = 123 String s = a as String How to Define Custom Coercion Rules. Let's take an example where we want to convert an object of class A to class B. class A { String name } class B { String name...

Groovy : What is Direct field access operator and how to use it

Direct field access operator In groovy when we access the property of any object as object.property then groovy under the hood calls the getter method of the property even if we have not specified it. The default getter and setter methods return the property and set the property respectively. Let's have a look at the example. ​ class User { String name String email String getName(){ return "Name is $name" } } User u = new User( name : 'ekiras' , email : 'ekansh@ekiras.com' ) println u.name When you execute the above code you will get the output Name is ekiras . Note What happens under the hood is u.name will internally call the getter method of instance variable name . Here calling u.getName()' is equivalent to calling u.name Also, all the classes and their instance variables are by default public in nature. So you can access the instance variable without calling the getter method byb using the Direct Field Access Operator S...

Groovy : What is Elvis Operator and difference b/w Elvis and Ternary operator

Ternary Operator Ternary Operator is the short hand of the if-else block , where a condition is evaluated to true or false and following statement is executed. Syntax of the ternary operator is below < boolean expression> ? <executed when condition is true > : <executed when condition is false > A typical example of if-else block is shown below where we check if a variable is null or not. String s = "hello" if ( s != null ) println s else println "not found" We can replace the above code with the ternary operator as shown below String s = "hello" println s != null ? s : "not found" Here, the variable s is assigned a string hello and condition s!=null evaluates to true hence the statement after ? is executed which returns s itself. Elvis Operator Elvis operator is the short hand of the Ternary operator explained above. In t...

Groovy : What is Safe Navigation operator or Null check Operator

Safe navigation operator The safe navigation Operator is used to avoid null pointer exceptions. Many a times you come across the need to access the value of an object if it is not null. Syntax for the Safe Navigation Operator is <object>?.<property> Here the could be any object and could be any property of the object. Let's have a look at the below example, class User { String name; } User u = new User(name : "ekiras" ) if (u != null ) println u.name // prints ekiras In the above code we have to write the if block to check if the user object is null or not. So, with the Safe navigation operator we do not need to write the if-else blocks. We can rewrite the above code as shown below class User { String name; } User u = new User( name : "ekiras" ) println u?.name // prints ekiras

Gradle : How to upload the Jar or War file to custom path in local machine

Points to Remember Default location for gradle dependencies is ~/.gradle or /home/{user}/.gradle . We can specify the location from where to gradle should find dependencies and where it should upload the created artifacts. How to upload an Artifact to a custom location in file system buildscript { repositories { maven { url uri( "$buildDir/repo" ) } } } group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'groovy' apply plugin: 'maven' repositories { mavenCentral() } dependencies { // dependencies } uploadArchives { repositories { mavenDeployer { repository(url: uri( "$buildDir/repo" )) } } } In the above example, uploadArchives { repositories { mavenDeployer { repository(url:...

Gradle : How to make a custom War file

Points to Remember War task extends Jar You can create war files with any configuration defined in configurations { } closure You can also add files to an existing war file. You can select the files that needs to be included or excluded while creating a war file How to create a War file in Gradle To create a war file you have to create a task of type War as shown below // include java plugin apply plugin : 'java' task createWar(type : War){ destinationDir = file ( "$buildDir" ) baseName = "my-war" version = "1.1" caseSensitive = true classifier = "SNAPSHOT" from "src" } Run the above task with command gradle -q createWar , this will create a war file named my-war-1.1-SNAPSHOT.war in the build folder. See Full Documentation of War Task How to create a War file and exclude some files Now ...

Groovy : What is Groovy Truth and define Custom Groovy Truth

What is Groovy Truth Groovy has its own way of defining what is true and what is false, this is called the Groovy Truth . In Java, we have to write statements like if(obj ==null) , in groovy you can simply write if(obj) and you can define in what conditions this object will evaluate to true and when it will evaluate to false. Info To define your custom logic for groovy Truth you need to define method boolean asBoolean(){ // your logic here } What is True in Groovy Following evaluated to true in groovy boolean true Not null objects Non 0 numbers Not null and not empty Strings Not null and not empty lists, maps, collections What is False in Groovy Following things evaluates to false in groovy boolean false Numeric 0 number Null objects Null Strings Null or empty lists, maps, collections def groovyTruth = {val-> if (val) println "$val is true" else println "$val is false" } boolean b1 = true boolean b2 = false groovyTruth(b1) // true groovyTru...

Browserify : How to use Node.js require dependencies in Browser as Javascipt

How to get Browserify Use the following to download browserify . npm install -g browserify Automate Browserify Dependency You can also create a package.json file by the following command. npm init This will create a basic package.json file. now run the command npm install --save browserify --save option will download the file and save its reference in package.json . Now on other machine you just need to run the command npm install and it will download the browserify plugin and save it on your machine. This will download the browserify in node_modules folder. Now we can use browserify to download our node dependencies. For this we will first create a .js file named require.js , you can name it anything you want. Let's see a sample file. angular = require ( 'angular' ); ngSanitize = require ( 'angular-sanitize' ); Now, to make browserify work you should have all these modules installed...

Gradle : How to use Gradle Cargo Plugin to Deploy war on Tomcat

This post is based on the Gradle Cargo Plugin by bmuschko Include the Plugin You need to include the plugin in your build.gradle . buildscript { repositories { jcenter() } dependencies { classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3' } } apply plugin: 'com.bmuschko.cargo' buildscript block should be the first thing in the build.gradle file. Include the Gradle Cargo plugin dependencies Add the following in your gradle dependencies block dependencies { def cargoVersion = '1.4.5' cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion" , "org.codehaus.cargo:cargo-ant:$cargoVersion" } Define the Tomcat Container To deploy The war files to Tomcat you need to create a dsl by name cargo and define the mandatory containerId . Note ContainerId will define the version of tomcat you...

Tomcat : How to start Tomcat as a service on Windows 7,8,10

Step 1 : Download and Extract tomcat Download tomcat from the link in .zip format. Once download is complete extract the tomcat in the location you want, preferably C:\Tomcat\ . Step 2 : Install Tomcat as a Service Go to the tomcat directory C:\Tomcat\bin . in the address bar write cmd . This will open the command prompt window. Now in the command prompt window type the command service.bat install <Service -Name> Here <Service-Name> can be any name that you want to assign to your Service. When you run this command you will see the following output. Check the installed Service Now, lets see if the service is created or not. For this open Services . It will give the following screen. Configure Service to Run at Startup We can make the service to run on windows startup, for this, open the Service setting by double click on the service. It will open the following screen ...