apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tweise <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: APEXMALHAR-1984 refactor out o...
Date Thu, 28 Jan 2016 06:14:51 GMT
Github user tweise commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51083964
  
    --- Diff: library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java ---
    @@ -0,0 +1,153 @@
    +/**
    + * 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.ByteArrayOutputStream;
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import org.apache.commons.io.IOUtils;
    +
    +import com.esotericsoftware.kryo.Kryo;
    +import com.esotericsoftware.kryo.io.Input;
    +import com.esotericsoftware.kryo.io.Output;
    +
    +/**
    + *
    + * A Kryo Clone Util class that clone object by using Kryo serializer and deserializer
    + * The class has static method that are can be used directly to clone one object
    + * Or it can be used as util instance to clone as many objects as you need from the one
source object
    + *
    + * @since 3.4.0
    + */
    +public class KryoCloneUtils<T>
    +{
    +
    +  /**
    +   * Reusable Kryo object as deserializer
    +   */
    +  private final Kryo kryo;
    +
    +  /**
    +   * Reusable binary data for object that would be deserialized from
    +   */
    +  private final byte[] bin;
    +
    +  /**
    +   * The class of the object
    +   */
    +  private final Class<T> clazz;
    +
    +  private KryoCloneUtils(Kryo kryo, T t)
    +  {
    +    this.kryo = kryo;
    +    ByteArrayOutputStream bos = null;
    +    Output output = null;
    +    try {
    +      bos = new ByteArrayOutputStream();
    +      output = new Output(bos);
    +      kryo.writeObject(output, t);
    +      output.close();
    +      bin = bos.toByteArray();
    +    } finally {
    +      IOUtils.closeQuietly(output);
    +      IOUtils.closeQuietly(bos);
    +    }
    +    clazz = (Class<T>)t.getClass();
    +    kryo.setClassLoader(clazz.getClassLoader());
    +  }
    +
    +  /**
    +   * Clone from the binary data of the source object
    +   * @return
    +   */
    +  public T getClone()
    +  {
    +    try (Input input = new Input(bin)) {
    +      return kryo.readObject(input, clazz);
    +    }
    +  }
    +
    +  /**
    +   * Clone array of objects from source object
    +   * @param num
    +   * @return
    +   */
    +  public T[] getClones(int num)
    +  {
    +    List<T> result = new ArrayList<>();
    +    for (int i = 0; i < num; i++) {
    +      result.add(getClone());
    +    }
    +    return (T[])result.toArray();
    +  }
    +
    +
    +
    +  /**
    +   * Clone object by serializing and deserializing using Kryo.
    +   * Note this is different from using {@link Kryo#copy(Object)}, which will attempt
to also clone transient fields.
    +   *
    +   * @param kryo kryo object used to clone objects
    +   * @param src src object that copy from
    +   * @return
    +   */
    +  public static <SRC> SRC cloneByKryo(Kryo kryo, SRC src)
    --- End diff --
    
    "ByKryo" is redundant


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message