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

com.datatorrent.lib.util.BaseKeyValueOperator 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 com.datatorrent.lib.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.datatorrent.api.StreamCodec;
import com.datatorrent.lib.codec.JavaSerializationStreamCodec;

/**
 * This is an abstract operator that allows cloneKey and cloneValue to allow users to use mutable objects.
 * 

* @displayName Base Key Value * @category Algorithmic * @tags key value abstract * @since 0.3.2 */ public class BaseKeyValueOperator extends BaseKeyOperator { /** * By default an immutable object is assumed. Override if V is mutable * * @param v * @return v as is */ public V cloneValue(V v) { return v; } /** * By default an immutable object is assumed. Override if V is mutable * * @param kv * @return v as is */ public KeyValPair cloneKeyValPair(KeyValPair kv) { return kv; } /** * Creates a HashMap<K,V> by using cloneKey(K) and cloneValue(V) * * @param tuple to be cloned * @return HashMap<K,V> */ public HashMap cloneTuple(Map tuple) { if (tuple == null) { return null; } HashMap ret = new HashMap(tuple.size()); for (Map.Entry e : tuple.entrySet()) { ret.put(cloneKey(e.getKey()), cloneValue(e.getValue())); } return ret; } /** * Creates a HashMap(1) by using cloneKey(K) and cloneValue(V) * * @param key key to be cloned * @param val value to be cloned * @return HashMap(1) */ public HashMap cloneTuple(K key, V val) { HashMap ret = new HashMap(1); ret.put(cloneKey(key), cloneValue(val)); return ret; } /** * A codec to enable partitioning to be done by key */ public StreamCodec> getKeyValPairStreamCodec() { return new DefaultPartitionCodec(); } public static class DefaultPartitionCodec extends JavaSerializationStreamCodec> implements Serializable { /** * A codec to enable partitioning to be done by key */ @Override public int getPartition(KeyValPair o) { return o.getKey().hashCode(); } private static final long serialVersionUID = 201411031350L; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy