Skip to content

Instantly share code, notes, and snippets.

View viveknaskar's full-sized avatar
🎯
Focusing

Vivek Naskar viveknaskar

🎯
Focusing
View GitHub Profile
@viveknaskar
viveknaskar / OAuthNotes.txt
Last active February 24, 2018 09:27
OAuth
OAuth is a standard that applications (and the developers who love them) can use to provide client applications with “secure delegated access”. OAuth works over HTTP and authorizes Devices, APIs, Servers and Applications with access tokens rather than credentials, which we will go over in depth below.
There are two versions of OAuth: OAuth 1.0a and OAuth2. These specifications are completely different from one another, and cannot be used together: there is no backwards compatibility between them.
Which one is more popular? Great question! Nowadays (at this time of writing), OAuth2 is no doubt the most widely used form of OAuth. So from now on, whenever I write just “OAuth”, I’m actually talking about OAuth2 — as it is most likely what you’ll be using.
@viveknaskar
viveknaskar / objectdeepcopy.js
Created April 17, 2018 09:37 — forked from izy521/objectdeepcopy.js
`JSON.parse( JSON.stringify( obj) )` has been regarded as the fastest method for deep copying Objects, but is it? This is mainly just to test. Obviously Functions aren't allowed in JSON.
var Types = new Map();
Types.set(Array, function(v) {
var l = v.length; i = 0, a = Array(l);
for (i; i<l; i++) {
a[i] = v[i];
}
return a;
});
Types.set(Number, function(v) {
return v * 1;
@viveknaskar
viveknaskar / lodash sortBy method
Created August 9, 2018 13:23
The javascript function to sort JSON objects
var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];
 
_.sortBy(users, [function(o) { return o.user; }]);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
 
@viveknaskar
viveknaskar / CollectionFrameworkBasics.md
Last active September 12, 2020 06:33
Basics of Collection Framework in a Nutshell

Collection framework

Collection framework is a set of classes and interfaces. It is referred as a collection, but it is used as a library for resusable collection data structures. Both arrays and collections function similarly as they both hold references to the objects and work as a group but the primary difference is that collections do not need to be assigned capacity when they are instantiated as collections can grow and shrink automatically as object are added or removed.

Types of collection

There are basically three types of collection: sets, lists and maps.

Sample code on how @Mock and @InjectMocks works.
Say we have Game and Player class.
class Game {
private Player player;
public Game(Player player) {
this.player = player;
@viveknaskar
viveknaskar / SpringAnnotations.md
Last active September 12, 2020 06:26
Explanation of the spring annotations

Spring Annotations

@RestController

This annotation eliminates the need of annotating each method with @ResponseBody. Under the hood, @RestController is itself annotated with @ResponseBody, and can be considered as combination of @Controller and @ResponseBody.

@RequestBody

If a method parameter is annotated with @RequestBody, Spring will bind the incoming HTTP request body(for the URL mentioned in @RequestMapping for that method) to that parameter. While doing that, Spring will [behind the scenes]

@viveknaskar
viveknaskar / http-get-http-post.java
Last active September 12, 2020 06:35
HTTP POST and HTTP GET using RestTemplate
// For GET request
if (marks != null && roll != null && !roll.isEmpty()) {
RestTemplate restTemplate = new RestTemplate();
String url = PropsUtil.get("some.server") + "/mark/v1/" + roll;
ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
String data = result.getBody();
if (data != null) {
JSONObject json = JSONFactoryUtil.createJSONObject(data);
@viveknaskar
viveknaskar / Q&A.md
Last active September 12, 2020 06:21
Added bunch of questions with their answers

How many ways you can create an object in java?

  1. Using new keyword → constructor get called Employee emp1 = new Employee();

  2. Using newInstance() method of Class → constructor get called Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance(); It can also be written as Employee emp2 = Employee.class.newInstance();

@viveknaskar
viveknaskar / bootstrap-modal-on-form-submit.markdown
Created August 14, 2019 21:13
Bootstrap Modal on Form Submit
@viveknaskar
viveknaskar / index.html
Last active August 16, 2019 10:04
Adding Modal Pop-up on button Submit
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>