View Javadoc

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.hbase.hbtop;
19  
20  import com.google.common.collect.ImmutableMap;
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  import java.util.AbstractMap;
23  import java.util.Collection;
24  import java.util.Map;
25  import java.util.Set;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.hbtop.field.Field;
28  import org.apache.hadoop.hbase.hbtop.field.FieldValue;
29  import org.apache.hadoop.hbase.hbtop.field.FieldValueType;
30  
31  
32  /**
33   * Represents a record of the metrics in the top screen.
34   */
35  @InterfaceAudience.Private
36  public final class Record implements Map<Field, FieldValue> {
37  
38    private final ImmutableMap<Field, FieldValue> values;
39  
40    public final static class Entry extends AbstractMap.SimpleImmutableEntry<Field, FieldValue> {
41      private Entry(Field key, FieldValue value) {
42        super(key, value);
43      }
44    }
45  
46    public final static class Builder {
47  
48      private final ImmutableMap.Builder<Field, FieldValue> builder;
49  
50      private Builder() {
51        builder = ImmutableMap.builder();
52      }
53  
54      public Builder put(Field key, Object value) {
55        builder.put(key, key.newValue(value));
56        return this;
57      }
58  
59      public Builder put(Field key, FieldValue value) {
60        builder.put(key, value);
61        return this;
62      }
63  
64      public Builder put(Entry entry) {
65        builder.put(entry);
66        return this;
67      }
68  
69      public Builder putAll(Map<Field, FieldValue> map) {
70        builder.putAll(map);
71        return this;
72      }
73  
74      public Record build() {
75        return new Record(builder.build());
76      }
77    }
78  
79    public static Builder builder() {
80      return new Builder();
81    }
82  
83    public static Entry entry(Field field, Object value) {
84      return new Entry(field, field.newValue(value));
85    }
86  
87    public static Entry entry(Field field, FieldValue value) {
88      return new Entry(field, value);
89    }
90  
91    public static Record ofEntries(Entry... entries) {
92      Builder builder = builder();
93      for (Entry entry : entries) {
94        builder.put(entry.getKey(), entry.getValue());
95      }
96      return builder.build();
97    }
98  
99    public static Record ofEntries(Iterable<Entry> entries) {
100     Builder builder = builder();
101     for (Entry entry : entries) {
102       builder.put(entry.getKey(), entry.getValue());
103     }
104     return builder.build();
105   }
106 
107   private Record(ImmutableMap<Field, FieldValue> values) {
108     this.values = values;
109   }
110 
111   @Override
112   public int size() {
113     return values.size();
114   }
115 
116   @Override
117   public boolean isEmpty() {
118     return values.isEmpty();
119   }
120 
121   @Override
122   public boolean containsKey(Object key) {
123     return values.containsKey(key);
124   }
125 
126   @Override
127   public boolean containsValue(Object value) {
128     return values.containsValue(value);
129   }
130 
131   @Override
132   public FieldValue get(Object key) {
133     return values.get(key);
134   }
135 
136   @Override
137   public FieldValue put(Field key, FieldValue value) {
138     throw new UnsupportedOperationException();
139   }
140 
141   @Override
142   public FieldValue remove(Object key) {
143     throw new UnsupportedOperationException();
144   }
145 
146   @Override
147   public void putAll(@NonNull Map<? extends Field, ? extends FieldValue> m) {
148     throw new UnsupportedOperationException();
149   }
150 
151   @Override
152   public void clear() {
153     throw new UnsupportedOperationException();
154   }
155 
156   @Override
157   @NonNull
158   public Set<Field> keySet() {
159     return values.keySet();
160   }
161 
162   @Override
163   @NonNull
164   public Collection<FieldValue> values() {
165     return values.values();
166   }
167 
168   @Override
169   @NonNull
170   public Set<Map.Entry<Field, FieldValue>> entrySet() {
171     return values.entrySet();
172   }
173 
174   public Record combine(Record o) {
175     Builder builder = builder();
176     for (Field k : values.keySet()) {
177       if (k.getFieldValueType() == FieldValueType.STRING) {
178         builder.put(k, values.get(k));
179       } else {
180         builder.put(k, values.get(k).plus(o.values.get(k)));
181       }
182     }
183     return builder.build();
184   }
185 }