apex-dev mailing list archives

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

    https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51163070
  
    --- 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<>();
    --- End diff --
    
    ```
    public class Gen<E> {
        private E a[];
    
        public Gen(int n) {
            a = (E[])new Object[n];
        }
    
        E[] foo(int n) {
            E[] result = (E[]) new Object[n];
            return result;
        }
    
        public static void main(String[] args) {
           Gen<Byte> g = new Gen<>(10);
        }
    }
    ```
    
    That code compiles and runs.



---
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