Sets a custom option. Any existing value for the key is overwritten. @param key The option key @param value The option value. @since 1.13.0
(Key<T> key, T value)
| 398 | * @since 1.13.0 |
| 399 | */ |
| 400 | public <T> CallOptions withOption(Key<T> key, T value) { |
| 401 | Preconditions.checkNotNull(key, "key"); |
| 402 | Preconditions.checkNotNull(value, "value"); |
| 403 | |
| 404 | Builder builder = toBuilder(this); |
| 405 | int existingIdx = -1; |
| 406 | for (int i = 0; i < customOptions.length; i++) { |
| 407 | if (key.equals(customOptions[i][0])) { |
| 408 | existingIdx = i; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | builder.customOptions = new Object[customOptions.length + (existingIdx == -1 ? 1 : 0)][2]; |
| 414 | System.arraycopy(customOptions, 0, builder.customOptions, 0, customOptions.length); |
| 415 | |
| 416 | if (existingIdx == -1) { |
| 417 | // Add a new option |
| 418 | builder.customOptions[customOptions.length] = new Object[] {key, value}; |
| 419 | } else { |
| 420 | // Replace an existing option |
| 421 | builder.customOptions[existingIdx] = new Object[] {key, value}; |
| 422 | } |
| 423 | |
| 424 | return builder.build(); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Get the value for a custom option or its inherent default. |