All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovyx.gpars.dataflow.stream.DataflowStreamWriteAdapter Maven / Gradle / Ivy

Go to download

The Groovy and Java high-level concurrency library offering actors, dataflow, CSP, agents, parallel collections, fork/join and more

There is a newer version: 1.2.1
Show newest version
// GPars - Groovy Parallel Systems
//
// Copyright © 2008-11  The original author or authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package groovyx.gpars.dataflow.stream;

import groovyx.gpars.dataflow.DataflowReadChannel;
import groovyx.gpars.dataflow.DataflowWriteChannel;

/**
 * Adapts a DataflowStream to accommodate for the DataflowWriteChannel interface.
 * It also synchronizes all changes to the underlying stream allowing multiple threads accessing the stream concurrently.
 *
 * @param  The type of messages to pass through the stream
 * @author Vaclav Pech
 */
@SuppressWarnings({"SynchronizedMethod"})
public class DataflowStreamWriteAdapter implements DataflowWriteChannel {

    private StreamCore head;

    /**
     * Creates a new adapter
     *
     * @param stream The stream to wrap
     */
    public DataflowStreamWriteAdapter(final StreamCore stream) {
        this.head = stream;
    }

    @Override
    public final DataflowWriteChannel leftShift(final T value) {
        updateHead().leftShift(value);
        return this;
    }

    @Override
    public final DataflowWriteChannel leftShift(final DataflowReadChannel ref) {
        updateHead().leftShift(ref);
        return this;
    }

    @Override
    public final void bind(final T value) {
        updateHead().leftShift(value);
    }

    /**
     * Moves head
     *
     * @return The old head
     */
    private synchronized StreamCore updateHead() {
        final StreamCore oldHead = head;
        head = (StreamCore) head.getRest();
        return oldHead;
    }

    @Override
    public synchronized String toString() {
        return head.toString();
    }

    protected final synchronized StreamCore getHead() {
        return head;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy