background preloader


Facebook Twitter

JsonParser (JSR 353 (JSON Processing) API 1.0-SNAPSHOT API) JDHT: Java Distributed Hash Table or Java DKS Hash Table. JGroups - The JGroups Project. Javolution. Why NULL is Bad? A simple example of NULL usage in Java: What is wrong with this method?

Why NULL is Bad?

It may return NULL instead of an object — that’s what is wrong. NULL is a terrible practice in an object-oriented paradigm and should be avoided at all costs. There have been a number of opinions about this published already, including Null References, The Billion Dollar Mistake presentation by Tony Hoare and the entire Object Thinking book by David West. Here, I’ll try to summarize all the arguments and show examples of how NULL usage can be avoided and replaced with proper object-oriented constructs. Basically, there are two possible alternatives to NULL. The first one is Null Object design pattern (the best way is to make it a constant): The second possible alternative is to fail fast by throwing an Exception when you can’t return an object: Now, let’s see the arguments against NULL. Ad-hoc Error Handling Every time you get an object as an input you must check whether it is NULL or a valid object reference.

Java Method Logging with AOP and Annotations. Sometimes, I want to log (through slf4j and log4j) every execution of a method, seeing what arguments it receives, what it returns and how much time every execution takes.

Java Method Logging with AOP and Annotations

This is how I’m doing it, with help of AspectJ, jcabi-aspects and Java 6 annotations: This is what I see in log4j output: Nice, isn’t it? Now, let’s see how it works. Annotation with Runtime Retention Annotations is a technique introduced in Java 6. Date/Time Formatting/Parsing, Java 8 Style. Since nearly the beginning of Java, Java developers have worked with dates and times via the java.util.Date class (since JDK 1.0) and then the java.util.Calendar class (since JDK 1.1).

Date/Time Formatting/Parsing, Java 8 Style

During this time, hundreds of thousands (or maybe millions) of Java developers have formatted and parsed Java dates and times using java.text.DateFormat and java.text.SimpleDateFormat. Given how frequently this has been done over the years, it’s no surprise that there are numerous online examples of and tutorials on parsing and formatting dates and times with these classes. The classic Java Tutorials cover these java.util and java.text classes in the Formatting lesson (Dates and Times). Exceptions Hierarchy and Custom Exceptions in Java. Exceptions are a vital part of every modern programming language, as it is a way to handle error cases and unexpected behavior, without necessarily terminating the whole application.

Exceptions Hierarchy and Custom Exceptions in Java

Except for that, by handling the exceptions in the correct way, we can get meaningful feedback regarding what went wrong, why and where. As you can understand, it is very important for every Java programmer to have a good grasp of how Exceptions work. In this post, we are going to take a look at the exception hierarchy, and explain the different kinds of exceptions that exist, the connections between each other and the different ways to approach them. 1. Exception hierarchy. When the Java 8 Streams API is not Enough. Java 8 was – as always – a release of compromises and backwards-compatibility.

When the Java 8 Streams API is not Enough

A release where the JSR-335 expert group might not have agreed upon scope or feasibility of certain features with some of the audience. See some concrete explanations by Brian Goetz about why … But today we’re going to focus on the Streams API’s “short-comings”, or as Brian Goetz would probably put it: things out of scope given the design goals. Scala and Java 8 type inference in higher order functions sample. One of the concepts mentioned in the Functional Programming in Scala is about the type inference in higher order functions in Scala and how it fails in certain situations and a workaround for the same.

Scala and Java 8 type inference in higher order functions sample

So consider a sample higher order function, purely for demonstration: Ideally, passing in a list of say integers, you would expect the predicate function to not require an explicit type: Type inference does not work in this specific instance however, the fix is to specify the type explicitly: Or a better fix is to use currying, then the type inference works! I was curious whether Java 8 type inference has this issue and tried a similar sample with Java 8 Lambda expression, the following is an equivalent filter function: Getters/Setters. Evil. Period. There is an old debate, started in 2003 by Allen Holub in this Why getter and setter methods are evil famous article, about whether getters/setters is an anti-pattern and should be avoided or if it is something we inevitably need in object-oriented programming.

Getters/Setters. Evil. Period.

I’ll try to add my two cents to this discussion. The gist of the following text is this: getters and setters is a terrible practice and those who use it can’t be excused. Again, to avoid any misunderstanding, I’m not saying that get/set should be avoided when possible. No. I’m saying that you should never have them near your code. Arrogant enough to catch your attention? This is the Final Discussion! Pun intended… Let’s discuss Java final.

This is the Final Discussion!

Recently, our popular blog post “10 Subtle Best Practices when Coding Java” had a significant revival and a new set of comments as it was summarised and linked from JavaWorld. In particular, the JavaWorld editors challenged our opinion about the Java keyword “final“: More controversially, Eder takes on the question of whether it’s ever safe to make methods final by default:“If you’re in full control of all source code, there’s absolutely nothing wrong with making methods final by default, because:”“If you do need to override a method (do you really?)

, you can still remove the final keyword”“You will never accidentally override any method anymore” Yes, indeed. Lambdas and side effects. Overview Java 8 has added features such as lambdas and type inference.

lambdas and side effects

This makes the language less verbose and cleaner, however it comes with more side effects as you don’t have to be as explicit in what you are doing. The return type of a lambda matters. 3 Examples of Parsing HTML File in Java using Jsoup. HTML is the core of the web, all the pages you see on the internet are based on HTML, whether they are dynamically generated by JavaScript, JSP, PHP, ASP or any other web technology.

3 Examples of Parsing HTML File in Java using Jsoup

Your browser actually parse HTMLs and render it for you. But what do you do, if you need to parse an HTML document and find some elements, tags, attributes or check if a particular element exists or not, all that using a Java program. If you have been in Java programming for some years, I am sure you have done some XML parsing work using parsers like DOM and SAX. Ironically, there are few instances when you need to parse HTML document from a core Java application, which doesn’t include Servlet and other Java web technologies. From Java 7 Futures to Akka actors with Scala. Garbage Collection: increasing the throughput. The inspiration for this post came after stumbling upon “Pig in the Python” definition in the memory management glossary. Apparently, this term is used to explain the situation where GC repeatedly promotes large objects from generation to generation.

The effect of doing so is supposedly similar to that of a python swallowing its prey in whole only to become immobilised during digestion. For the next 24 hours I just could not get the picture of choking pythons out of my head. As the psychiatrists say, the best way to let go of your fears is to speak about them. So here we go. Garbage Collection pauses are well known by their potential of becoming a performance bottleneck. Property-based testing with Spock. Property based testing is an alternative approach to testing, complementing example based testing. The latter is what we’ve been doing all our lives: exercising production code against “examples” – inputs we think are representative. Picking these examples is an art on its own: “ordinary” inputs, edge cases, malformed inputs, etc.

But why are we limiting ourselves to just few examples? Why not test hundreds, millions… ALL inputs? Processing Java Annotations Using Reflection. In my previous article covering Java Annotations, I outlined a recent use case and provided you with some examples of custom annotations and how they might be used. In this article, I’m going to take that a step further and give you a few examples of custom annotations and how you would process these custom annotations using the Java Reflection API. Once you have gone through this tutorial, you should come away with a better understanding of the simplicity and flexibility that custom annotations can provide. So let’s dig into the code! Custom Annotation Listings I have created three different annotations for the example code today which are the DoItLikeThis, DoItLikeThat and DoItWithAWhiffleBallBat annotations.

DoItLikeThis Annotation The DoItLikeThis annotation is targeted for the ElementType TYPE, which makes it only available for Java types. How Nashorn Impacts API Evolution on a New Level. Java EE 7 with Angular JS – CRUD, REST, Validations – Part 2. This is the promised follow up to the Java EE 7 with Angular JS – Part 1. It took longer than I expect (to find the time to prepare the code and blog post), but it’s finally here! The Application. Java RunnableFuture Example. Joining Strings in JDK 8. RE2/J: Linear-time regular-expression matching for Java. Today we’re announcing the public release of RE2/J: a pure-Java implementation of the popular RE2 regular expression library.

Although RE2/J is not always faster than java.util.regexp, its running time is always linear in the size of the input. Thus when matching larger inputs, especially against patterns containing a high degree of alternation, RE2/J may be dramatically faster. Spring Rest API with Swagger – Creating documentation. The real key to making your REST API easy to use is good documentation. Custom Reason Phrase in HTTP status error message response with JAX-RS (Jersey) In some of my recent work I got the request to produce a custom Reason Phrase in the HTTP status response delivered to one of our REST API consuming clients when an error occurs.

Beyond Thread Pools: Java Concurrency is Not as Bad as You Think. Apache Hadoop, Apache Spark, Akka, Java 8 streams and Quasar: The classic use cases to the newest concurrency approaches for Java developers. Using Google Guava Cache for local caching. Lot of times we would have to fetch the data from a database or another webservice or load it from file system. In cases where it involves a network call there would be inherent network latencies, network bandwidth limitations. One of the approaches to overcome this is to have a cache local to the application. If your application spans across multiple nodes then the cache will be local to each node causing inherent data inconsistency. Top 10 Most Common Java Performance Problems. Using Groovy for Bash (shell) Operations. Recently I needed to create a groovy script that deletes some directories in a Linux machine. Java.util.concurrent.Phaser Example. In this example, we shall be demonstrating the java.util.concurrent.Phaser Class.

Java8 Sorting – Performance Pitfall. Java 8 brings all the goodness of lambdas to enable us to program using a declarative style. Disambiguating between instances with Google Guice. Thread.tss?thread_id=80365&utm_content=buffer7791c&utm_medium=social&utm_source=twitter. The Optimum Method to Concatenate Strings in Java. Fork/Join Framework vs. Parallel Streams vs. ExecutorService: The Ultimate Fork/Join Benchmark. Fast Conservative Garbage Collection. Java 8 pitfall - Beware of Files.lines()