1 /*
2  * Copyright 2010-2020 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 package com.amazonaws.services.s3.metrics;
16
17 import com.amazonaws.metrics.ServiceMetricType;
18 import com.amazonaws.metrics.SimpleMetricType;
19 import com.amazonaws.metrics.ThroughputMetricType;
20 import com.amazonaws.services.s3.internal.Constants;
21
22 /**
23  * S3 specific service metrics in the form of "simulated enum". The name of
24  * every enum literal defined in this class must all start with "S3" in order
25  * for the default AWS SDK metric collection system to recognize it as a
26  * predefined S3 metrics.
27  */

28 public class S3ServiceMetric extends SimpleMetricType implements ServiceMetricType {
29     static final String SERVICE_NAME_PREFIX = "S3";
30
31     /**
32      * Returns a metric name by concatenating the service name prefix with the
33      * given suffix.
34      */

35     private static final String metricName(String suffix) {
36         return SERVICE_NAME_PREFIX + suffix;
37     }
38
39     public static final S3ThroughputMetric S3DownloadThroughput = new S3ThroughputMetric(
40             metricName(DOWNLOAD_THROUGHPUT_NAME_SUFFIX)) {
41         @Override
42         public ServiceMetricType getByteCountMetricType() {
43             return S3DownloadByteCount;
44         }
45     };
46     public static final S3ServiceMetric S3DownloadByteCount = new S3ServiceMetric(
47             metricName(DOWNLOAD_BYTE_COUNT_NAME_SUFFIX));
48     public static final S3ThroughputMetric S3UploadThroughput = new S3ThroughputMetric(
49             metricName(UPLOAD_THROUGHPUT_NAME_SUFFIX)) {
50         @Override
51         public ServiceMetricType getByteCountMetricType() {
52             return S3UploadByteCount;
53         }
54     };
55     public static final S3ServiceMetric S3UploadByteCount = new S3ServiceMetric(
56             metricName(UPLOAD_BYTE_COUNT_NAME_SUFFIX));
57     private static final S3ServiceMetric[] values = {
58         S3DownloadThroughput,
59         S3DownloadByteCount,
60         S3UploadThroughput,
61         S3UploadByteCount
62     };
63
64     private final String name;
65     private S3ServiceMetric(String name) { this.name = name; }
66     @Override public String name() { return name; }
67     @Override public String getServiceName() {
68         return Constants.S3_SERVICE_DISPLAY_NAME;
69     }
70
71     private static abstract class S3ThroughputMetric extends S3ServiceMetric
72             implements ThroughputMetricType {
73         private S3ThroughputMetric(String name) {
74             super(name);
75         }
76     };
77
78     public static S3ServiceMetric[] values() { return values.clone(); }
79     public static S3ServiceMetric valueOf(String name) {
80         for (S3ServiceMetric e: values()) {
81             if (e.name().equals(name)) {
82                 return e;
83             }
84         }
85         throw new IllegalArgumentException("No S3ServiceMetric defined for the name "+ name);
86     }
87 }
88