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

com.datatorrent.stram.debug.MuxSink Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.datatorrent.stram.debug;

import java.lang.reflect.Array;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datatorrent.api.Sink;

/**
 * 

MuxSink class.

* * @since 0.3.2 */ public class MuxSink implements Sink { Sink[] sinks; private int count; public MuxSink(Sink... s) { sinks = s; } @SuppressWarnings("unchecked") public MuxSink() { sinks = (Sink[])Array.newInstance(Sink.class, 0); } @Override public void put(Object tuple) { count++; for (int i = sinks.length; i-- > 0;) { sinks[i].put(tuple); } } public void add(Sink... s) { int i = sinks.length; sinks = Arrays.copyOf(sinks, i + s.length); for (Sink ss : s) { sinks[i++] = ss; } } public void remove(Sink s) { boolean found = false; for (int i = sinks.length; i-- > 0;) { if (sinks[i] == s) { sinks[i] = null; found = true; } } if (found) { @SuppressWarnings("unchecked") Sink[] newInstance = (Sink[])Array.newInstance(Sink.class, sinks.length - 1); int i = 0; for (int j = sinks.length; j-- > 0;) { if (sinks[j] != null) { newInstance[i++] = sinks[j]; } } sinks = newInstance; } } public void remove(Sink... s) { /* mark all the sinks to be deleted as null */ int found = 0; for (int i = s.length; i-- > 0;) { for (int j = sinks.length; j-- > 0;) { if (s[i] == sinks[j]) { sinks[j] = null; found++; break; } } } /* copy over rest of the sinks to a new array */ @SuppressWarnings({"unchecked"}) Sink[] newInstance = (Sink[])Array.newInstance(Sink.class, sinks.length - found); int i = 0; for (int j = sinks.length; j-- > 0;) { if (sinks[j] != null) { newInstance[i++] = sinks[j]; } } /* now new array is our final list of sinks */ sinks = newInstance; } /** * Get the count of sinks supported currently. * * @return the count of sinks catered. */ public Sink[] getSinks() { return Arrays.copyOf(sinks, sinks.length); } @Override public int getCount(boolean reset) { try { return count; } finally { if (reset) { count = 0; } } } private static final Logger logger = LoggerFactory.getLogger(MuxSink.class); }