001/** 002 * Copyright (C) 2011-2012 Typesafe Inc. <http://typesafe.com> 003 */ 004package com.typesafe.config; 005 006import java.time.Duration; 007import java.time.Period; 008import java.time.temporal.TemporalAmount; 009import java.util.List; 010import java.util.Map; 011import java.util.Set; 012import java.util.concurrent.TimeUnit; 013 014/** 015 * An immutable map from config paths to config values. Paths are dot-separated 016 * expressions such as <code>foo.bar.baz</code>. Values are as in JSON 017 * (booleans, strings, numbers, lists, or objects), represented by 018 * {@link ConfigValue} instances. Values accessed through the 019 * <code>Config</code> interface are never null. 020 * 021 * <p> 022 * {@code Config} is an immutable object and thus safe to use from multiple 023 * threads. There's never a need for "defensive copies." 024 * 025 * <p> 026 * Fundamental operations on a {@code Config} include getting configuration 027 * values, <em>resolving</em> substitutions with {@link Config#resolve()}, and 028 * merging configs using {@link Config#withFallback(ConfigMergeable)}. 029 * 030 * <p> 031 * All operations return a new immutable {@code Config} rather than modifying 032 * the original instance. 033 * 034 * <p> 035 * <strong>Examples</strong> 036 * 037 * <p> 038 * You can find an example app and library <a 039 * href="https://github.com/lightbend/config/tree/main/examples">on 040 * GitHub</a>. Also be sure to read the <a 041 * href="package-summary.html#package_description">package overview</a> which 042 * describes the big picture as shown in those examples. 043 * 044 * <p> 045 * <strong>Paths, keys, and Config vs. ConfigObject</strong> 046 * 047 * <p> 048 * <code>Config</code> is a view onto a tree of {@link ConfigObject}; the 049 * corresponding object tree can be found through {@link Config#root()}. 050 * <code>ConfigObject</code> is a map from config <em>keys</em>, rather than 051 * paths, to config values. Think of <code>ConfigObject</code> as a JSON object 052 * and <code>Config</code> as a configuration API. 053 * 054 * <p> 055 * The API tries to consistently use the terms "key" and "path." A key is a key 056 * in a JSON object; it's just a string that's the key in a map. A "path" is a 057 * parseable expression with a syntax and it refers to a series of keys. Path 058 * expressions are described in the <a 059 * href="https://github.com/lightbend/config/blob/main/HOCON.md">spec for 060 * Human-Optimized Config Object Notation</a>. In brief, a path is 061 * period-separated so "a.b.c" looks for key c in object b in object a in the 062 * root object. Sometimes double quotes are needed around special characters in 063 * path expressions. 064 * 065 * <p> 066 * The API for a {@code Config} is in terms of path expressions, while the API 067 * for a {@code ConfigObject} is in terms of keys. Conceptually, {@code Config} 068 * is a one-level map from <em>paths</em> to values, while a 069 * {@code ConfigObject} is a tree of nested maps from <em>keys</em> to values. 070 * 071 * <p> 072 * Use {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath} to convert 073 * between path expressions and individual path elements (keys). 074 * 075 * <p> 076 * Another difference between {@code Config} and {@code ConfigObject} is that 077 * conceptually, {@code ConfigValue}s with a {@link ConfigValue#valueType() 078 * valueType()} of {@link ConfigValueType#NULL NULL} exist in a 079 * {@code ConfigObject}, while a {@code Config} treats null values as if they 080 * were missing. (With the exception of two methods: {@link Config#hasPathOrNull} 081 * and {@link Config#getIsNull} let you detect <code>null</code> values.) 082 * 083 * <p> 084 * <strong>Getting configuration values</strong> 085 * 086 * <p> 087 * The "getters" on a {@code Config} all work in the same way. They never return 088 * null, nor do they return a {@code ConfigValue} with 089 * {@link ConfigValue#valueType() valueType()} of {@link ConfigValueType#NULL 090 * NULL}. Instead, they throw {@link ConfigException.Missing} if the value is 091 * completely absent or set to null. If the value is set to null, a subtype of 092 * {@code ConfigException.Missing} called {@link ConfigException.Null} will be 093 * thrown. {@link ConfigException.WrongType} will be thrown anytime you ask for 094 * a type and the value has an incompatible type. Reasonable type conversions 095 * are performed for you though. 096 * 097 * <p> 098 * <strong>Iteration</strong> 099 * 100 * <p> 101 * If you want to iterate over the contents of a {@code Config}, you can get its 102 * {@code ConfigObject} with {@link #root()}, and then iterate over the 103 * {@code ConfigObject} (which implements <code>java.util.Map</code>). Or, you 104 * can use {@link #entrySet()} which recurses the object tree for you and builds 105 * up a <code>Set</code> of all path-value pairs where the value is not null. 106 * 107 * <p> 108 * <strong>Resolving substitutions</strong> 109 * 110 * <p> 111 * <em>Substitutions</em> are the <code>${foo.bar}</code> syntax in config 112 * files, described in the <a href= 113 * "https://github.com/lightbend/config/blob/main/HOCON.md#substitutions" 114 * >specification</a>. Resolving substitutions replaces these references with real 115 * values. 116 * 117 * <p> 118 * Before using a {@code Config} it's necessary to call {@link Config#resolve()} 119 * to handle substitutions (though {@link ConfigFactory#load()} and similar 120 * methods will do the resolve for you already). 121 * 122 * <p> 123 * <strong>Merging</strong> 124 * 125 * <p> 126 * The full <code>Config</code> for your application can be constructed using 127 * the associative operation {@link Config#withFallback(ConfigMergeable)}. If 128 * you use {@link ConfigFactory#load()} (recommended), it merges system 129 * properties over the top of <code>application.conf</code> over the top of 130 * <code>reference.conf</code>, using <code>withFallback</code>. You can add in 131 * additional sources of configuration in the same way (usually, custom layers 132 * should go either just above or just below <code>application.conf</code>, 133 * keeping <code>reference.conf</code> at the bottom and system properties at 134 * the top). 135 * 136 * <p> 137 * <strong>Serialization</strong> 138 * 139 * <p> 140 * Convert a <code>Config</code> to a JSON or HOCON string by calling 141 * {@link ConfigObject#render()} on the root object, 142 * <code>myConfig.root().render()</code>. There's also a variant 143 * {@link ConfigObject#render(ConfigRenderOptions)} which allows you to control 144 * the format of the rendered string. (See {@link ConfigRenderOptions}.) Note 145 * that <code>Config</code> does not remember the formatting of the original 146 * file, so if you load, modify, and re-save a config file, it will be 147 * substantially reformatted. 148 * 149 * <p> 150 * As an alternative to {@link ConfigObject#render()}, the 151 * <code>toString()</code> method produces a debug-output-oriented 152 * representation (which is not valid JSON). 153 * 154 * <p> 155 * Java serialization is supported as well for <code>Config</code> and all 156 * subtypes of <code>ConfigValue</code>. 157 * 158 * <p> 159 * <strong>This is an interface but don't implement it yourself</strong> 160 * 161 * <p> 162 * <em>Do not implement {@code Config}</em>; it should only be implemented by 163 * the config library. Arbitrary implementations will not work because the 164 * library internals assume a specific concrete implementation. Also, this 165 * interface is likely to grow new methods over time, so third-party 166 * implementations will break. 167 */ 168public interface Config extends ConfigMergeable { 169 /** 170 * Gets the {@code Config} as a tree of {@link ConfigObject}. This is a 171 * constant-time operation (it is not proportional to the number of values 172 * in the {@code Config}). 173 * 174 * @return the root object in the configuration 175 */ 176 ConfigObject root(); 177 178 /** 179 * Gets the origin of the {@code Config}, which may be a file, or a file 180 * with a line number, or just a descriptive phrase. 181 * 182 * @return the origin of the {@code Config} for use in error messages 183 */ 184 ConfigOrigin origin(); 185 186 @Override 187 Config withFallback(ConfigMergeable other); 188 189 /** 190 * Returns a replacement config with all substitutions (the 191 * <code>${foo.bar}</code> syntax, see <a 192 * href="https://github.com/lightbend/config/blob/main/HOCON.md">the 193 * spec</a>) resolved. Substitutions are looked up using this 194 * <code>Config</code> as the root object, that is, a substitution 195 * <code>${foo.bar}</code> will be replaced with the result of 196 * <code>getValue("foo.bar")</code>. 197 * 198 * <p> 199 * This method uses {@link ConfigResolveOptions#defaults()}, there is 200 * another variant {@link Config#resolve(ConfigResolveOptions)} which lets 201 * you specify non-default options. 202 * 203 * <p> 204 * A given {@link Config} must be resolved before using it to retrieve 205 * config values, but ideally should be resolved one time for your entire 206 * stack of fallbacks (see {@link Config#withFallback}). Otherwise, some 207 * substitutions that could have resolved with all fallbacks available may 208 * not resolve, which will be potentially confusing for your application's 209 * users. 210 * 211 * <p> 212 * <code>resolve()</code> should be invoked on root config objects, rather 213 * than on a subtree (a subtree is the result of something like 214 * <code>config.getConfig("foo")</code>). The problem with 215 * <code>resolve()</code> on a subtree is that substitutions are relative to 216 * the root of the config and the subtree will have no way to get values 217 * from the root. For example, if you did 218 * <code>config.getConfig("foo").resolve()</code> on the below config file, 219 * it would not work: 220 * 221 * <pre> 222 * common-value = 10 223 * foo { 224 * whatever = ${common-value} 225 * } 226 * </pre> 227 * 228 * <p> 229 * Many methods on {@link ConfigFactory} such as 230 * {@link ConfigFactory#load()} automatically resolve the loaded 231 * <code>Config</code> on the loaded stack of config files. 232 * 233 * <p> 234 * Resolving an already-resolved config is a harmless no-op, but again, it 235 * is best to resolve an entire stack of fallbacks (such as all your config 236 * files combined) rather than resolving each one individually. 237 * 238 * @return an immutable object with substitutions resolved 239 * @throws ConfigException.UnresolvedSubstitution 240 * if any substitutions refer to nonexistent paths 241 * @throws ConfigException 242 * some other config exception if there are other problems 243 */ 244 Config resolve(); 245 246 /** 247 * Like {@link Config#resolve()} but allows you to specify non-default 248 * options. 249 * 250 * @param options 251 * resolve options 252 * @return the resolved <code>Config</code> (may be only partially resolved if options are set to allow unresolved) 253 */ 254 Config resolve(ConfigResolveOptions options); 255 256 /** 257 * Checks whether the config is completely resolved. After a successful call 258 * to {@link Config#resolve()} it will be completely resolved, but after 259 * calling {@link Config#resolve(ConfigResolveOptions)} with 260 * <code>allowUnresolved</code> set in the options, it may or may not be 261 * completely resolved. A newly-loaded config may or may not be completely 262 * resolved depending on whether there were substitutions present in the 263 * file. 264 * 265 * @return true if there are no unresolved substitutions remaining in this 266 * configuration. 267 * @since 1.2.0 268 */ 269 boolean isResolved(); 270 271 /** 272 * Like {@link Config#resolve()} except that substitution values are looked 273 * up in the given source, rather than in this instance. This is a 274 * special-purpose method which doesn't make sense to use in most cases; 275 * it's only needed if you're constructing some sort of app-specific custom 276 * approach to configuration. The more usual approach if you have a source 277 * of substitution values would be to merge that source into your config 278 * stack using {@link Config#withFallback} and then resolve. 279 * <p> 280 * Note that this method does NOT look in this instance for substitution 281 * values. If you want to do that, you could either merge this instance into 282 * your value source using {@link Config#withFallback}, or you could resolve 283 * multiple times with multiple sources (using 284 * {@link ConfigResolveOptions#setAllowUnresolved(boolean)} so the partial 285 * resolves don't fail). 286 * 287 * @param source 288 * configuration to pull values from 289 * @return an immutable object with substitutions resolved 290 * @throws ConfigException.UnresolvedSubstitution 291 * if any substitutions refer to paths which are not in the 292 * source 293 * @throws ConfigException 294 * some other config exception if there are other problems 295 * @since 1.2.0 296 */ 297 Config resolveWith(Config source); 298 299 /** 300 * Like {@link Config#resolveWith(Config)} but allows you to specify 301 * non-default options. 302 * 303 * @param source 304 * source configuration to pull values from 305 * @param options 306 * resolve options 307 * @return the resolved <code>Config</code> (may be only partially resolved 308 * if options are set to allow unresolved) 309 * @since 1.2.0 310 */ 311 Config resolveWith(Config source, ConfigResolveOptions options); 312 313 /** 314 * Validates this config against a reference config, throwing an exception 315 * if it is invalid. The purpose of this method is to "fail early" with a 316 * comprehensive list of problems; in general, anything this method can find 317 * would be detected later when trying to use the config, but it's often 318 * more user-friendly to fail right away when loading the config. 319 * 320 * <p> 321 * Using this method is always optional, since you can "fail late" instead. 322 * 323 * <p> 324 * You must restrict validation to paths you "own" (those whose meaning are 325 * defined by your code module). If you validate globally, you may trigger 326 * errors about paths that happen to be in the config but have nothing to do 327 * with your module. It's best to allow the modules owning those paths to 328 * validate them. Also, if every module validates only its own stuff, there 329 * isn't as much redundant work being done. 330 * 331 * <p> 332 * If no paths are specified in <code>checkValid()</code>'s parameter list, 333 * validation is for the entire config. 334 * 335 * <p> 336 * If you specify paths that are not in the reference config, those paths 337 * are ignored. (There's nothing to validate.) 338 * 339 * <p> 340 * Here's what validation involves: 341 * 342 * <ul> 343 * <li>All paths found in the reference config must be present in this 344 * config or an exception will be thrown. 345 * <li> 346 * Some changes in type from the reference config to this config will cause 347 * an exception to be thrown. Not all potential type problems are detected, 348 * in particular it's assumed that strings are compatible with everything 349 * except objects and lists. This is because string types are often "really" 350 * some other type (system properties always start out as strings, or a 351 * string like "5ms" could be used with {@link #getMilliseconds}). Also, 352 * it's allowed to set any type to null or override null with any type. 353 * <li> 354 * Any unresolved substitutions in this config will cause a validation 355 * failure; both the reference config and this config should be resolved 356 * before validation. If the reference config is unresolved, it's a bug in 357 * the caller of this method. 358 * </ul> 359 * 360 * <p> 361 * If you want to allow a certain setting to have a flexible type (or 362 * otherwise want validation to be looser for some settings), you could 363 * either remove the problematic setting from the reference config provided 364 * to this method, or you could intercept the validation exception and 365 * screen out certain problems. Of course, this will only work if all other 366 * callers of this method are careful to restrict validation to their own 367 * paths, as they should be. 368 * 369 * <p> 370 * If validation fails, the thrown exception contains a list of all problems 371 * found. See {@link ConfigException.ValidationFailed#problems}. The 372 * exception's <code>getMessage()</code> will have all the problems 373 * concatenated into one huge string, as well. 374 * 375 * <p> 376 * Again, <code>checkValid()</code> can't guess every domain-specific way a 377 * setting can be invalid, so some problems may arise later when attempting 378 * to use the config. <code>checkValid()</code> is limited to reporting 379 * generic, but common, problems such as missing settings and blatant type 380 * incompatibilities. 381 * 382 * @param reference 383 * a reference configuration 384 * @param restrictToPaths 385 * only validate values underneath these paths that your code 386 * module owns and understands 387 * @throws ConfigException.ValidationFailed 388 * if there are any validation issues 389 * @throws ConfigException.NotResolved 390 * if this config is not resolved 391 * @throws ConfigException.BugOrBroken 392 * if the reference config is unresolved or caller otherwise 393 * misuses the API 394 */ 395 void checkValid(Config reference, String... restrictToPaths); 396 397 /** 398 * Checks whether a value is present and non-null at the given path. This 399 * differs in two ways from {@code Map.containsKey()} as implemented by 400 * {@link ConfigObject}: it looks for a path expression, not a key; and it 401 * returns false for null values, while {@code containsKey()} returns true 402 * indicating that the object contains a null value for the key. 403 * 404 * <p> 405 * If a path exists according to {@link #hasPath(String)}, then 406 * {@link #getValue(String)} will never throw an exception. However, the 407 * typed getters, such as {@link #getInt(String)}, will still throw if the 408 * value is not convertible to the requested type. 409 * 410 * <p> 411 * Note that path expressions have a syntax and sometimes require quoting 412 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 413 * 414 * @param path 415 * the path expression 416 * @return true if a non-null value is present at the path 417 * @throws ConfigException.BadPath 418 * if the path expression is invalid 419 */ 420 boolean hasPath(String path); 421 422 /** 423 * Checks whether a value is present at the given path, even 424 * if the value is null. Most of the getters on 425 * <code>Config</code> will throw if you try to get a null 426 * value, so if you plan to call {@link #getValue(String)}, 427 * {@link #getInt(String)}, or another getter you may want to 428 * use plain {@link #hasPath(String)} rather than this method. 429 * 430 * <p> 431 * To handle all three cases (unset, null, and a non-null value) 432 * the code might look like: 433 * <pre><code> 434 * if (config.hasPathOrNull(path)) { 435 * if (config.getIsNull(path)) { 436 * // handle null setting 437 * } else { 438 * // get and use non-null setting 439 * } 440 * } else { 441 * // handle entirely unset path 442 * } 443 * </code></pre> 444 * 445 * <p> However, the usual thing is to allow entirely unset 446 * paths to be a bug that throws an exception (because you set 447 * a default in your <code>reference.conf</code>), so in that 448 * case it's OK to call {@link #getIsNull(String)} without 449 * checking <code>hasPathOrNull</code> first. 450 * 451 * <p> 452 * Note that path expressions have a syntax and sometimes require quoting 453 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 454 * 455 * @param path 456 * the path expression 457 * @return true if a value is present at the path, even if the value is null 458 * @throws ConfigException.BadPath 459 * if the path expression is invalid 460 */ 461 boolean hasPathOrNull(String path); 462 463 /** 464 * Returns true if the {@code Config}'s root object contains no key-value 465 * pairs. 466 * 467 * @return true if the configuration is empty 468 */ 469 boolean isEmpty(); 470 471 /** 472 * Returns the set of path-value pairs, excluding any null values, found by 473 * recursing {@link #root() the root object}. Note that this is very 474 * different from <code>root().entrySet()</code> which returns the set of 475 * immediate-child keys in the root object and includes null values. 476 * <p> 477 * Entries contain <em>path expressions</em> meaning there may be quoting 478 * and escaping involved. Parse path expressions with 479 * {@link ConfigUtil#splitPath}. 480 * <p> 481 * Because a <code>Config</code> is conceptually a single-level map from 482 * paths to values, there will not be any {@link ConfigObject} values in the 483 * entries (that is, all entries represent leaf nodes). Use 484 * {@link ConfigObject} rather than <code>Config</code> if you want a tree. 485 * (OK, this is a slight lie: <code>Config</code> entries may contain 486 * {@link ConfigList} and the lists may contain objects. But no objects are 487 * directly included as entry values.) 488 * 489 * @return set of paths with non-null values, built up by recursing the 490 * entire tree of {@link ConfigObject} and creating an entry for 491 * each leaf value. 492 */ 493 Set<Map.Entry<String, ConfigValue>> entrySet(); 494 495 /** 496 * Checks whether a value is set to null at the given path, 497 * but throws an exception if the value is entirely 498 * unset. This method will not throw if {@link 499 * #hasPathOrNull(String)} returned true for the same path, so 500 * to avoid any possible exception check 501 * <code>hasPathOrNull()</code> first. However, an exception 502 * for unset paths will usually be the right thing (because a 503 * <code>reference.conf</code> should exist that has the path 504 * set, the path should never be unset unless something is 505 * broken). 506 * 507 * <p> 508 * Note that path expressions have a syntax and sometimes require quoting 509 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 510 * 511 * @param path 512 * the path expression 513 * @return true if the value exists and is null, false if it 514 * exists and is not null 515 * @throws ConfigException.BadPath 516 * if the path expression is invalid 517 * @throws ConfigException.Missing 518 * if value is not set at all 519 */ 520 boolean getIsNull(String path); 521 522 /** 523 * 524 * @param path 525 * path expression 526 * @return the boolean value at the requested path 527 * @throws ConfigException.Missing 528 * if value is absent or null 529 * @throws ConfigException.WrongType 530 * if value is not convertible to boolean 531 */ 532 boolean getBoolean(String path); 533 534 /** 535 * @param path 536 * path expression 537 * @return the numeric value at the requested path 538 * @throws ConfigException.Missing 539 * if value is absent or null 540 * @throws ConfigException.WrongType 541 * if value is not convertible to a number 542 */ 543 Number getNumber(String path); 544 545 /** 546 * Gets the integer at the given path. If the value at the 547 * path has a fractional (floating point) component, it 548 * will be discarded and only the integer part will be 549 * returned (it works like a "narrowing primitive conversion" 550 * in the Java language specification). 551 * 552 * @param path 553 * path expression 554 * @return the 32-bit integer value at the requested path 555 * @throws ConfigException.Missing 556 * if value is absent or null 557 * @throws ConfigException.WrongType 558 * if value is not convertible to an int (for example it is out 559 * of range, or it's a boolean value) 560 */ 561 int getInt(String path); 562 563 /** 564 * Gets the long integer at the given path. If the value at 565 * the path has a fractional (floating point) component, it 566 * will be discarded and only the integer part will be 567 * returned (it works like a "narrowing primitive conversion" 568 * in the Java language specification). 569 * 570 * @param path 571 * path expression 572 * @return the 64-bit long value at the requested path 573 * @throws ConfigException.Missing 574 * if value is absent or null 575 * @throws ConfigException.WrongType 576 * if value is not convertible to a long 577 */ 578 long getLong(String path); 579 580 /** 581 * @param path 582 * path expression 583 * @return the floating-point value at the requested path 584 * @throws ConfigException.Missing 585 * if value is absent or null 586 * @throws ConfigException.WrongType 587 * if value is not convertible to a double 588 */ 589 double getDouble(String path); 590 591 /** 592 * @param path 593 * path expression 594 * @return the string value at the requested path 595 * @throws ConfigException.Missing 596 * if value is absent or null 597 * @throws ConfigException.WrongType 598 * if value is not convertible to a string 599 */ 600 String getString(String path); 601 602 /** 603 * @param enumClass 604 * an enum class 605 * @param <T> 606 * a generic denoting a specific type of enum 607 * @param path 608 * path expression 609 * @return the {@code Enum} value at the requested path 610 * of the requested enum class 611 * @throws ConfigException.Missing 612 * if value is absent or null 613 * @throws ConfigException.WrongType 614 * if value is not convertible to an Enum 615 */ 616 public <T extends Enum<T>> T getEnum(Class<T> enumClass, String path); 617 618 /** 619 * @param path 620 * path expression 621 * @return the {@link ConfigObject} value at the requested path 622 * @throws ConfigException.Missing 623 * if value is absent or null 624 * @throws ConfigException.WrongType 625 * if value is not convertible to an object 626 */ 627 ConfigObject getObject(String path); 628 629 /** 630 * @param path 631 * path expression 632 * @return the nested {@code Config} value at the requested path 633 * @throws ConfigException.Missing 634 * if value is absent or null 635 * @throws ConfigException.WrongType 636 * if value is not convertible to a Config 637 */ 638 Config getConfig(String path); 639 640 /** 641 * Gets the value at the path as an unwrapped Java boxed value ( 642 * {@link java.lang.Boolean Boolean}, {@link java.lang.Integer Integer}, and 643 * so on - see {@link ConfigValue#unwrapped()}). 644 * 645 * @param path 646 * path expression 647 * @return the unwrapped value at the requested path 648 * @throws ConfigException.Missing 649 * if value is absent or null 650 */ 651 Object getAnyRef(String path); 652 653 /** 654 * Gets the value at the given path, unless the value is a 655 * null value or missing, in which case it throws just like 656 * the other getters. Use {@code get()} on the {@link 657 * Config#root()} object (or other object in the tree) if you 658 * want an unprocessed value. 659 * 660 * @param path 661 * path expression 662 * @return the value at the requested path 663 * @throws ConfigException.Missing 664 * if value is absent or null 665 */ 666 ConfigValue getValue(String path); 667 668 /** 669 * Gets a value as a size in bytes (parses special strings like "128M"). If 670 * the value is already a number, then it's left alone; if it's a string, 671 * it's parsed understanding unit suffixes such as "128K", as documented in 672 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the 673 * spec</a>. 674 * 675 * @param path 676 * path expression 677 * @return the value at the requested path, in bytes 678 * @throws ConfigException.Missing 679 * if value is absent or null 680 * @throws ConfigException.WrongType 681 * if value is not convertible to Long or String 682 * @throws ConfigException.BadValue 683 * if value cannot be parsed as a size in bytes 684 */ 685 Long getBytes(String path); 686 687 /** 688 * Gets a value as an amount of memory (parses special strings like "128M"). If 689 * the value is already a number, then it's left alone; if it's a string, 690 * it's parsed understanding unit suffixes such as "128K", as documented in 691 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the 692 * spec</a>. 693 * 694 * @since 1.3.0 695 * 696 * @param path 697 * path expression 698 * @return the value at the requested path, in bytes 699 * @throws ConfigException.Missing 700 * if value is absent or null 701 * @throws ConfigException.WrongType 702 * if value is not convertible to Long or String 703 * @throws ConfigException.BadValue 704 * if value cannot be parsed as a size in bytes 705 */ 706 ConfigMemorySize getMemorySize(String path); 707 708 /** 709 * Get value as a duration in milliseconds. If the value is already a 710 * number, then it's left alone; if it's a string, it's parsed understanding 711 * units suffixes like "10m" or "5ns" as documented in 712 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the spec</a>. 713 * 714 * @deprecated As of release 1.1, replaced by {@link #getDuration(String, TimeUnit)} 715 * 716 * @param path 717 * path expression 718 * @return the duration value at the requested path, in milliseconds 719 * @throws ConfigException.Missing 720 * if value is absent or null 721 * @throws ConfigException.WrongType 722 * if value is not convertible to Long or String 723 * @throws ConfigException.BadValue 724 * if value cannot be parsed as a number of milliseconds 725 */ 726 @Deprecated Long getMilliseconds(String path); 727 728 /** 729 * Get value as a duration in nanoseconds. If the value is already a number 730 * it's taken as milliseconds and converted to nanoseconds. If it's a 731 * string, it's parsed understanding unit suffixes, as for 732 * {@link #getDuration(String, TimeUnit)}. 733 * 734 * @deprecated As of release 1.1, replaced by {@link #getDuration(String, TimeUnit)} 735 * 736 * @param path 737 * path expression 738 * @return the duration value at the requested path, in nanoseconds 739 * @throws ConfigException.Missing 740 * if value is absent or null 741 * @throws ConfigException.WrongType 742 * if value is not convertible to Long or String 743 * @throws ConfigException.BadValue 744 * if value cannot be parsed as a number of nanoseconds 745 */ 746 @Deprecated Long getNanoseconds(String path); 747 748 /** 749 * Gets a value as a duration in a specified 750 * {@link java.util.concurrent.TimeUnit TimeUnit}. If the value is already a 751 * number, then it's taken as milliseconds and then converted to the 752 * requested TimeUnit; if it's a string, it's parsed understanding units 753 * suffixes like "10m" or "5ns" as documented in 754 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the spec</a>. 755 * 756 * @since 1.2.0 757 * 758 * @param path 759 * path expression 760 * @param unit 761 * convert the return value to this time unit 762 * @return the duration value at the requested path, in the given TimeUnit 763 * @throws ConfigException.Missing 764 * if value is absent or null 765 * @throws ConfigException.WrongType 766 * if value is not convertible to Long or String 767 * @throws ConfigException.BadValue 768 * if value cannot be parsed as a number of the given TimeUnit 769 */ 770 long getDuration(String path, TimeUnit unit); 771 772 /** 773 * Gets a value as a java.time.Duration. If the value is 774 * already a number, then it's taken as milliseconds; if it's 775 * a string, it's parsed understanding units suffixes like 776 * "10m" or "5ns" as documented in 777 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the spec</a>. 778 * This method never returns null. 779 * 780 * @since 1.3.0 781 * 782 * @param path 783 * path expression 784 * @return the duration value at the requested path 785 * @throws ConfigException.Missing 786 * if value is absent or null 787 * @throws ConfigException.WrongType 788 * if value is not convertible to Long or String 789 * @throws ConfigException.BadValue 790 * if value cannot be parsed as a number of the given TimeUnit 791 */ 792 Duration getDuration(String path); 793 794 /** 795 * Gets a value as a java.time.Period. If the value is 796 * already a number, then it's taken as days; if it's 797 * a string, it's parsed understanding units suffixes like 798 * "10d" or "5w" as documented in 799 * <a href="https://github.com/lightbend/config/blob/main/HOCON.md">the spec</a>. 800 * This method never returns null. 801 * 802 * @since 1.3.0 803 * 804 * @param path 805 * path expression 806 * @return the period value at the requested path 807 * @throws ConfigException.Missing 808 * if value is absent or null 809 * @throws ConfigException.WrongType 810 * if value is not convertible to Long or String 811 * @throws ConfigException.BadValue 812 * if value cannot be parsed as a number of the given TimeUnit 813 */ 814 Period getPeriod(String path); 815 816 /** 817 * Gets a value as a java.time.temporal.TemporalAmount. 818 * This method will first try to get the value as a java.time.Duration, and if unsuccessful, 819 * then as a java.time.Period. 820 * This means that values like "5m" will be parsed as 5 minutes rather than 5 months 821 * @param path path expression 822 * @return the temporal value at the requested path 823 * @throws ConfigException.Missing 824 * if value is absent or null 825 * @throws ConfigException.WrongType 826 * if value is not convertible to Long or String 827 * @throws ConfigException.BadValue 828 * if value cannot be parsed as a TemporalAmount 829 */ 830 TemporalAmount getTemporal(String path); 831 832 /** 833 * Gets a list value (with any element type) as a {@link ConfigList}, which 834 * implements {@code java.util.List<ConfigValue>}. Throws if the path is 835 * unset or null. 836 * 837 * @param path 838 * the path to the list value. 839 * @return the {@link ConfigList} at the path 840 * @throws ConfigException.Missing 841 * if value is absent or null 842 * @throws ConfigException.WrongType 843 * if value is not convertible to a ConfigList 844 */ 845 ConfigList getList(String path); 846 847 /** 848 * Gets a list value with boolean elements. Throws if the 849 * path is unset or null or not a list or contains values not 850 * convertible to boolean. 851 * 852 * @param path 853 * the path to the list value. 854 * @return the list at the path 855 * @throws ConfigException.Missing 856 * if value is absent or null 857 * @throws ConfigException.WrongType 858 * if value is not convertible to a list of booleans 859 */ 860 List<Boolean> getBooleanList(String path); 861 862 /** 863 * Gets a list value with number elements. Throws if the 864 * path is unset or null or not a list or contains values not 865 * convertible to number. 866 * 867 * @param path 868 * the path to the list value. 869 * @return the list at the path 870 * @throws ConfigException.Missing 871 * if value is absent or null 872 * @throws ConfigException.WrongType 873 * if value is not convertible to a list of numbers 874 */ 875 List<Number> getNumberList(String path); 876 877 /** 878 * Gets a list value with int elements. Throws if the 879 * path is unset or null or not a list or contains values not 880 * convertible to int. 881 * 882 * @param path 883 * the path to the list value. 884 * @return the list at the path 885 * @throws ConfigException.Missing 886 * if value is absent or null 887 * @throws ConfigException.WrongType 888 * if value is not convertible to a list of ints 889 */ 890 List<Integer> getIntList(String path); 891 892 /** 893 * Gets a list value with long elements. Throws if the 894 * path is unset or null or not a list or contains values not 895 * convertible to long. 896 * 897 * @param path 898 * the path to the list value. 899 * @return the list at the path 900 * @throws ConfigException.Missing 901 * if value is absent or null 902 * @throws ConfigException.WrongType 903 * if value is not convertible to a list of longs 904 */ 905 List<Long> getLongList(String path); 906 907 /** 908 * Gets a list value with double elements. Throws if the 909 * path is unset or null or not a list or contains values not 910 * convertible to double. 911 * 912 * @param path 913 * the path to the list value. 914 * @return the list at the path 915 * @throws ConfigException.Missing 916 * if value is absent or null 917 * @throws ConfigException.WrongType 918 * if value is not convertible to a list of doubles 919 */ 920 List<Double> getDoubleList(String path); 921 922 /** 923 * Gets a list value with string elements. Throws if the 924 * path is unset or null or not a list or contains values not 925 * convertible to string. 926 * 927 * @param path 928 * the path to the list value. 929 * @return the list at the path 930 * @throws ConfigException.Missing 931 * if value is absent or null 932 * @throws ConfigException.WrongType 933 * if value is not convertible to a list of strings 934 */ 935 List<String> getStringList(String path); 936 937 /** 938 * Gets a list value with {@code Enum} elements. Throws if the 939 * path is unset or null or not a list or contains values not 940 * convertible to {@code Enum}. 941 * 942 * @param enumClass 943 * the enum class 944 * @param <T> 945 * a generic denoting a specific type of enum 946 * @param path 947 * the path to the list value. 948 * @return the list at the path 949 * @throws ConfigException.Missing 950 * if value is absent or null 951 * @throws ConfigException.WrongType 952 * if value is not convertible to a list of {@code Enum} 953 */ 954 <T extends Enum<T>> List<T> getEnumList(Class<T> enumClass, String path); 955 956 /** 957 * Gets a list value with object elements. Throws if the 958 * path is unset or null or not a list or contains values not 959 * convertible to <code>ConfigObject</code>. 960 * 961 * @param path 962 * the path to the list value. 963 * @return the list at the path 964 * @throws ConfigException.Missing 965 * if value is absent or null 966 * @throws ConfigException.WrongType 967 * if value is not convertible to a list of objects 968 */ 969 List<? extends ConfigObject> getObjectList(String path); 970 971 /** 972 * Gets a list value with <code>Config</code> elements. 973 * Throws if the path is unset or null or not a list or 974 * contains values not convertible to <code>Config</code>. 975 * 976 * @param path 977 * the path to the list value. 978 * @return the list at the path 979 * @throws ConfigException.Missing 980 * if value is absent or null 981 * @throws ConfigException.WrongType 982 * if value is not convertible to a list of configs 983 */ 984 List<? extends Config> getConfigList(String path); 985 986 /** 987 * Gets a list value with any kind of elements. Throws if the 988 * path is unset or null or not a list. Each element is 989 * "unwrapped" (see {@link ConfigValue#unwrapped()}). 990 * 991 * @param path 992 * the path to the list value. 993 * @return the list at the path 994 * @throws ConfigException.Missing 995 * if value is absent or null 996 * @throws ConfigException.WrongType 997 * if value is not convertible to a list 998 */ 999 List<? extends Object> getAnyRefList(String path); 1000 1001 /** 1002 * Gets a list value with elements representing a size in 1003 * bytes. Throws if the path is unset or null or not a list 1004 * or contains values not convertible to memory sizes. 1005 * 1006 * @param path 1007 * the path to the list value. 1008 * @return the list at the path 1009 * @throws ConfigException.Missing 1010 * if value is absent or null 1011 * @throws ConfigException.WrongType 1012 * if value is not convertible to a list of memory sizes 1013 */ 1014 List<Long> getBytesList(String path); 1015 1016 /** 1017 * Gets a list, converting each value in the list to a memory size, using the 1018 * same rules as {@link #getMemorySize(String)}. 1019 * 1020 * @since 1.3.0 1021 * @param path 1022 * a path expression 1023 * @return list of memory sizes 1024 * @throws ConfigException.Missing 1025 * if value is absent or null 1026 * @throws ConfigException.WrongType 1027 * if value is not convertible to a list of memory sizes 1028 */ 1029 List<ConfigMemorySize> getMemorySizeList(String path); 1030 1031 /** 1032 * @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)} 1033 * @param path the path 1034 * @return list of millisecond values 1035 */ 1036 @Deprecated List<Long> getMillisecondsList(String path); 1037 1038 /** 1039 * @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)} 1040 * @param path the path 1041 * @return list of nanosecond values 1042 */ 1043 @Deprecated List<Long> getNanosecondsList(String path); 1044 1045 /** 1046 * Gets a list, converting each value in the list to a duration, using the 1047 * same rules as {@link #getDuration(String, TimeUnit)}. 1048 * 1049 * @since 1.2.0 1050 * @param path 1051 * a path expression 1052 * @param unit 1053 * time units of the returned values 1054 * @return list of durations, in the requested units 1055 */ 1056 List<Long> getDurationList(String path, TimeUnit unit); 1057 1058 /** 1059 * Gets a list, converting each value in the list to a duration, using the 1060 * same rules as {@link #getDuration(String)}. 1061 * 1062 * @since 1.3.0 1063 * @param path 1064 * a path expression 1065 * @return list of durations 1066 */ 1067 List<Duration> getDurationList(String path); 1068 1069 /** 1070 * Clone the config with only the given path (and its children) retained; 1071 * all sibling paths are removed. 1072 * <p> 1073 * Note that path expressions have a syntax and sometimes require quoting 1074 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 1075 * 1076 * @param path 1077 * path to keep 1078 * @return a copy of the config minus all paths except the one specified 1079 */ 1080 Config withOnlyPath(String path); 1081 1082 /** 1083 * Clone the config with the given path removed. 1084 * <p> 1085 * Note that path expressions have a syntax and sometimes require quoting 1086 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 1087 * 1088 * @param path 1089 * path expression to remove 1090 * @return a copy of the config minus the specified path 1091 */ 1092 Config withoutPath(String path); 1093 1094 /** 1095 * Places the config inside another {@code Config} at the given path. 1096 * <p> 1097 * Note that path expressions have a syntax and sometimes require quoting 1098 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 1099 * 1100 * @param path 1101 * path expression to store this config at. 1102 * @return a {@code Config} instance containing this config at the given 1103 * path. 1104 */ 1105 Config atPath(String path); 1106 1107 /** 1108 * Places the config inside a {@code Config} at the given key. See also 1109 * atPath(). Note that a key is NOT a path expression (see 1110 * {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 1111 * 1112 * @param key 1113 * key to store this config at. 1114 * @return a {@code Config} instance containing this config at the given 1115 * key. 1116 */ 1117 Config atKey(String key); 1118 1119 /** 1120 * Returns a {@code Config} based on this one, but with the given path set 1121 * to the given value. Does not modify this instance (since it's immutable). 1122 * If the path already has a value, that value is replaced. To remove a 1123 * value, use withoutPath(). 1124 * <p> 1125 * Note that path expressions have a syntax and sometimes require quoting 1126 * (see {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath}). 1127 * 1128 * @param path 1129 * path expression for the value's new location 1130 * @param value 1131 * value at the new path 1132 * @return the new instance with the new map entry 1133 */ 1134 Config withValue(String path, ConfigValue value); 1135}