// Path: src/main/java/com/urbanairship/datacube/backfill/CollectionWritable.java
// public class CollectionWritable implements Writable {
//     private Collection<? extends Writable> collection;
//     private Class<? extends Writable> elementClass;
//     
//     public CollectionWritable() { } // No-op constructor is required for deserializing
//     
//     public CollectionWritable(Class<? extends Writable> elementClass, 
//             Collection<? extends Writable> collection) {
//         this.elementClass = elementClass;
//         this.collection = collection;
//     }
// 
//     @SuppressWarnings("unchecked")
//     @Override
//     public void readFields(DataInput in) throws IOException {
//         try {
//             String valueClassName = in.readUTF();
//             elementClass = (Class<? extends Writable>)Class.forName(valueClassName);
//             if(!Writable.class.isAssignableFrom(elementClass)) {
//                 throw new RuntimeException("valueClass is not writable: " + valueClassName);
//             }
//             int numElements = in.readInt();
//             List<Writable> newList = new ArrayList<Writable>(numElements);
//             for(int i=0; i<numElements; i++) {
//                 Writable instance = elementClass.newInstance();
//                 instance.readFields(in);
//                 newList.add(instance);
//             }
//             collection = newList;
//         } catch (Exception e) {
//             throw new RuntimeException(e);
//         }
//     }
// 
//     @Override
//     public void write(DataOutput out) throws IOException {
//         out.writeUTF(elementClass.getName());
//         out.writeInt(collection.size());
//         for(Writable writable: collection) {
//             writable.write(out);
//         }
//     }
//     
//     public Collection<? extends Writable> getCollection() {
//         return collection;
//     }
// }


import com.urbanairship.datacube.backfill.CollectionWritable;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
/*
Copyright 2012 Urban Airship and Contributors
*/

package com.urbanairship.datacube;



public class CollectionWritableTest {
    @Test
    public void basicTest() throws Exception {
        List<IntWritable> list = new ArrayList<IntWritable>();
        list.add(new IntWritable(1));
        list.add(new IntWritable(2));
        list.add(new IntWritable(3));

        CollectionWritable c = new CollectionWritable(IntWritable.class, list);

        DataOutputBuffer buf = new DataOutputBuffer();
        c.write(buf);

        DataInputBuffer in = new DataInputBuffer();
        in.reset(buf.getData(), buf.getLength());
// Path: src/main/java/com/urbanairship/datacube/backfill/CollectionWritable.java
// public class CollectionWritable implements Writable {
//     private Collection<? extends Writable> collection;
//     private Class<? extends Writable> elementClass;
//     
//     public CollectionWritable() { } // No-op constructor is required for deserializing
//     
//     public CollectionWritable(Class<? extends Writable> elementClass, 
//             Collection<? extends Writable> collection) {
//         this.elementClass = elementClass;
//         this.collection = collection;
//     }
// 
//     @SuppressWarnings("unchecked")
//     @Override
//     public void readFields(DataInput in) throws IOException {
//         try {
//             String valueClassName = in.readUTF();
//             elementClass = (Class<? extends Writable>)Class.forName(valueClassName);
//             if(!Writable.class.isAssignableFrom(elementClass)) {
//                 throw new RuntimeException("valueClass is not writable: " + valueClassName);
//             }
//             int numElements = in.readInt();
//             List<Writable> newList = new ArrayList<Writable>(numElements);
//             for(int i=0; i<numElements; i++) {
//                 Writable instance = elementClass.newInstance();
//                 instance.readFields(in);
//                 newList.add(instance);
//             }
//             collection = newList;
//         } catch (Exception e) {
//             throw new RuntimeException(e);
//         }
//     }
// 
//     @Override
//     public void write(DataOutput out) throws IOException {
//         out.writeUTF(elementClass.getName());
//         out.writeInt(collection.size());
//         for(Writable writable: collection) {
//             writable.write(out);
//         }
//     }
//     
//     public Collection<? extends Writable> getCollection() {
//         return collection;
//     }
// }


// Path: src/test/java/com/urbanairship/datacube/CollectionWritableTest.java
import com.urbanairship.datacube.backfill.CollectionWritable;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/*
Copyright 2012 Urban Airship and Contributors
*/

package com.urbanairship.datacube;



public class CollectionWritableTest {
    @Test
    public void basicTest() throws Exception {
        List<IntWritable> list = new ArrayList<IntWritable>();
        list.add(new IntWritable(1));
        list.add(new IntWritable(2));
        list.add(new IntWritable(3));

        CollectionWritable c = new CollectionWritable(IntWritable.class, list);

        DataOutputBuffer buf = new DataOutputBuffer();
        c.write(buf);

        DataInputBuffer in = new DataInputBuffer();
        in.reset(buf.getData(), buf.getLength());
        CollectionWritable roundTripped = new CollectionWritable();