পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, January 16, 2017

Guava - Overview

Guava is an open source, Java-based library and contains many core libraries of Google, which are being used in many of their projects. It facilitates the best coding practices and helps reduce coding errors. It provides utility methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and validations.

Benefits of Guava

  • Standardized - The Guava library is managed by Google.
  • Efficient - It is a reliable, fast and efficient extension to the java standard library.
  • Optimized - The library is highly optimized.
  • Functional Programming - It adds functional processing capability to Java.
  • Utilities - It provides many utility classes which are regularly required in programming application development.
  • Validation - It provides a standard failsafe validation mechanism.
  • Best Practices - It emphasizes on best practices.
Consider the following code snippet.
public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();
      
      Integer a =  null;
      Integer b =  new Integer(10);
      
      System.out.println(guavaTester.sum(a,b));
   }

   public Integer sum(Integer a, Integer b){
      return a + b;
   } 
}
Run the program to get the following result.
Exception in thread "main" java.lang.NullPointerException
 at GuavaTester.sum(GuavaTester.java:13)
 at GuavaTester.main(GuavaTester.java:9)
Following are the problems with the code.
  • sum() is not taking care of any of the parameters to be passed as null.
  • caller function is also not worried about passing a null to the sum() method accidently.
  • When the program runs, NullPointerException occurs.
In order to avoid the above problems, null check is to be made in each and every place where such problems are present.
Let's see the use of Optional, a Guava provided Utility class to solve the above problems in a standardized way.
import com.google.common.base.Optional;

public class GuavaTester {
   public static void main(String args[]){
      GuavaTester guavaTester = new GuavaTester();

      Integer invalidInput = null;

      Optional<Integer> a =  Optional.of(invalidInput);
      Optional<Integer> b =  Optional.of(new Integer(10));
      
      System.out.println(guavaTester.sum(a,b));      
   }

   public Integer sum(Optional<Integer> a, Optional<Integer> b){
      return a.get() + b.get();
   } 
}
Run the program to get the following result.
Exception in thread "main" java.lang.NullPointerException
 at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
 at com.google.common.base.Optional.of(Optional.java:85)
 at GuavaTester.main(GuavaTester.java:8)
Let's understand the important concepts of the above program.
  • Optional - A utility class, to make the code use the null properly.
  • Optional.of - It returns the instance of Optional class to be used as a parameter. It checks passed, not to be 'null'.
  • Optional.get - It gets the value of the input stored in the Optional class.
Using the Optional class, you can check whether caller method is passing a proper parameter or not.

No comments:

Post a Comment