1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package gc.g1.plab.lib;
  24 
  25 import java.util.Collection;
  26 import java.util.HashMap;
  27 import java.util.List;
  28 import java.util.Map;
  29 import java.util.stream.Collectors;
  30 import java.util.stream.Stream;
  31 
  32 public class PlabInfo {
  33 
  34     private final Map<String, Long> plabInfo;
  35 
  36     public PlabInfo() {
  37         plabInfo = new HashMap<>();
  38     }
  39 
  40     private PlabInfo(Map<String, Long> map) {
  41         plabInfo = new HashMap<>(map);
  42     }
  43 
  44     /**
  45      * Add key and value to underlying Map.
  46      * @param key   PLAB info field name
  47      * @param value PLAB info value for field
  48      */
  49     public void put(String key, long value) {
  50         plabInfo.put(key, value);
  51     }
  52 
  53     /**
  54      * Get stream of Map.Entry representing underlying Map with PLAB information.
  55      */
  56     public Stream<Map.Entry<String, Long>> entryStream() {
  57         return plabInfo.entrySet().stream();
  58     }
  59 
  60     /**
  61      * Returns the PlabInfo narrowed for the given fields only
  62      * @param fields
  63      * @return PlabInfo
  64      */
  65     public PlabInfo filter(List<String> fields) {
  66         return new PlabInfo(entryStream()
  67                 .filter(field -> fields.contains(field.getKey()))
  68                 .collect(Collectors.toMap(
  69                         item -> item.getKey(),
  70                         item -> item.getValue())
  71                 )
  72         );
  73     }
  74 
  75     /**
  76      * Checks if statistic contains expected fields.
  77      * @param fields fields which should be in statistic
  78      * @return true if all fields are in statistic, false otherwise
  79      */
  80     public boolean checkFields(List<String> fields) {
  81         for (String key : fields) {
  82             if (!plabInfo.containsKey(key)) {
  83                 return false;
  84             }
  85         }
  86         return true;
  87     }
  88 
  89     /**
  90      * Return a collection of the values.
  91      * @return collection of values
  92      */
  93     public Collection<Long> values() {
  94         return plabInfo.values();
  95     }
  96 
  97     /**
  98      * Get value for specified field.
  99      * @param field
 100      * @return long value which is contained in specified field
 101      */
 102     public long get(String field) {
 103         return plabInfo.get(field);
 104     }
 105 }