Wednesday, April 22, 2020

Blockchain Myths

Blockchain is not Bitcoin

Blockchain is a technology that allows peer-to-peer transactions to be recorded on a distributed ledger across the network. These transactions are stored in blocks and each block is linked to the previous one, creating a chain.

Bitcoin is a cryptocurrency that makes electronic payment possible directly between two people without going through a third party like a bank. Bitcoin was the first blockchian application which uses the blockchain as underlying technology to use it as a database ledger.


Blockchain is not a cryptocurrency

Blockchain is the platform which brings cryptocurrencies into play, it's mainly a database which holds transactions records and forms the network. This network creates the means for transacting and enables transferring of value and information.

Cryptocurrencies are the token used within these networks to send value and pay for these transactions. You can see them as a tool on blockchain.


Blockchain, Bitcoin and Distributed Ledgers are not the same thing

Let's clarify this point!

Bitcoin is a cryptocurrency that uses blockchain technology.
Blockchain is a type of distributed ledger.
Not all distributed ledgers are effectively blockchains.
(A distributed ledger is a database that is consensually shared and synchronized across multiple sites, institutions or geographies. It allows transactions to have public "witnesses", thereby making a cyberattack more difficult.)


Blockchain doesn't exist on the cloud

Many believe blockchain is a database that exists on the cloud. The truth is that Blockchain is a ledger distributed across a number of members computers.


Blockchain is not a product

Blockchain itself is not an app or a product.
Many Dapps(decentralized applications) are being based on blockchain but the blcokchain itself is not that application or a thing.
Blockchain's utility comes from an appropriate set of applications built on top of it.


All transactions using blockchain are not necessarily anonymous

Just because cryptos transactions allow a degree of anonymity to its users doesn't mean that all blockchain transactions work in the same manner.
Truth is, several blockchain networks adhere to KYC/AML regulations and offer great transparency.


Blockchain is not used only for powering cryptocurrencies

Cryptos and banking in general are not the only industry that could be transformed by blockchain technology.
Banking is just the beginning.
Blockchain can be used for fields as diverse as music to healthcare, to voting, to supply chain management...


There is not a single blockchain system for everything

Blockchain networks can take many forms.
It could be private, public or there are consortium blockchain networks(Consortium blockchain is a system that is 'semi-private' and has a controlled user group, but works across different organizations).


Blockchain records can be hacked or altered?

Blockchain is not invulnerable to outside attacks. No system or database is completely secure, but the larger and more distributed the network, the more secure it is believed to be.
Blockchain can provide to applications that are developed on top of them a way of catching unauthorised changes to records.

Thursday, April 16, 2020

Spring Annotations

Spring framework implements and promotes the principle of control inversion (IOC) or dependency injection (DI) and is in fact an IOC container.
Traditionally, Spring allows a developer to manage bean dependencies by using XML-based configuration.
There is an alternative way to define beans and their dependencies. This method is a Java-based configuration.
Unlike the XML approach, Java-based configuration allows you to manage bean components programmatically. That’s why Spring annotations were introduced.
This is a compilation of most commonly used Spring Annotations.

********Spring Framework annotations********

@ComponentScan - make Spring scan the package for the @Configuration classes.
@Configuration - mark a class as a source of bean definitions.
@Bean - indicates that a method produces a bean to be managed by the Spring container.
@Component - turns the class into a Spring bean at the auto-scan time.
@Service - specialization of the @Component, has no encapsulated state.
@Autowired - Spring’s dependency injection wires an appropriate bean into the marked class member.
@Lazy - makes @Bean or @Component be initialized on demand rather than eagerly.
@Qualifier - filters what beans should be used to @Autowire a field or parameter.
@Value - indicates a default value expression for the field or parameter, typically something like
“#{systemProperties.myProp}”
@Required - fail the configuration, if the dependency cannot be injected.



********Spring Boot and Web annotations********

@SpringBootApplication - uses @Configuration, @EnableAutoConfiguration and @ComponentScan.
@EnableAutoConfiguration - make Spring guess the configuration based on the classpath.
@Controller - marks the class as web controller, capable of handling the requests.
@RestController - a convenience annotation of a @Controller and @ResponseBody.
@ResponseBody - makes Spring bind method’s return value to the web response body.
@RequestMapping - specify on the method in the controller, to map a HTTP request to the URL to this method. @RequestMapping variants ---> @GetMapping
                                                                                                         @PostMapping
                                                                                                         @PutMapping
                                                                                                         @PatchMapping
                                                                                                         @DeleteMapping
@RequestParam - bind HTTP parameters into method arguments.
@PathVariable - binds placeholder from the URI to the method parameter.



********Spring Cloud annotations******** 

@EnableConfigServer - turns your application into a server other apps can get their configuration from.
Use spring.application.cloud.config.uri in the client @SpringBootApplication to point to the config server.
@EnableEurekaServer - makes your app an Eureka discovery service, other apps can locate services through it.
@EnableDiscoveryClient - makes your app register in the service discovery server and discover other services through it.
@EnableCircuitBreaker - configures Hystrix circuit breaker protocols.
@HystrixCommand(fallbackMethod = “fallbackMethodName”) - marks methods to fall back
to another method if they cannot succeed normally.

I have listed here most of the important annotations, but there are a lot more of them for specific tasks. If you are not clear with these quick definitions, you can find more detailed information on each of these annotations at https://dzone.com/articles/a-guide-to-spring-framework-annotations.

Friday, April 10, 2020

Streams API in Java 8 – Intermediate Operations

In this blog, lets learn to perform intermediate operations in Streams.You can understand about streams and learn to create streams in Part-I of the Streams API in Java 8 series. Now, let us learn the intermediate operations with examples.
filter()
sorted()
map()
limit()
flatMap()
concat()

filter()
filter() is an intermediate operation. The syntax is filter(Predicate predicate).It takes Predicate as parameter. You can use lambda or method references to implement Predicate.
The result of filter is a stream. so, a terminal operation is needed to get the result back. To start with we will use forEach() to get the result.
forEach() is an intermediate operation which takes Consumer as parameter.Let us use method references inside forEach() method.
Example:
Code snippet for filter() methodJava
List<String> list = Arrays.asList("Java","Maven","CSS","JUnit","HTML","JSP");
// convert list into stream
// filter the list based on the condition
// call forEach that is a terminal operation
list.stream()
.filter((str)->str.length()>3)
.forEach(System.out::println);
Output:
Java
Maven
JUnit
HTML

sorted()
sorted() is an intermediate operation. It returns a stream consisting of the elements, sorted according to natural order. It has one overloaded method that takes Comparator as parameter. The syntax is sort(Comparator compare).You can use lambda or method references to implement Comparator.
This can be used in case of a collection of objects. Here also we will use forEach() for terminal operation. I am using method chaining by adding methods
Example:
Code snippet for sorted() methodJava
// convert list into stream
// filter the list based on a condition
// call sorted on the stream
// call forEach that is a terminal operation
Arrays.asList("Java","Maven","CSS","JUnit","HTML","JSP")
.stream()
.filter((str)->str.length()>3)
.sorted()
.forEach(System.out::println);
Output:
HTML
JUnit
Java
Maven

map()
map() is an intermediate operation. It returns a stream that is a result of applying a function to all the elements in the stream.The syntax is map(Function mapper).You can use lambda or method references to implement Function. So it can be either map(String::toUpperCase) or map(str->str.toUpperCase()).
In case of String Stream pass the method of String inside map, in case of Integer Stream , pass the functionality of Integer inside map.
Here also we will use forEach() for terminal operation.
Example:
Code snippet for map() methodJava
// convert list into stream
// filter the list based on a condition
// call sorted on the stream
// convert each element to uppercase - uses method reference
// call forEach that is a terminal operation
Arrays.asList("Java","Maven","CSS","JUnit","HTML","JSP")
.stream()
.filter((str)->str.length()>3)
.sorted()
.map(String::toUpperCase)
.forEach(System.out::println);
// For integer Stream
Stream.of(new Integer[] {10,20,40})
.map(num->num.doubleValue())
.forEach(System.out::println);
Output:
10.0
20.0
40.0

limit()
limit() is an intermediate operation. It returns a stream that limits the size of the stream to the given length.The syntax is limit(int length). It throws IllegalArgumentException in case of negative length.
Here also we will use forEach() for terminal operation.
Example:
Code snippet for limit() methodJava
Arrays.asList("Java","Maven","CSS","JUnit","HTML","JSP")
.stream()
.filter((str)->str.length()>3)
.sorted()
.map(String::toUpperCase)
.limit(3)
.forEach(System.out::println);
output:
HTML
JAVA
JUNIT

skip()
skip() is an intermediate operation. It returns a stream after discarding the first n elements of the stream.If the stream has less elements than n, then an empty stream will be returned.The syntax is skip(int length).
Here also we will use forEach() for terminal operation.
Example:
code snippet for skip() methodJava
Arrays.asList("Java","Maven","CSS","JUnit","HTML","JSP")
.stream()
.map(x->x.toUpperCase())
.sorted()
.skip(2)
.forEach(System.out::println);
output:
JAVA
JSP
JUNIT
MAVEN

flatMap()
flatMap() is an intermediate operation. Flatmap is used when the stream itself is of type List, Set or an Array, that is Stream<List<String>>or Stream<Set<String>> or Stream<String[]> or Stream<List<Object>>.
It is used to convert a Stream of Collection or array into a Stream of single elements of that particular datatype. It returns a stream after applying the provided mapping function to each element. The syntax is flatMap(Function mapper).
using flatMap() methodJava
Stream<String[]>      -> flatMap -> Stream<String>
Stream<Set<String>>   -> flatMap -> Stream<String>
Stream<List<String>>  -> flatMap -> Stream<String>
Stream<List<Object>>  -> flatMap ->     Stream<Object>
Stream<String[]>      -> flatMap -> Stream<String>
Stream<Set<String>>   -> flatMap -> Stream<String>
Stream<List<String>>  -> flatMap -> Stream<String>
Stream<List<Object>>  -> flatMap ->     Stream<Object>
Example:
code snippet for flatMap() methodJava
String[][] arrTwo = new String[][]
{ { "Rama", "Janani","Rohan" },
{ "Akash", "Rahul", "Kumaran" } };
//converting a TwoD array to stream of oneDimensional array
Stream<String[]> onestream = Arrays.stream(arrTwo);
//stream of oneDimensional array to Stream of String elements
Stream<String> strStream =
onestream.flatMap((row)->Arrays.stream(row))
.filter((x)->x.startsWith("R"));
strStream.forEach(System.out::println);
// As method chaining
String[][] arrTwo = new String[][]
{ { "Rama", "Janani","Rohan" },{ "Akash", "Rahul", "Kumaran" } };
Arrays.stream(arrTwo)
.flatMap((row)->Arrays.stream(row))
.filter((x)->x.startsWith("R"))
.forEach(System.out::println);
output:
Rama
Rohan
Rahul
                String[][] arrTwo = new String[][]
 { { "Rama", "Janani","Rohan" },
   { "Akash", "Rahul", "Kumaran" } };
 //converting a TwoD array to stream of oneDimensional array
 Stream<String[]> onestream = Arrays.stream(arrTwo);
 //stream of oneDimensional array to Stream of String elements
 Stream<String> strStream =
 onestream.flatMap((row)->Arrays.stream(row))
 .filter((x)->x.startsWith("R"));
 strStream.forEach(System.out::println);
// As method chaining
String[][] arrTwo = new String[][]
 { { "Rama", "Janani","Rohan" },{ "Akash", "Rahul", "Kumaran" } };
Arrays.stream(arrTwo)
      .flatMap((row)->Arrays.stream(row))
      .filter((x)->x.startsWith("R"))
      .forEach(System.out::println);
Output:
Rama
Rohan
Rahul

concat()
concat() is an intermediate operation. It returns a concatenated stream with all the elements of the first stream followed by all the elements of the second stream. The syntax is stream(Stream a, Stream b).
Here also we will use forEach() for terminal operation.
Example:
Code snippet for concat() methodJava
Stream.concat(Stream.of("Apple","Orange"),Stream.of("Kiwi","Papaya"))
.forEach(System.out::println);
output:
Apple
Orange
Kiwi
Papaya

Your Quantum Reality

Your reality is created from within yourself. Everything you see on the outside in your life is a reflection of your thoughts, beliefs an...