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  * Converter implementations that transform a String to a specified type.
26  */

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

35     @FunctionalInterface
36     public interface StringToValue<T> {
37
38         /**
39          * Converts the value to a string.
40          *
41          * @param s Value to convert from.
42          * @param sdkField {@link SdkField} containing metadata about the member being unmarshalled.
43          * @return Unmarshalled value.
44          */

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

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

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

73     public static final SimpleStringToValue<String> TO_STRING = val -> val;
74
75     public static final SimpleStringToValue<Integer> TO_INTEGER = Integer::parseInt;
76
77     public static final SimpleStringToValue<Long> TO_LONG = Long::parseLong;
78
79     public static final SimpleStringToValue<Float> TO_FLOAT = Float::parseFloat;
80
81     public static final SimpleStringToValue<Double> TO_DOUBLE = Double::parseDouble;
82
83     public static final SimpleStringToValue<BigDecimal> TO_BIG_DECIMAL = BigDecimal::new;
84
85     public static final SimpleStringToValue<Boolean> TO_BOOLEAN = Boolean::parseBoolean;
86
87     public static final SimpleStringToValue<SdkBytes> TO_SDK_BYTES = StringToValueConverter::toSdkBytes;
88
89     private StringToValueConverter() {
90     }
91
92     private static SdkBytes toSdkBytes(String s) {
93         return SdkBytes.fromByteArray(BinaryUtils.fromBase64(s));
94     }
95
96 }
97