1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.http; 17 18 import java.util.Map; 19 import java.util.Objects; 20 import software.amazon.awssdk.annotations.SdkPublicApi; 21 import software.amazon.awssdk.http.async.AsyncExecuteRequest; 22 import software.amazon.awssdk.utils.AttributeMap; 23 import software.amazon.awssdk.utils.Validate; 24 import software.amazon.awssdk.utils.builder.CopyableBuilder; 25 import software.amazon.awssdk.utils.builder.ToCopyableBuilder; 26 27 /** 28 * An immutable collection of {@link SdkHttpExecutionAttribute}s that can be configured on an {@link AsyncExecuteRequest} via 29 * {@link AsyncExecuteRequest.Builder#httpExecutionAttributes(SdkHttpExecutionAttributes)} 30 */ 31 @SdkPublicApi 32 public final class SdkHttpExecutionAttributes implements ToCopyableBuilder<SdkHttpExecutionAttributes.Builder, 33 SdkHttpExecutionAttributes> { 34 private final AttributeMap attributes; 35 SdkHttpExecutionAttributes(Builder builder)36 private SdkHttpExecutionAttributes(Builder builder) { 37 this.attributes = builder.sdkHttpExecutionAttributes.build(); 38 } 39 40 /** 41 * Retrieve the current value of the provided attribute in this collection of attributes. This will return null if the value 42 * is not set. 43 */ getAttribute(SdkHttpExecutionAttribute<T> attribute)44 public <T> T getAttribute(SdkHttpExecutionAttribute<T> attribute) { 45 return attributes.get(attribute); 46 } 47 builder()48 public static Builder builder() { 49 return new Builder(); 50 } 51 52 @Override toBuilder()53 public Builder toBuilder() { 54 return new Builder(attributes); 55 } 56 57 @Override equals(Object o)58 public boolean equals(Object o) { 59 if (this == o) { 60 return true; 61 } 62 63 if (o == null || getClass() != o.getClass()) { 64 return false; 65 } 66 67 SdkHttpExecutionAttributes that = (SdkHttpExecutionAttributes) o; 68 69 return Objects.equals(attributes, that.attributes); 70 } 71 72 @Override hashCode()73 public int hashCode() { 74 return attributes.hashCode(); 75 } 76 77 public static final class Builder implements CopyableBuilder<SdkHttpExecutionAttributes.Builder, SdkHttpExecutionAttributes> { 78 private AttributeMap.Builder sdkHttpExecutionAttributes = AttributeMap.builder(); 79 Builder(AttributeMap attributes)80 private Builder(AttributeMap attributes) { 81 sdkHttpExecutionAttributes = attributes.toBuilder(); 82 } 83 Builder()84 private Builder() { 85 } 86 87 /** 88 * Add a mapping between the provided key and value. 89 */ put(SdkHttpExecutionAttribute<T> key, T value)90 public <T> SdkHttpExecutionAttributes.Builder put(SdkHttpExecutionAttribute<T> key, T value) { 91 Validate.notNull(key, "Key to set must not be null."); 92 sdkHttpExecutionAttributes.put(key, value); 93 return this; 94 } 95 96 /** 97 * Adds all the attributes from the map provided. 98 */ putAll(Map<? extends SdkHttpExecutionAttribute<?>, ?> attributes)99 public SdkHttpExecutionAttributes.Builder putAll(Map<? extends SdkHttpExecutionAttribute<?>, ?> attributes) { 100 sdkHttpExecutionAttributes.putAll(attributes); 101 return this; 102 } 103 104 @Override build()105 public SdkHttpExecutionAttributes build() { 106 return new SdkHttpExecutionAttributes(this); 107 } 108 } 109 }