src/jdk.jfr/share/classes/jdk/jfr/consumer/InternalEventFilter.java
branchJEP-349-branch
changeset 57361 53dccc90a5be
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     1 /*
       
     2  * Copyright (c) 2019, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.consumer;
       
    27 
       
    28 import java.util.Collection;
       
    29 import java.util.HashMap;
       
    30 import java.util.HashSet;
       
    31 import java.util.Map;
       
    32 import java.util.Set;
       
    33 
       
    34 public final class InternalEventFilter {
       
    35     static final InternalEventFilter ACCEPT_ALL = new InternalEventFilter();
       
    36     private final Map<String, Long> thresholds = new HashMap<>();
       
    37     private boolean acceptAll;
       
    38 
       
    39     public static InternalEventFilter merge(Collection<InternalEventFilter> filters) {
       
    40         for (InternalEventFilter ef : filters) {
       
    41             if (ef.getAcceptAll()) {
       
    42                 return ACCEPT_ALL;
       
    43             }
       
    44         }
       
    45         if (filters.size() == 1) {
       
    46             return filters.iterator().next();
       
    47         }
       
    48 
       
    49         Set<String> eventNames = new HashSet<>();
       
    50         for (InternalEventFilter ef : filters) {
       
    51             eventNames.addAll(ef.thresholds.keySet());
       
    52         }
       
    53         InternalEventFilter result = new InternalEventFilter();
       
    54         for (String eventName : eventNames) {
       
    55             for (InternalEventFilter ef : filters) {
       
    56                 Long l = ef.thresholds.get(eventName);
       
    57                 if (l != null) {
       
    58                     result.setThreshold(eventName, l.longValue());
       
    59                 }
       
    60             }
       
    61         }
       
    62         return result;
       
    63     }
       
    64 
       
    65     private boolean getAcceptAll() {
       
    66         return acceptAll;
       
    67     }
       
    68 
       
    69     public void setAcceptAll() {
       
    70         acceptAll = true;
       
    71     }
       
    72 
       
    73     public void setThreshold(String eventName, long nanos) {
       
    74         Long l = thresholds.get(eventName);
       
    75         if (l != null) {
       
    76             l = Math.min(l, nanos);
       
    77         } else {
       
    78             l = nanos;
       
    79         }
       
    80         thresholds.put(eventName, l);
       
    81     }
       
    82 
       
    83     public long getThreshold(String eventName) {
       
    84         if (acceptAll) {
       
    85             return 0;
       
    86         }
       
    87         Long l = thresholds.get(eventName);
       
    88         if (l != null) {
       
    89             return l;
       
    90         }
       
    91         return -1;
       
    92     }
       
    93     public String toString() {
       
    94         if (acceptAll) {
       
    95             return "ACCEPT ALL";
       
    96         }
       
    97         StringBuilder sb = new StringBuilder();
       
    98         for (String key : thresholds.keySet().toArray(new String[0])) {
       
    99             Long value = thresholds.get(key);
       
   100             sb.append(key);
       
   101             sb.append(" = ");
       
   102             sb.append(value.longValue() / 1_000_000);
       
   103             sb.append(" ms");
       
   104         }
       
   105         return sb.toString();
       
   106     }
       
   107 }