src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/EventConsumer.java
author egahlin
Thu, 27 Jun 2019 10:19:32 +0200
branchJEP-349-branch
changeset 57433 83e4343a6984
parent 57432 ba454a26d2c1
permissions -rw-r--r--
Clean up and fix parser level filtering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     1
/*
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     4
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    10
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    15
 * accompanied this code).
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    16
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    20
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    23
 * questions.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    24
 */
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    25
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal.consumer;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    27
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    28
import java.io.IOException;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    29
import java.lang.invoke.MethodHandles;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    30
import java.lang.invoke.VarHandle;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    31
import java.security.AccessControlContext;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    32
import java.security.AccessController;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    33
import java.security.PrivilegedAction;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    34
import java.time.Duration;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    35
import java.util.ArrayList;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    36
import java.util.Arrays;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    37
import java.util.List;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    38
import java.util.Objects;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    39
import java.util.function.Consumer;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    40
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    41
import jdk.jfr.EventType;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    42
import jdk.jfr.consumer.RecordedEvent;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    43
import jdk.jfr.internal.JVM;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    44
import jdk.jfr.internal.LogLevel;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    45
import jdk.jfr.internal.LogTag;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    46
import jdk.jfr.internal.Logger;
57373
400db63e4937 Move LongMap to util. Update use cases
egahlin
parents: 57372
diff changeset
    47
import jdk.jfr.internal.LongMap;
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    48
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    49
abstract public class EventConsumer implements Runnable {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    50
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    51
    private final static class EventDispatcher {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    52
        public final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    53
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    54
        final private String eventName;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    55
        final Consumer<RecordedEvent> action;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    56
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    57
        public EventDispatcher(Consumer<RecordedEvent> action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    58
            this(null, action);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    59
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    60
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    61
        public EventDispatcher(String eventName, Consumer<RecordedEvent> action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    62
            this.eventName = eventName;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    63
            this.action = action;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    64
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    65
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    66
        public void offer(RecordedEvent event) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    67
            action.accept(event);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    68
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    69
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    70
        public boolean accepts(EventType eventType) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    71
            return (eventName == null || eventType.getName().equals(eventName));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    72
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    73
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    74
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    75
    private final static JVM jvm = JVM.getJVM();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    76
    private final static VarHandle closedHandle;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    77
    private final static VarHandle consumersHandle;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    78
    private final static VarHandle dispatcherHandle;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    79
    private final static VarHandle flushActionsHandle;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    80
    private final static VarHandle closeActionsHandle;
57432
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
    81
    private final static VarHandle orderedHandle;
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
    82
    private final static VarHandle reuseHandle;
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    83
    static {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    84
        try {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    85
            MethodHandles.Lookup l = MethodHandles.lookup();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    86
            closedHandle = l.findVarHandle(EventConsumer.class, "closed", boolean.class);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    87
            consumersHandle = l.findVarHandle(EventConsumer.class, "consumers", EventDispatcher[].class);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    88
            dispatcherHandle = l.findVarHandle(EventConsumer.class, "dispatcher", LongMap.class);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    89
            flushActionsHandle = l.findVarHandle(EventConsumer.class, "flushActions", Runnable[].class);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    90
            closeActionsHandle = l.findVarHandle(EventConsumer.class, "closeActions", Runnable[].class);
57432
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
    91
            orderedHandle = l.findVarHandle(EventConsumer.class, "ordered", boolean.class);
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
    92
            reuseHandle = l.findVarHandle(EventConsumer.class, "reuse", boolean.class);
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
    93
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    94
        } catch (ReflectiveOperationException e) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    95
            throw new InternalError(e);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    96
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    97
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    98
    // set by VarHandle
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    99
    private boolean closed;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   100
    // set by VarHandle
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   101
    private EventDispatcher[] consumers = new EventDispatcher[0];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   102
    // set by VarHandle
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   103
    private LongMap<EventDispatcher[]> dispatcher = new LongMap<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   104
    // set by VarHandle
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   105
    private Runnable[] flushActions = new Runnable[0];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   106
    // set by VarHandle
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   107
    private Runnable[] closeActions = new Runnable[0];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   108
57432
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   109
    protected boolean ordered = true;
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   110
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   111
    protected boolean reuse = true;
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   112
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   113
    protected volatile InternalEventFilter eventFilter = InternalEventFilter.ACCEPT_ALL;
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   114
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   115
    private final AccessControlContext accessControlContext;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   116
    private boolean started;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   117
    private Thread thread;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   118
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   119
    protected long startNanos;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   120
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   121
    public EventConsumer(AccessControlContext acc) throws IOException {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   122
        this.accessControlContext = acc;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   123
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   124
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   125
    public void run() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   126
        doPriviliged(() -> execute());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   127
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   128
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   129
    void doPriviliged(Runnable r) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   130
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   131
            @Override
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   132
            public Void run() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   133
                r.run();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   134
                return null;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   135
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   136
        }, accessControlContext);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   137
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   138
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   139
    private void execute() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   140
        jvm.exclude(Thread.currentThread());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   141
        try {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   142
            process();
57385
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   143
        } catch (IOException e) {
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   144
            if (!isClosed()) {
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   145
                logException(e);
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   146
            }
57380
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   147
        } catch (Exception e) {
57385
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   148
            logException(e);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   149
        } finally {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   150
            Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended.");
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   151
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   152
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   153
57385
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   154
    private void logException(Exception e) {
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   155
        e.printStackTrace(); // for debugging purposes, remove before
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   156
        // integration
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   157
        Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Unexpected error processing stream. " + e.getMessage());
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   158
    }
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   159
7d9d4f629f6e Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents: 57380
diff changeset
   160
    public abstract void process() throws IOException;
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   161
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   162
    public synchronized boolean remove(Object action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   163
        boolean remove = false;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   164
        Runnable[] updatedFlushActions = removeAction(flushActions, action);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   165
        if (updatedFlushActions != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   166
            flushActionsHandle.setVolatile(this, updatedFlushActions);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   167
            remove = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   168
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   169
        Runnable[] updatedCloseActions = removeAction(closeActions, action);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   170
        if (updatedCloseActions != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   171
            closeActionsHandle.setVolatile(this, updatedCloseActions);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   172
            remove = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   173
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   174
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   175
        boolean removeConsumer = false;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   176
        List<EventDispatcher> list = new ArrayList<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   177
        for (int i = 0; i < consumers.length; i++) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   178
            if (consumers[i].action != action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   179
                list.add(consumers[i]);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   180
            } else {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   181
                removeConsumer = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   182
                remove = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   183
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   184
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   185
        if (removeConsumer) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   186
            EventDispatcher[] array = list.toArray(new EventDispatcher[list.size()]);
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   187
            eventFilter = buildFilter(array);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   188
            consumersHandle.setVolatile(this, array);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   189
            dispatcherHandle.setVolatile(this, new LongMap<>()); // will reset
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   190
                                                                 // dispatch
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   191
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   192
        return remove;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   193
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   194
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   195
    public void dispatch(RecordedEvent e) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   196
        EventDispatcher[] consumerDispatch = dispatcher.get(e.getEventType().getId());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   197
        if (consumerDispatch == null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   198
            consumerDispatch = EventDispatcher.NO_DISPATCHERS;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   199
            for (EventDispatcher ec : consumers.clone()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   200
                if (ec.accepts(e.getEventType())) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   201
                    consumerDispatch = merge(consumerDispatch, ec);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   202
                }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   203
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   204
            dispatcher.put(e.getEventType().getId(), consumerDispatch);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   205
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   206
        for (int i = 0; i < consumerDispatch.length; i++) {
57380
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   207
            try {
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   208
                consumerDispatch[i].offer(e);
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   209
            } catch (Exception exception) {
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   210
                // Is this a reasonable behavior for an exception?
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   211
                // Error will abort the stream.
6a7e7743b82f setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents: 57373
diff changeset
   212
            }
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   213
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   214
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   215
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   216
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   217
    public void onEvent(Consumer<RecordedEvent> action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   218
        add(new EventDispatcher(action));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   219
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   220
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   221
    public void onEvent(String eventName, Consumer<RecordedEvent> action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   222
        add(new EventDispatcher(eventName, action));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   223
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   224
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   225
    InternalEventFilter buildFilter(EventDispatcher[] dispatchers) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   226
        InternalEventFilter ef = new InternalEventFilter();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   227
        for (EventDispatcher ed : dispatchers) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   228
            String name = ed.eventName;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   229
            if (name == null) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   230
                return InternalEventFilter.ACCEPT_ALL;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   231
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   232
            ef.setThreshold(name, 0);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   233
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   234
        return ef.threadSafe();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   235
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   236
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   237
    private synchronized void add(EventDispatcher e) {
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   238
        EventDispatcher[] dispatchers = merge(consumers,e);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   239
        eventFilter = buildFilter(dispatchers);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents: 57432
diff changeset
   240
        consumersHandle.setVolatile(this, dispatchers);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   241
        dispatcherHandle.setVolatile(this, new LongMap<>()); // will reset
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   242
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   243
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   244
    public synchronized void onFlush(Runnable action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   245
        flushActionsHandle.setVolatile(this, addAction(flushActions, action));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   246
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   247
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   248
    public synchronized void addCloseAction(Runnable action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   249
        closeActionsHandle.setVolatile(this, addAction(closeActions, action));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   250
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   251
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   252
    public void setClosed(boolean closed) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   253
        closedHandle.setVolatile(this, closed);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   254
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   255
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   256
    final public boolean isClosed() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   257
        return closed;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   258
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   259
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   260
    public void runCloseActions() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   261
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   262
        Runnable[] cas = this.closeActions;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   263
        for (int i = 0; i < cas.length; i++) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   264
            cas[i].run();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   265
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   266
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   267
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   268
    public void runFlushActions() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   269
        Runnable[] fas = this.flushActions;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   270
        for (int i = 0; i < fas.length; i++) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   271
            fas[i].run();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   272
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   273
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   274
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   275
    public synchronized void startAsync(long startNanos) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   276
        if (started) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   277
            throw new IllegalStateException("Event stream can only be started once");
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   278
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   279
        started = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   280
        setStartNanos(startNanos);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   281
        thread = new Thread(this);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   282
        thread.setDaemon(true);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   283
        thread.start();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   284
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   285
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   286
    public void start(long startNanos) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   287
        synchronized (this) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   288
            if (started) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   289
                throw new IllegalStateException("Event stream can only be started once");
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   290
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   291
            started = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   292
            setStartNanos(startNanos);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   293
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   294
        run();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   295
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   296
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   297
    public void awaitTermination(Duration timeout) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   298
        Objects.requireNonNull(timeout);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   299
        Thread t = null;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   300
        synchronized (this) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   301
            t = thread;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   302
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   303
        if (t != null && t != Thread.currentThread()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   304
            try {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   305
                t.join(timeout.toMillis());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   306
            } catch (InterruptedException e) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   307
                // ignore
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   308
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   309
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   310
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   311
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   312
    public void awaitTermination() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   313
        awaitTermination(Duration.ofMillis(0));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   314
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   315
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   316
    private void setStartNanos(long startNanos) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   317
        this.startNanos = startNanos;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   318
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   319
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   320
    protected static EventDispatcher[] merge(EventDispatcher[] current, EventDispatcher add) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   321
        EventDispatcher[] array = new EventDispatcher[current.length + 1];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   322
        System.arraycopy(current, 0, array, 0, current.length);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   323
        array[current.length] = add;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   324
        return array;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   325
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   326
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   327
    private static Runnable[] removeAction(Runnable[] array, Object action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   328
        if (array.length == 0) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   329
            return null;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   330
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   331
        boolean remove = false;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   332
        List<Runnable> list = new ArrayList<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   333
        for (int i = 0; i < array.length; i++) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   334
            if (array[i] != action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   335
                list.add(array[i]);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   336
            } else {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   337
                remove = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   338
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   339
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   340
        if (remove) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   341
            return list.toArray(new Runnable[list.size()]);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   342
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   343
        return null;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   344
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   345
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   346
    private static Runnable[] addAction(Runnable[] array, Runnable action) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   347
        ArrayList<Runnable> a = new ArrayList<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   348
        a.addAll(Arrays.asList(array));
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   349
        a.add(action);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   350
        return a.toArray(new Runnable[0]);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   351
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   352
57386
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   353
    abstract public void close();
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   354
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   355
    public void setReuse(boolean reuse) {
57432
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   356
        reuseHandle.setVolatile(this, reuse);
57386
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   357
    }
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   358
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   359
    public void setOrdered(boolean ordered) {
57432
ba454a26d2c1 SetOrdered and setReuse now work on a directory stream
egahlin
parents: 57386
diff changeset
   360
        orderedHandle.setVolatile(this, ordered);
57386
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   361
    }
acdd0dbe37ee First parts of unshared parser + updated javadoc
egahlin
parents: 57385
diff changeset
   362
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   363
}