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.protocols.core;
17
18 import java.math.BigDecimal;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.core.SdkBytes;
21 import software.amazon.awssdk.core.SdkField;
22 import software.amazon.awssdk.utils.BinaryUtils;
23
24 /**
25  * Converts various types to Strings. Used for Query Param/Header/Path marshalling.
26  */

27 @SdkProtectedApi
28 public final class ValueToStringConverter {
29
30     /**
31      * Interface to convert a type to a String.
32      *
33      * @param <T> Type to convert.
34      */

35     @FunctionalInterface
36     public interface ValueToString<T> {
37
38         /**
39          * Converts the value to a string.
40          *
41          * @param t Value to convert.
42          * @param field {@link SdkField} containing metadata about the member being marshalled.
43          * @return String value.
44          */

45         String convert(T t, SdkField<T> field);
46     }
47
48     /**
49      * Simple interface to convert a type to a String. Useful for implementations that don't need the {@link SdkField}.
50      *
51      * @param <T> Type to convert.
52      */

53     @FunctionalInterface
54     public interface SimpleValueToString<T> extends ValueToString<T> {
55
56         @Override
57         default String convert(T t, SdkField<T> field) {
58             return convert(t);
59         }
60
61         /**
62          * Converts the value to a string.
63          *
64          * @param t Value to convert.
65          * @return String value.
66          */

67         String convert(T t);
68     }
69
70     /**
71      * Identity converter.
72      */

73     public static final SimpleValueToString<String> FROM_STRING = val -> val;
74
75     public static final SimpleValueToString<Integer> FROM_INTEGER = Object::toString;
76
77     public static final SimpleValueToString<Long> FROM_LONG = Object::toString;
78
79     public static final SimpleValueToString<Float> FROM_FLOAT = Object::toString;
80
81     public static final SimpleValueToString<Double> FROM_DOUBLE = Object::toString;
82
83     public static final SimpleValueToString<BigDecimal> FROM_BIG_DECIMAL = Object::toString;
84
85     /**
86      * Marshalls boolean as a literal 'true' or 'false' string.
87      */

88     public static final SimpleValueToString<Boolean> FROM_BOOLEAN = Object::toString;
89
90     /**
91      * Marshalls bytes as a Base64 string.
92      */

93     public static final SimpleValueToString<SdkBytes> FROM_SDK_BYTES = b -> BinaryUtils.toBase64(b.asByteArray());
94
95     private ValueToStringConverter() {
96     }
97
98 }
99