ResourceBundle.Control needsReload(String, Locale, String, ClassLoader, ResourceBundle, long) in Java

needsReload(String, Locale, String, ClassLoader, ResourceBundle, long): This method is available in java.util.ResourceBundle.Control class of Java.

Syntax:

boolean java.util.ResourceBundle.Control.needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime)

This method takes six arguments. This method determines if the expired bundle in the cache needs to be reloaded based on the loading time given by loadTime or some other criteria.

Note: The method returns true if reloading is required; false otherwise. loadTime is a millisecond offset since the CalendarEpoch.

Parameters: Six parameters are required for this method.

baseName: the base bundle name of the resource bundle, a fully qualified class name.

locale: the locale for which the resource bundle should be instantiated.

format: the resource bundle format to be loaded.

loader: the ClassLoader to use to load the bundle.

bundle: the resource bundle instance that has been expired in the cache.

loadTime: the time when the bundle was loaded and put in the cache.

Returns: true if the expired bundle needs to be reloaded; false otherwise.

Throws:

NullPointerException - if baseName, locale, format, loader, or bundle is null

Approach 1: When no exception

Java

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

public class ResourceBundleControlneedsReload {
    public static void main(String[] args) {

        ResourceBundle.Control reControl =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

        String baseName = "Hello", format = "txt";
        ClassLoader loader = new ClassLoader() {
        };
        ResourceBundle bundle =
ResourceBundle.getBundle(baseName);
        long loadTime = 19919999L;
        System.out.println(reControl.needsReload(
baseName, Locale.US, format, loader,
bundle, loadTime));

    }
}

Output:

false


Approach 2: NullPointerException 

Java

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

public class ResourceBundleControlneedsReload {
    public static void main(String[] args) {

        ResourceBundle.Control reControl =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

        String baseName = "Hello", format = null;
        ClassLoader loader = new ClassLoader() {
        };
        ResourceBundle bundle =
ResourceBundle.getBundle(baseName);
        long loadTime = 19919999L;
        System.out.println(reControl.needsReload(
baseName, Locale.US, format, loader,
bundle, loadTime));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "format" is null at java.base/java.util.ResourceBundle$Control.needsReload(ResourceBundle.java:3364)


No comments:

Post a Comment