Class Entry

java.lang.Object
org.apache.juneau.config.Entry

public class Entry extends Object
A single entry in a Config file.
  • Constructor Details

    • Entry

      protected Entry(Config config, ConfigMap configMap, String sectionName, String entryName)
      Constructor.
      Parameters:
      config - The config that this entry belongs to.
      configMap - The map that this belongs to.
      sectionName - The section name of this entry.
      entryName - The name of this entry.
  • Method Details

    • isPresent

      public boolean isPresent()
      Returns true if this entry exists in the config.
      Returns:
      true if this entry exists in the config.
    • isNotEmpty

      public boolean isNotEmpty()
      Returns true if this entry exists in the config and is not empty.
      Returns:
      true if this entry exists in the config and is not empty.
    • get

      public String get()
      Returns this entry as a string.
      Returns:
      true if this entry exists in the config and is not empty.
      Throws:
      NullPointerException - if value was null.
    • orElse

      public String orElse(String def)
      Returns this entry converted to the specified type or returns the default value.

      This is equivalent to calling as(def.getClass()).orElse(def) but is simpler and avoids the creation of an Optional object.

      Parameters:
      def - The default value to return if value does not exist.
      Returns:
      This entry converted to the specified type or returns the default value.
    • orElseGet

      Returns this entry converted to the specified type or returns the default value.

      This is equivalent to calling as(def.getClass()).orElse(def) but is simpler and avoids the creation of an Optional object.

      Parameters:
      def - The default value to return if value does not exist.
      Returns:
      This entry converted to the specified type or returns the default value.
    • as

      public <T> Optional<T> as(Class<T> type)
      Returns this entry converted to the specified type.
      Type Parameters:
      T - The type to convert the value to.
      Parameters:
      type - The type to convert the value to.
      Returns:
      This entry converted to the specified type.
    • as

      public <T> Optional<T> as(Type type, Type... args)
      Returns this entry converted to the specified value.

      The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).

      Examples:

      Config config = Config.create().name("MyConfig.cfg").build(); // Parse into a linked-list of strings. List list = config.get("MySection/myListOfStrings").to(LinkedList.class, String.class); // Parse into a linked-list of beans. List list = config.get("MySection/myListOfBeans").to(LinkedList.class, MyBean.class); // Parse into a linked-list of linked-lists of strings. List list = config.get("MySection/my2dListOfStrings").to(LinkedList.class, LinkedList.class, String.class); // Parse into a map of string keys/values. Map map = config.get("MySection/myMap").to(TreeMap.class, String.class, String.class); // Parse into a map containing string keys and values of lists containing beans. Map map = config.get("MySection/myMapOfListsOfBeans").to(TreeMap.class, String.class, List.class, MyBean.class);

      Collection classes are assumed to be followed by zero or one objects indicating the element type.

      Map classes are assumed to be followed by zero or two meta objects indicating the key and value types.

      The array can be arbitrarily long to indicate arbitrarily complex data structures.

      Notes:
      • Use the as(Class) method instead if you don't need a parameterized map/collection.
      Type Parameters:
      T - The object type to create.
      Parameters:
      type - The object type to create.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      args - The type arguments of the class if it's a collection or map.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      Ignored if the main type is not a map or collection.
      Returns:
      The value, or Optional.empty() if the section or key does not exist.
    • as

      public <T> Optional<T> as(Parser parser, Type type, Type... args)
      Same as as(Type, Type...) but specifies the parser to use to parse the entry.
      Type Parameters:
      T - The object type to create.
      Parameters:
      parser - The parser to use to parse the entry.
      type - The object type to create.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      args - The type arguments of the class if it's a collection or map.
      Can be any of the following: ClassMeta, Class, ParameterizedType, GenericArrayType
      Ignored if the main type is not a map or collection.
      Returns:
      The value, or Optional.empty() if the section or key does not exist.
    • as

      public <T> Optional<T> as(Parser parser, Class<T> type)
      Returns this entry converted to the specified type.
      Type Parameters:
      T - The type to convert the value to.
      Parameters:
      parser - The parser to use to parse the entry value.
      type - The type to convert the value to.
      Returns:
      This entry converted to the specified type, or Optional.empty() if the entry does not exist.
    • toString

      public String toString()
      Returns this entry as a string.
      Overrides:
      toString in class Object
      Returns:
      This entry as a string, or null if the entry does not exist.
    • asString

      Returns this entry as a string.
      Returns:
      This entry as a string, or Optional.empty() if the entry does not exist.
    • asStringArray

      Returns this entry as a string array.

      If the value exists, splits the value on commas and returns the values as trimmed strings.

      Returns:
      This entry as a string array, or Optional.empty() if the entry does not exist.
    • asInteger

      Returns this entry as an integer.

      "K", "M", and "G" can be used to identify kilo, mega, and giga in base 2.
      "k", "m", and "g" can be used to identify kilo, mega, and giga in base 10.

      Example:
      • "100K" -> 1024000
      • "100M" -> 104857600
      • "100k" -> 1000000
      • "100m" -> 100000000

      Uses Integer.decode(String) underneath, so any of the following integer formats are supported:

      • "0x..."
      • "0X..."
      • "#..."
      • "0..."
      Returns:
      The value, or Optional.empty() if the value does not exist or the value is empty.
    • asBoolean

      Returns this entry as a parsed boolean.

      Uses Boolean.parseBoolean(String) to parse value.

      Returns:
      The value, or Optional.empty() if the value does not exist or the value is empty.
    • asLong

      public Optional<Long> asLong()
      Returns this entry as a long.

      "K", "M", "G", "T", and "P" can be used to identify kilo, mega, giga, tera, and penta in base 2.
      "k", "m", "g", "t", and "p" can be used to identify kilo, mega, giga, tera, and p in base 10.

      Example:
      • "100K" -> 1024000
      • "100M" -> 104857600
      • "100k" -> 1000000
      • "100m" -> 100000000

      Uses Long.decode(String) underneath, so any of the following integer formats are supported:

      • "0x..."
      • "0X..."
      • "#..."
      • "0..."
      Returns:
      The value, or Optional.empty() if the value does not exist or the value is empty.
    • asDouble

      Returns this entry as a double.

      Uses Double.valueOf(String) underneath, so any of the following number formats are supported:

      • "0x..."
      • "0X..."
      • "#..."
      • "0..."
      Returns:
      The value, or Optional.empty() if the value does not exist or the value is empty.
    • asFloat

      public Optional<Float> asFloat()
      Returns this entry as a float.

      Uses Float.valueOf(String) underneath, so any of the following number formats are supported:

      • "0x..."
      • "0X..."
      • "#..."
      • "0..."
      Returns:
      The value, or Optional.empty() if the value does not exist or the value is empty.
    • asBytes

      public Optional<byte[]> asBytes()
      Returns this entry as a byte array.

      Byte arrays are stored as encoded strings, typically BASE64, but dependent on the Config.Builder.binaryFormat(BinaryFormat) setting.

      Returns:
      The value, or Optional.empty() if the section or key does not exist.
    • asMap

      Returns this entry as a parsed map.

      Uses the parser registered on the Config to parse the entry.

      If the parser is a JSON parser, the starting/trailing "{"/"}" in the value are optional.

      Returns:
      The value, or Optional.empty() if the section or key does not exist.
      Throws:
      ParseException - If value could not be parsed.
    • asMap

      public Optional<JsonMap> asMap(Parser parser) throws ParseException
      Returns this entry as a parsed map.

      If the parser is a JSON parser, the starting/trailing "{"/"}" in the value are optional.

      Parameters:
      parser - The parser to use to parse the value, or Optional.empty() to use the parser defined on the config.
      Returns:
      The value, or null if the section or key does not exist.
      Throws:
      ParseException - If value could not be parsed.
    • asList

      Returns this entry as a parsed list.

      Uses the parser registered on the Config to parse the entry.

      If the parser is a JSON parser, the starting/trailing "["/"]" in the value are optional.

      Returns:
      The value, or Optional.empty() if the section or key does not exist.
      Throws:
      ParseException - If value could not be parsed.
    • asList

      public Optional<JsonList> asList(Parser parser) throws ParseException
      Returns this entry as a parsed list.

      If the parser is a JSON parser, the starting/trailing "["/"]" in the value are optional.

      Parameters:
      parser - The parser to use to parse the value, or Optional.empty() to use the parser defined on the config.
      Returns:
      The value, or Optional.empty() if the section or key does not exist.
      Throws:
      ParseException - If value could not be parsed.
    • getKey

      public String getKey()
      Returns the name of this entry.
      Returns:
      The name of this entry.
    • getValue

      public String getValue()
      Returns the raw value of this entry.
      Returns:
      The raw value of this entry.
    • getComment

      public String getComment()
      Returns the same-line comment of this entry.
      Returns:
      The same-line comment of this entry.
    • getPreLines

      public List<String> getPreLines()
      Returns the pre-lines of this entry.
      Returns:
      The pre-lines of this entry as an unmodifiable list.
    • getModifiers

      public String getModifiers()
      Returns the modifiers for this entry.
      Returns:
      The modifiers for this entry, or null if it has no modifiers.