1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.metrics.util; |
---|
19 | |
---|
20 | import java.util.Collection; |
---|
21 | import java.util.HashMap; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * This is the registry for metrics. |
---|
27 | * Related set of metrics should be declared in a holding class and registered |
---|
28 | * in a registry for those metrics which is also stored in the the holding class. |
---|
29 | * |
---|
30 | */ |
---|
31 | public class MetricsRegistry { |
---|
32 | private Map<String, MetricsBase> metricsList = new HashMap<String, MetricsBase>(); |
---|
33 | |
---|
34 | public MetricsRegistry() { |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @return number of metrics in the registry |
---|
40 | */ |
---|
41 | public int size() { |
---|
42 | return metricsList.size(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Add a new metrics to the registry |
---|
47 | * @param metricsName - the name |
---|
48 | * @param theMetricsObj - the metrics |
---|
49 | * @throws IllegalArgumentException if a name is already registered |
---|
50 | */ |
---|
51 | public synchronized void add(final String metricsName, final MetricsBase theMetricsObj) { |
---|
52 | if (metricsList.containsKey(metricsName)) { |
---|
53 | throw new IllegalArgumentException("Duplicate metricsName:" + metricsName); |
---|
54 | } |
---|
55 | metricsList.put(metricsName, theMetricsObj); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * |
---|
61 | * @param metricsName |
---|
62 | * @return the metrics if there is one registered by the supplied name. |
---|
63 | * Returns null if none is registered |
---|
64 | */ |
---|
65 | public synchronized MetricsBase get(final String metricsName) { |
---|
66 | return metricsList.get(metricsName); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * |
---|
72 | * @return the list of metrics names |
---|
73 | */ |
---|
74 | public synchronized Collection<String> getKeyList() { |
---|
75 | return metricsList.keySet(); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * |
---|
80 | * @return the list of metrics |
---|
81 | */ |
---|
82 | public synchronized Collection<MetricsBase> getMetricsList() { |
---|
83 | return metricsList.values(); |
---|
84 | } |
---|
85 | } |
---|