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

io.dstream.function.ValuesAggregatingFunction Maven / Gradle / Ivy

/**
 * 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 io.dstream.function;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import io.dstream.SerializableStreamAssets.SerBinaryOperator;

/**
 * An extension to {@link ValuesReducingFunction} which simply ensures the 
 * semantical correctness of the value (Iterable[V]). In a typical case where more then one
 * value existed in the first place it is the expectation that the combiner will 
 * produce an {@link Iterable}. However, for cases where there was only one value, combiner 
 * is never called, so this function ensures that such single value is wrapped into {@link Iterable}.
 *
 * @param  key type
 * @param  value type
 */
public class ValuesAggregatingFunction extends  ValuesReducingFunction {
	private static final long serialVersionUID = 5774838692658472433L;
	
	/**
	 * Constructs this function.
	 * 
	 * @param aggregator an aggregate function, used to resolve collisions between
     *                      values associated with the same key.
	 */
	@SuppressWarnings("rawtypes")
	public ValuesAggregatingFunction(SerBinaryOperator aggregator) {
		super(aggregator);
	}
	
	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected Object buildValue(Stream valuesStream){
		Object value = super.buildValue(valuesStream);
		if (!(value instanceof Iterable)){
			List l = new ArrayList();
			l.add((V) value);
			value = l;
		}
		return value;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy