Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Java Utility Libraries and Custom Tools

Tech May 7 3

Apache Commons

Apache Commons provides a comprehensive set of utility classes for common programming tasks.

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;

public class CommonsDemo {
    
    public static void validateStrings() {
        String empty = "";
        String nullStr = null;
        String whitespace = " ";
        
        System.out.println("Empty check: " + StringUtils.isEmpty(empty));
        System.out.println("Null check: " + StringUtils.isEmpty(nullStr));
        System.out.println("Whitespace check: " + StringUtils.isEmpty(whitespace));
        
        String numeric = "123";
        String mixed = "12a3";
        System.out.println("Numeric validation: " + StringUtils.isNumeric(numeric));
        System.out.println("Mixed validation: " + StringUtils.isNumeric(mixed));
    }
    
    public static void generateRandomData() {
        System.out.println("Random numbers: " + RandomStringUtils.randomNumeric(8));
        System.out.println("Random letters: " + RandomStringUtils.randomAlphabetic(8));
    }
    
    public static void demonstrateBag() {
        Bag<String> colorBag = new HashBag<>();
        colorBag.add("red", 3);
        colorBag.add("blue", 2);
        colorBag.add("green");
        
        System.out.println("Red count: " + colorBag.getCount("red"));
        System.out.println("Total items: " + colorBag.size());
    }
}

Google Guava

Guava offers powerful collection utilities and functional programming features.

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.*;

public class GuavaExamples {
    
    public static void immutableCollections() {
        ImmutableList<String> colors = ImmutableList.of("red", "green", "blue");
        ImmutableMap<Integer, String> numberMap = ImmutableMap.of(1, "one", 2, "two");
        
        System.out.println("Immutable list: " + colors);
        System.out.println("Immutable map: " + numberMap);
    }
    
    public static void multiValueMap() {
        Multimap<String, String> phoneBook = ArrayListMultimap.create();
        phoneBook.put("Alice", "123-4567");
        phoneBook.put("Alice", "987-6543");
        phoneBook.put("Bob", "555-1234");
        
        System.out.println("Alice's phones: " + phoneBook.get("Alice"));
    }
    
    public static void stringOperations() {
        String joined = Joiner.on("; ").join("apple", "banana", "cherry");
        Iterable<String> split = Splitter.on(',').trimResults().split("one, two , three");
        
        System.out.println("Joined: " + joined);
        split.forEach(System.out::println);
    }
}

Joda-Time

Joda-Time provides robust date and time manipulation capabilities.

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeHandling {
    
    public static void dateOperations() {
        DateTime current = new DateTime();
        DateTime future = current.plusMonths(3);
        
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
        
        System.out.println("Current: " + formatter.print(current));
        System.out.println("Future: " + formatter.print(future));
        System.out.println("Day of week: " + current.dayOfWeek().getAsText());
    }
    
    public static void timeZoneHandling() {
        DateTime utc = new DateTime().withZone(DateTimeZone.UTC);
        DateTime london = new DateTime().withZone(DateTimeZone.forID("Europe/London"));
        
        System.out.println("UTC time: " + utc);
        System.out.println("London time: " + london);
    }
}

Custom Utility Class

A comprehensive utility class coevring common programming needs.

import java.util.*;
import java.util.regex.Pattern;

public final class ApplicationUtils {
    private static final Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
    
    public static boolean isBlank(String input) {
        return input == null || input.trim().isEmpty();
    }
    
    public static boolean isNumeric(String input) {
        return !isBlank(input) && DIGIT_PATTERN.matcher(input).matches();
    }
    
    public static String generateRandomCode(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        
        for (int i = 0; i < length; i++) {
            builder.append(characters.charAt(random.nextInt(characters.length())));
        }
        return builder.toString();
    }
    
    public static <T> boolean hasElements(Collection<T> collection) {
        return collection != null && !collection.isEmpty();
    }
    
    public static String reverseString(String original) {
        if (isBlank(original)) return original;
        return new StringBuilder(original).reverse().toString();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.