Class Arrays.SystemSettings

java.lang.Object
net.algart.arrays.Arrays.SystemSettings
Enclosing class:
Arrays

public static class Arrays.SystemSettings extends Object

A set of static methods for getting some important global settings, stored in system properties and used for customizing modules processing AlgART arrays.

All settings returned by this class are determined (loaded from the system properties) while initializing Arrays class and cannot be modified after this moment.

This class cannot be instantiated.

  • Field Details

    • MAX_AVAILABLE_PROCESSORS

      public static final int MAX_AVAILABLE_PROCESSORS
      The maximal number of processors, that are allowed for simultaneous usage by AlgART libraries. See availableProcessors().

      This value is determined while initializing Arrays class from the system property "net.algart.arrays.maxAvailableProcessors" by the call System.getProperty("net.algart.arrays.maxAvailableProcessors","256") (on 64-bit Java machines) or System.getProperty("net.algart.arrays.maxAvailableProcessors","8") (on 32-bit Java machines). If this property contains zero or a negative integer, it is supposed to be 1, that means disabling any attempts of multiprocessor optimization. If this property contains an integer greater than the limit 1024, this limit is used instead. If some exception occurs while calling System.getProperty or if it is not an interger, it will contain the default value 256/8 (on 64/32-bit Java machines correspondingly).

      32-bit and 64-bit Java are distinguished via isJava32() method. Please remember that the result of that method is not 100% robust.

      See Also:
    • CPU_COUNT_PROPERTY_NAME

      public static final String CPU_COUNT_PROPERTY_NAME
      The name of system property ("net.algart.arrays.CPUCount"), returned by cpuCountProperty() method.
      See Also:
    • MIN_OPTIMIZATION_JAVA_MEMORY

      public static final int MIN_OPTIMIZATION_JAVA_MEMORY
      The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode: this optimization uses a buffer in RAM (SimpleMemoryModel's array), the size of which in bytes is the maximum from this value and maxTempJavaMemory(). This value can be used for similar needs in another modules.

      The default value of this constant is 524288 (512 KB) and can be customized via (undocumented) system properties.

    • BLOCK_OPTIMIZATION_FOR_COORDINATE_TRANSFORMATION

      public static final boolean BLOCK_OPTIMIZATION_FOR_COORDINATE_TRANSFORMATION
      (Internal) The flag enabling optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode. Should be true always; false value is useful for debugging only.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

    • BLOCK_OPTIMIZATION_FOR_RESIZING_WHOLE_MATRIX

      public static final boolean BLOCK_OPTIMIZATION_FOR_RESIZING_WHOLE_MATRIX
      (Internal) The flag enabling optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode in a case of resizing whole matrix, created by Matrices.asResized(Matrices.ResizingMethod, Matrix, long...) method. Should be true always; false value is useful for debugging only.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

    • BLOCK_OPTIMIZATION_FOR_RESIZING_TRANSFORMATION

      public static final boolean BLOCK_OPTIMIZATION_FOR_RESIZING_TRANSFORMATION
      (Internal) The flag enabling optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode in a case of resizing coordinate transformation. Should be true always; false value is useful for debugging only.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

    • BLOCK_OPTIMIZATION_FOR_AFFINE_TRANSFORMATION

      public static final boolean BLOCK_OPTIMIZATION_FOR_AFFINE_TRANSFORMATION
      (Internal) The flag enabling optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode in a case of affine coordinate transformation. Should be true always; false value is useful for debugging only.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

    • BLOCK_OPTIMIZATION_FOR_PROJECTIVE_TRANSFORMATION

      public static final boolean BLOCK_OPTIMIZATION_FOR_PROJECTIVE_TRANSFORMATION
      (Internal) The flag enabling optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode in a case of projective coordinate transformation. Should be true always; false value is useful for debugging only.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

    • MIN_USED_PART_FOR_PRELOADING_WHOLE_MATRIX_WITH_BLOCK_OPTIMIZATION

      public static final double MIN_USED_PART_FOR_PRELOADING_WHOLE_MATRIX_WITH_BLOCK_OPTIMIZATION
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode: this optimization tries to preload whole original matrix in RAM (if it is little enough) only if the resulting matrix depends on the specified part of the original one. In other case (for example, if the resulting matrix is fully defined by a little fragment of the original one) we use the common algorithms of splitting the matrix into layers or tiles.

      The value of this constant is 0.7 (70%).

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
    • MIN_USED_PART_FOR_PRELOADING_WHOLE_MATRIX_WITHOUT_BLOCK_OPTIMIZATION

      public static final double MIN_USED_PART_FOR_PRELOADING_WHOLE_MATRIX_WITHOUT_BLOCK_OPTIMIZATION
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "strict" mode: this optimization tries to preload whole original matrix in RAM (if it is little enough) only if the resulting matrix depends on the specified part of the original one. In other case (for example, if the resulting matrix is fully defined by a very little fragment of the original one) we do not optimize copying: even slow access to the original matrix elements (when it is created by LargeMemoryModel) is better idea than downloading extra elements in RAM.

      The value of this constant is 0.01 (1%). In other words, if more than 99% of the original matrix is not used, we prefer to use slow access to elements than to download >99% unused elements.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
    • MIN_NON_OPTIMIZED_SIMPLE_COORDINATE_COMPRESSION

      public static final double MIN_NON_OPTIMIZED_SIMPLE_COORDINATE_COMPRESSION
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode: layer optimization is not used in a case of simple compressing resizing without averaging, if total compression is stronger than this value. In this situation, even slow access to the original matrix elements (when it is created by LargeMemoryModel) is better idea than downloading extra elements in RAM.

      The value of this constant is 200.0 (times).

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
    • MIN_LAYER_USED_PART_FOR_LAYER_OPTIMIZATION

      public static final double MIN_LAYER_USED_PART_FOR_LAYER_OPTIMIZATION
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode, the case of resizing coordinate transformation: layer optimization is used only if the resulting matrix depends on the specified part of every layer. In other case, we prefer to use tiling algorithm, which does not load extra parts of the matrix.

      The value of this constant is 0.7 (70%).

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
    • MIN_NON_OPTIMIZED_COMPRESSION_FOR_TILING

      public static final double MIN_NON_OPTIMIZED_COMPRESSION_FOR_TILING
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode, the case of affine coordinate transformation: tile optimization is not used in a case of a common affine transformation without averaging, if it is a compression, stronger than this value. In this situation, even slow access to the original matrix elements (when it is created by LargeMemoryModel) is better idea than downloading extra elements in RAM.

      The value of this constant is 100.0 (times).

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
    • MIN_OPTIMIZATION_RESULT_TILE_VOLUME

      public static final int MIN_OPTIMIZATION_RESULT_TILE_VOLUME
      (Internal) The control parameter for optimization of Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) method in "non-strict" mode, the case of projective coordinate transformation: recursive splitting algorithm does not try to split a tile, if the total number of its elements is less than this value. For very small matrices it is much faster to calculate all elements directly, than to perform a complex recursive algorithm.

      The value of this constant is 100 (elements). Must be positive.

      Please do not use this constant in your code: it may be deleted or renamed in future versions.

      See Also:
  • Method Details

    • availableProcessors

      public static int availableProcessors()
      The number of processor units, that are allowed for simultaneous usage by AlgART libraries: the minimum from Runtime.getRuntime().availableProcessors() value and the MAX_AVAILABLE_PROCESSORS constant. The value of that constant is read from system property "net.algart.arrays.maxAvailableProcessors" and equal to 256 by default (or 8 on 32-bit JVM). See comments to that constant for more details.

      This method is used by AlgART libraries in all situation, where it is necessary to know to actual number of processor units, in particular, in cpuCount() method, in DefaultThreadPoolFactory class while detecting the size of the global thread pool and in DefaultDataFileModel class while calculation of the recommended number of banks. We also recommend you to use this method instead of direct Runtime.getRuntime().availableProcessors() call. It is very helpful on multiprocessor systems, containing many CPU kernels (several tens), to limit CPU usage by your application.

      Note: this method has another sense than cpuCount(). This method provides a global limitation for usage of CPU kernels in any situations, while cpuCount() usually controls the number of threads while multithreading optimization.

      Returns:
      the number of processor units among Runtime.getRuntime().availableProcessors(), allowed for usage by AlgART libraries.
      See Also:
    • cpuCountProperty

      public static int cpuCountProperty()
      Returns the recommended number of used system processors, specified by CPU_COUNT_PROPERTY_NAME system property ("net.algart.arrays.CPUCount"); the result 0 means a recommendation to use the result of availableProcessors() method.

      More precisely, if this system property exists and contains non-negative integer value, this method returns minimum from this value, Integer.getInteger(CPU_COUNT_PROPERTY_NAME), and the limit MAX_AVAILABLE_PROCESSORS. Zero value is also possible; in this case, it is supposed that availableProcessors() should be used instead. If there is no such property, or if it contains a negative integer or not a number, or if some exception occurred while calling Integer.getInteger, this method returns the default value 0 (that means, as supposed, availableProcessors() processors).

      This value is used by DefaultThreadPoolFactory class for creating thread pools, used for parallel execution of algorithms processing AlgART arrays. You can use this property for analogous goals.

      Please note: unlike most other system properties, used by this package, the "net.algart.arrays.CPUCount" property is loaded at the moment of calling this method. So, the classes using this method, like DefaultThreadPoolFactory, always use the actual value of this property, that can be customized in the application without restarting JVM.

      Returns:
      the supposed number of processors, specified by CPU_COUNT_PROPERTY_NAME system property, or 0 if there is no such property, or MAX_AVAILABLE_PROCESSORS if this property contains an integer greater than this limit. The value 0 means a recommendation to use availableProcessors().
      See Also:
    • cpuCount

      public static int cpuCount()
      Returns the recommended number of used system processors as the result of cpuCountProperty() method, if it is a positive number, or the result of availableProcessors(), if it is zero. This value is typically used for multiprocessing optimization: see DefaultThreadPoolFactory.
      Returns:
      the recommended number of used processors.
      See Also:
    • globalMemoryModel

      public static MemoryModel globalMemoryModel()
      Returns the global memory model that is recommended to use in the application for creating large AlgART arrays.

      The global memory model is determined while initializing Arrays class from the system property "net.algart.arrays.globalMemoryModel". This property must contain the full name of the Java class implementing MemoryModel interface. The class must have a public constructor without arguments or, as a variant, static getInstance() method without arguments returning an instance of this memory model. This constructor or method will be used for creating the global memory model instance.

      If there is not such system property, the SimpleMemoryModel will be used as the global model.

      The following standard aliases are allowed for the class name in "net.algart.arrays.globalMemoryModel" system property:

      Important: the global memory model must always support all primitive types! (All 3 standard models, "simple", "buffer" and "large", do support all primitive types.) If it is not true for a model specified in "net.algart.arrays.globalMemoryModel" system property, or in a case of any problem while reading the system property or while instantiation, the corresponded message will be logged by the standard Java logger (Logger.getLogger(Arrays.class.getName()) with SEVERE level, and the SimpleMemoryModel will be used as the global model.

      Note: the global memory model may not support non-primitive element types! You may use globalMemoryModel(Class) method to choose appropriate memory model.

      If you are sure that your AlgART arrays will not be too large, you may ignore this method and use the SimpleMemoryModel. It is the most quick model for small arrays, but may be inefficient for very large arrays (100-500 MB or more) and does not allow work with arrays greater than 2 GB.

      Returns:
      the global memory model recommended for creating large AlgART arrays.
      See Also:
    • globalMemoryModel

      public static MemoryModel globalMemoryModel(Class<?> elementType)
      Returns global memory model, if it supports the passed type of array elements, or SimpleMemoryModel instance in other case.
      Parameters:
      elementType - the type of array elements.
      Returns:
      the global memory model if it supports this type, SimpleMemoryModel otherwise.
      Throws:
      NullPointerException - if the argument is null.
      See Also:
    • maxTempJavaMemory

      public static long maxTempJavaMemory()
      Specifies the maximal amount of usual Java memory, in bytes, which may be freely used by methods, processing AlgART arrays, for internal needs and for creating results. In other words, if the size of the resulting AlgART array or matrix, or some temporary set of arrays or matrices, is not greater than this limit, then the method may (though not must) use SimpleMemoryModel for creating such AlgART arrays (matrices) or may allocate usual Java arrays instead of AlgART arrays. For allocating greater amount of memory, all methods should use, when possible, some more advanced and controlled memory model, for example, globalMemoryModel().

      This value is determined while initializing Arrays class from the system property "net.algart.arrays.maxTempJavaMemory" by the call Long.getLong("net.algart.arrays.maxTempJavaMemory",33554432) (32 MB). If this property contains a negative integer, it is supposed to be zero, that means that this limit will be ignored and this optimization ability should not be used. If this property contains an integer greater than the limit 256~7.2*1016, this limit is used instead: it helps to guarantee that using this value will not lead to integer overflow. If some exception occurs while calling Long.getLong, it will contain the default 33554432 value.

      Warning: if you specify too large value for this limit in "net.algart.arrays.maxTempJavaMemory" system property, it can lead to unexpected OutOfMemoryError in some methods, if the amount of available Java memory is not enough.

      Returns:
      the value of "net.algart.arrays.maxTempJavaMemory" system property, 33554432 by default.
      See Also:
    • maxTempJavaMemoryForTiling

      public static long maxTempJavaMemoryForTiling()
      Analog of maxTempJavaMemory() value for a special case: conversion AlgART matrices into the tiled form and inversely from the tiled form, i.e. for copying a usual matrix to the matrix created by the Matrix.tile(long...) method and, inversely, for copying a result of Matrix.tile(long...) method into a usual matrix. If a method of this package, such as Matrices.copy(ArrayContext, Matrix, Matrix), detects that one from two matrices is tiled and it should be copied into/from another usual (untiled) matrix, and that the tiled matrix is not created by a very quick memory model alike SimpleMemoryModel, then the method allocates a buffer in the Java heap space up to maxTempJavaMemoryForTiling() bytes (more precisely, up to availableTempJavaMemoryForTiling() bytes) and use it for reducing swapping while tiling/untiling.

      Usually this limit is chosen greater than maxTempJavaMemory(), because conversion matrices into/from the tiled form strongly needs RAM for quick processing and can extremely slow down if there is not enough memory to download the minimal converted block into RAM.

      This value is determined while initializing Arrays class from the system property "net.algart.arrays.maxTempJavaMemoryForTiling" by the call Long.getLong("net.algart.arrays.maxTempJavaMemoryForTiling", Math.max(134217728, maxTempJavaMemory())) (134217728=128 MB). If this property contains a negative integer, it is supposed to be zero, that means that this limit will be ignored and this optimization ability should not be used. If this property contains an integer greater than the limit 256~7.2*1016, this limit is used instead: it helps to guarantee that using this value will not lead to integer overflow. If some exception occurs while calling Long.getLong, it will contain the default value: maximum from 134217728 and maxTempJavaMemory().

      Note: if this limit is greater than the amount of available Java memory, then tiling/untiling methods use only available amount (depending on "-Xmx" JVM argument) and don't try to allocate more than ~80% of available free Java heap space: see availableTempJavaMemoryForTiling(). So, large values of this limit should probably not lead to unexpected OutOfMemoryError.

      Returns:
      the value of "net.algart.arrays.maxTempJavaMemoryForTiling" system property, max(134217728,maxTempJavaMemory()) by default.
    • availableTempJavaMemoryForTiling

      public static long availableTempJavaMemoryForTiling()
      Returns the minimum from maxTempJavaMemoryForTiling() and 80% of the amount of memory, that can be probably allocated in Java heap without the risk of OutOfMemoryError. This value is used for estimating the temporary work memory by tiling/untiling methods of this package: see maxTempJavaMemoryForTiling().
      Returns:
      minimum from maxTempJavaMemoryForTiling() and 80% of the amount of memory, that can be allocated in Java heap at this moment.
    • maxMultithreadingMemory

      public static long maxMultithreadingMemory()
      Specifies the maximal size of memory block, in bytes, that should be processed in several threads for optimization on multiprocessor or multi-core computers.

      Many algorithms, in particular, based on Arrays.copy(ArrayContext, UpdatableArray, Array) method and Arrays.ParallelExecutor class, use multithreading processing for more optimal usage of available processors. But parallel processing works fine only for limited amount of memory. Multithreading processing very large array, for example, larger than available RAM, leads to a lot of simultaneous disk accesses and usually extremely slows down calculations. For AlgART arrays, created by LargeMemoryModel, the good practice is multithreading processing a sequential range of an array, not greater than one bank.

      This constant is used for calculating length of sequential subarrays (ranges), processed in several threads by Arrays.ParallelExecutor class and methods based on that class. See Arrays.ParallelExecutor.recommendedNumberOfRanges(Array, boolean) method for more details. You also may use this constant in your code for similar needs.

      This value is determined while initializing Arrays class from the system property "net.algart.arrays.maxMultithreadingMemory" by the call Long.getLong("net.algart.arrays.maxMultithreadingMemory",1048576) (1 MB). If this property contains negative integer or a value less than 256, it is supposed to be 256 (bytes): the minimal allowed amount of memory for multithreading. If this property contains an integer greater than the limit 256~7.2*1016, this limit is used instead: it helps to guarantee that using this value will not lead to integer overflow. If some exception occurs while calling Long.getLong, it will contain the default 1048576 value.

      Returns:
      the maximal size of memory block, in bytes, that should be processed in several threads for optimization on multiprocessor or multi-core computers, 1048576 by default.
    • globalDiskSynchronizer

      public static Arrays.SystemSettings.DiskSynchronizer globalDiskSynchronizer()
      Returns the global disk synchronizer that is recommended to use in the application for any disk I/O operations.

      The global disk synchronizer is determined while initializing Arrays class from the system property "net.algart.arrays.globalDiskSynchronizer". This property must contain the full name of the Java class implementing Arrays.SystemSettings.DiskSynchronizer interface. The class must have a public constructor without arguments or, as a variant, static getInstance() method without arguments returning an instance of this disk synchronizer. This constructor or method will be used for creating the global disk synchronizer instance.

      If there is not such system property, the default implementation of Arrays.SystemSettings.DiskSynchronizer interface will be used, that performs simple global synchronization of all operations. See comments to this interface for more details about the default implementation.

      In a case of any problem while reading the system property or while instantiation of the required class, the corresponded message will be logged by the standard Java logger (Logger.getLogger(Arrays.class.getName()) with SEVERE level, and the default implementation of Arrays.SystemSettings.DiskSynchronizer interface will be used.

      Returns:
      the current synchronizer of all disk operations.
    • isJava32

      public static boolean isJava32()
      Returns true on 32-bit Java machines, false on 64-bit ones (or probably better JVM in the future: for example, 128-bit). It is used by DefaultDataFileModel.recommendedBankSize(boolean) and DefaultDataFileModel.recommendedSingleMappingLimit() methods and while determining the value of MAX_AVAILABLE_PROCESSORS property.

      Warning: this method does not provide 100% guaranteed result, because the current Java specification doesn't publish robust methods for distinguishing 32-bit and 64-bit Java. So, you cannot be absolutely sure that the system is 64-bit (or better) if this method returns false.

      This method checks System.getProperty("sun.arch.data.model"). If this property exists, can be read without exceptions and contains "32" or "64" substring, this method returns true if it contains "32" and false if it contains "64". In other case (for example, in Java applets, which have no permissions to read this property), this method makes a decision on the base of System.getProperty("os.arch"). If it cannot make a decision (for example, due to some exception in the last getProperty call), it returns false by default. All this analyse is performed only once while initializing while initializing Arrays class.

      Returns:
      true on 32-bit Java machines.
      See Also:
    • profilingMode

      public static boolean profilingMode()
      If true, the algorithms, processing AlgART arrays, should write to logs some timing information with the level CONFIG or higher (in terms of java.util.logging). In particular, Arrays.copy(ArrayContext, UpdatableArray, Array) and Arrays.copy(ArrayContext, UpdatableArray, Array, int) methods log the time of copying in this case.

      This value is determined while initializing Arrays class from the system property "net.algart.arrays.profiling", that can contain "true" or "false" value. If there is no such property, this method returns the default value, which is identical to "-ea" JVM flag: if java was called with "-ea" flag (assertions are enabled), the default profiling mode is true, if it was called without this flag (assertions are disabled), the default profiling mode is false.

      Returns:
      the value of "net.algart.arrays.profiling" system property, identical to "-ea" flag by default.
    • parseIntWithMetricalSuffixes

      public static int parseIntWithMetricalSuffixes(String s)
      An analog of Integer.parseInt that "understands" suffixes K, M, G, that mean "Kilo" (1024), "Mega" (1048576) and "Giga" (1073741824). The suffixes can be written both in upper and lower case. For example, for "3k" string this method returns 3*1024=3072, for "1M" it returns 1048576, for "1783" it returns 1783.

      More precisely, if the specified string ends with "K", "k", "M", "m", "G" or "g", then this method calls Integer.parseInt(s.substring(0,s.length()-1)) and multiplies the result by 1024 (210), 1048576 (220) or 1073741824 (230); in a case of overflow while multiplying, NumberFormatException is thrown. If the specified string does not end with "K", "k", "M", "m", "G" or "g", then this method is equivalent to Integer.parseInt(s).

      This method is convenient while analysing some system properties or another settings, usually describing amounts of memory.

      Parameters:
      s - a string containing the int representation to be parsed, maybe with K, M, G suffixes.
      Returns:
      the integer value represented by the argument in decimal.
      Throws:
      NumberFormatException - if the string does not contain a parsable integer.
    • parseLongWithMetricalSuffixes

      public static long parseLongWithMetricalSuffixes(String s)
      An analog of Long.parseLong that "understands" suffixes K, M, G, T, that mean "Kilo" (1024), "Mega" (1048576), "Giga" (1073741824) and "Tera" (1099511627776). The suffixes can be written both in upper and lower case. For example, for "3k" string this method returns 3*1024=3072, for "1M" it returns 1048576, for "1783" it returns 1783.

      More precisely, if the specified string ends with "K", "k", "M", "m", "G", "g", "T" or "t", then this method calls Long.parseLong(s.substring(0,s.length()-1)) and multiplies the result by 1024 (210), 1048576 (220), 1073741824 (230) or 1099511627776 (240); in a case of overflow while multiplying, NumberFormatException is thrown. If the specified string does not end with "K", "k", "M", "m", "G", "g", "T" or "t", then this method is equivalent to Long.parseLong(s).

      This method is convenient while analysing some system properties or another settings, usually describing amounts of memory.

      Parameters:
      s - a string containing the long representation to be parsed, maybe with K, M, G, T suffixes.
      Returns:
      the long integer value represented by the argument in decimal.
      Throws:
      NumberFormatException - if the string does not contain a parsable integer.
    • getStringProperty

      public static String getStringProperty(String propertyName, String defaultValue)
      An analog of System.getProperty, but returns defaultValue even in a case of exception.

      More precisely, this method does the following:

       try {
           return System.getProperty(propertyName, defaultValue);
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException, this method just returns defaultValue. In particular, this method returns defaultValue if propertyName is null or an empty string (System.getProperty throws exceptions in these cases). There is a guarantee that this method never throws exceptions.
      Parameters:
      propertyName - the name of the property.
      defaultValue - default value; null is an allowed value.
      Returns:
      the value of the string property or the default value in a case of any problems.
    • getStringEnv

      public static String getStringEnv(String envVarName, String defaultValue)
      An analog of System.getenv, but returns defaultValue in a case, when System.getenv returns null or throws any exception.

      More precisely, this method does the following:

       try {
           String s = System.getenv(envVarName);
           return s != null ? s : defaultValue;
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException, this method just returns defaultValue. In particular, this method returns defaultValue if propertyName is null (System.getenv throws exceptions in these cases). There is a guarantee that this method never throws exceptions.
      Parameters:
      envVarName - the name of the environment variable.
      defaultValue - default value; null is an allowed value.
      Returns:
      the value of the environment variable or the default value in a case of any problems.
    • getIntProperty

      public static int getIntProperty(String propertyName, int defaultValue)
      An analog of Integer.getInteger, but "understands" suffixes K, M, G, that mean "Kilo" (1024), "Mega" (1048576) and "Giga" (1073741824) and returns defaultValue in a case of any exception (including security exception).

      More precisely, this method does the following:

       try {
           return parseIntWithMetricalSuffixes(System.getProperty(
               propertyName, String.valueOf(defaultValue)));
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException and NumberFormatException, this method just returns defaultValue. There is a guarantee that this method never throws exceptions.
      Parameters:
      propertyName - the name of the property.
      defaultValue - default value.
      Returns:
      the value of the integer property or the default value in a case of any problems.
    • getIntEnv

      public static int getIntEnv(String envVarName, int defaultValue)
      An analog of getIntProperty(String, int), which reads the number from the environment variable instead of the system property.

      More precisely, this method does the following:

       try {
           String s = System.getenv(envVarName);
           return s == null ? defaultValue : parseIntWithMetricalSuffixes(s);
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException and NumberFormatException, this method just returns defaultValue. There is a guarantee that this method never throws exceptions.
      Parameters:
      envVarName - the name of the environment variable.
      defaultValue - default value.
      Returns:
      the value of the integer environment variable or the default value in a case of any problems.
    • getLongProperty

      public static long getLongProperty(String propertyName, long defaultValue)
      An analog of Long.getLong, but "understands" suffixes K, M, G, T, that mean "Kilo" (1024), "Mega" (1048576), "Giga" (1073741824) and "Tera" (1099511627776) and returns defaultValue in a case of any exception (including security exception).

      More precisely, this method does the following:

       try {
           return parseLongWithMetricalSuffixes(System.getProperty(
               propertyName, String.valueOf(defaultValue)));
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException and NumberFormatException, this method just returns defaultValue. There is a guarantee that this method never throws exceptions.
      Parameters:
      propertyName - the name of the property.
      defaultValue - default value.
      Returns:
      the value of the long-integer property or the default value in a case of any problems.
    • getLongEnv

      public static long getLongEnv(String envVarName, long defaultValue)
      An analog of getLongProperty(String, long), which reads the number from the environment variable instead of the system property.

      More precisely, this method does the following:

       try {
           String s = System.getenv(envVarName);
           return s == null ? defaultValue : parseLongWithMetricalSuffixes(s);
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException and NumberFormatException, this method just returns defaultValue. There is a guarantee that this method never throws exceptions.
      Parameters:
      envVarName - the name of the environment variable.
      defaultValue - default value.
      Returns:
      the value of the long-integer environment variable or the default value in a case of any problems.
    • getBooleanProperty

      public static boolean getBooleanProperty(String propertyName, boolean defaultValue)
      An analog of Boolean.getBoolean, but returns defaultValue if there is no required property, or if it doesn't equal "true" or "false", or in a case of any exception. (The test of this string is case insensitive.)

      Unlike Boolean.getBoolean, this method catches all exceptions, including SecurityException, and returns defaultValue in a case of an exception. There is a guarantee that this method never throws exceptions.

      Parameters:
      propertyName - the name of property.
      defaultValue - default value.
      Returns:
      the value of the boolean property or the default value in a case of any problems.
    • getBooleanEnv

      public static boolean getBooleanEnv(String envVarName, boolean defaultValue)
      An analog of getBooleanProperty(String, boolean), which tests the environment variable instead of the system property.

      More precisely, this method does the following:

       try {
           String s = System.getenv(envVarName);
           return s == null ? defaultValue
               : s.equalsIgnoreCase("true") ? true
               : s.equalsIgnoreCase("false") ? false
               : defaultValue;
       } catch (Exception ex) {
           return defaultValue;
       }
       
      So, in a case of any exception, including SecurityException and NumberFormatException, this method just returns defaultValue. There is a guarantee that this method never throws exceptions.
      Parameters:
      envVarName - the name of the environment variable.
      defaultValue - default value.
      Returns:
      the value of the environment variable or the default value in a case of any problems.