Example usage for org.apache.commons.lang3 SerializationUtils clone

List of usage examples for org.apache.commons.lang3 SerializationUtils clone

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationUtils clone.

Prototype

public static <T extends Serializable> T clone(final T object) 

Source Link

Document

Deep clone an Object using serialization.

This is many times slower than writing clone methods by hand on all objects in your object graph.

Usage

From source file:org.grouplens.grapht.reflect.QualifiersTest.java

@Test
public void testMatchValue() throws Exception {
    assertThat(Qualifiers.match(makeQual()).matches(null), equalTo(false));
    assertThat(Qualifiers.match(makeQual()).matches(makeQual()), equalTo(true));
    assertThat(Qualifiers.match(makeQual()).matches(makeVQual("foo")), equalTo(false));
    assertThat(Qualifiers.match(makeVQual("foo")).matches(makeVQual("foo")), equalTo(true));
    assertThat(Qualifiers.match(makeVQual("bar")).matches(makeVQual("foo")), equalTo(false));
    assertThat(Qualifiers.match(makeQual()), equalTo(Qualifiers.match(makeQual())));
    assertThat(Qualifiers.match(makeVQual("foo")), equalTo(Qualifiers.match(makeVQual("foo"))));
    assertThat(Qualifiers.match(makeVQual("foo")), not(equalTo(Qualifiers.match(makeVQual("bar")))));
    assertThat(SerializationUtils.clone(Qualifiers.match(makeQual())), equalTo(Qualifiers.match(makeQual())));
    assertThat(SerializationUtils.clone(Qualifiers.match(makeVQual("foo"))),
            equalTo(Qualifiers.match(makeVQual("foo"))));
}

From source file:org.grouplens.grapht.util.ClassProxyTest.java

/**
 * Serialize and deserialize a class proxy.
 * @param cls The class to serialize/*from  www . j a  v  a  2  s .  c  o  m*/
 * @return A class proxy that is the result of serializing a proxy for {@code cls} and
 * deserializing it.
 */
private ClassProxy roundTrip(Class<?> cls) throws IOException, ClassNotFoundException {
    ClassProxy proxy = ClassProxy.of(cls);
    return SerializationUtils.clone(proxy);
}

From source file:org.grouplens.grapht.util.ClassProxyTest.java

@Test
public void testEquals() {
    ClassProxy proxy = ClassProxy.of(String.class);
    assertThat(proxy.equals(null), equalTo(false));
    assertThat(proxy.equals(proxy), equalTo(true));
    ClassProxy equal = ClassProxy.of(String.class);
    ClassProxy unequal = ClassProxy.of(List.class);
    assertThat(proxy.equals(equal), equalTo(true));
    assertThat(proxy.equals(unequal), equalTo(false));

    ClassProxy serialized = SerializationUtils.clone(proxy);
    assertThat(proxy.equals(serialized), equalTo(true));

    // and test the hash code
    assertThat(equal.hashCode(), equalTo(proxy.hashCode()));
}

From source file:org.grouplens.grapht.util.ConstructorProxyTest.java

/**
 * Serialize and deserialize a constructor proxy.
 * @param fld The constructor to serialize
 * @return A constructor proxy that is the result of serializing a proxy for {@code fld} and
 * deserializing it.//from   w w  w.j a  va2s . c o  m
 */
private ConstructorProxy roundTrip(Constructor fld) throws IOException, ClassNotFoundException {
    ConstructorProxy proxy = ConstructorProxy.of(fld);
    return SerializationUtils.clone(proxy);
}

From source file:org.grouplens.grapht.util.FieldProxyTest.java

/**
 * Serialize and deserialize a field proxy.
 * @param fld The field to serialize/*from  ww  w.j  a  v a2 s  .co m*/
 * @return A field proxy that is the result of serializing a proxy for {@code fld} and
 * deserializing it.
 */
private FieldProxy roundTrip(Field fld) throws IOException, ClassNotFoundException {
    FieldProxy proxy = FieldProxy.of(fld);
    return SerializationUtils.clone(proxy);
}

From source file:org.grouplens.grapht.util.MethodProxyTest.java

/**
 * Serialize and deserialize a method proxy.
 * @param fld The method to serialize//w ww . j  a v a  2 s .  com
 * @return A method proxy that is the result of serializing a proxy for {@code fld} and
 * deserializing it.
 */
private MethodProxy roundTrip(Method fld) throws IOException, ClassNotFoundException {
    MethodProxy proxy = MethodProxy.of(fld);
    return SerializationUtils.clone(proxy);
}

From source file:org.grouplens.lenskit.collections.CompactableLongArrayListTest.java

@Test
public void testSerializeCompact() {
    CompactableLongArrayList list = new CompactableLongArrayList();
    list.add(42);/*  ww  w .  ja va2  s .c  o  m*/
    list.add(37);
    list.add(4);

    LongList copy = SerializationUtils.clone(list);
    assertThat(copy, contains(42L, 37L, 4L));
}

From source file:org.grouplens.lenskit.collections.CompactableLongArrayListTest.java

@Test
public void testSerializeFull() {
    CompactableLongArrayList list = new CompactableLongArrayList();
    list.add(42);//from   w ww .  ja v a 2  s .c o m
    list.add(37);
    list.add(Integer.MAX_VALUE + 7L);

    LongList copy = SerializationUtils.clone(list);
    assertThat(copy, contains(42L, 37L, Integer.MAX_VALUE + 7L));
}

From source file:org.grouplens.lenskit.data.dao.packed.BinaryIndexTableTest.java

@Test
public void testLimitedView() throws IOException {
    File file = folder.newFile();
    FileChannel chan = new RandomAccessFile(file, "rw").getChannel();
    BinaryIndexTableWriter writer = BinaryIndexTableWriter.create(BinaryFormat.create(), chan, 3);
    writer.writeEntry(12, new int[] { 0 });
    writer.writeEntry(17, new int[] { 1, 3 });
    writer.writeEntry(19, new int[] { 4, 5 });

    MappedByteBuffer buffer = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
    BinaryIndexTable tbl = BinaryIndexTable.fromBuffer(3, buffer);
    tbl = tbl.createLimitedView(2);//  w  w  w .  j  a  v  a  2 s  .c o m
    assertThat(tbl.getKeys(), hasSize(2));
    assertThat(tbl.getKeys(), contains(12L, 17L));
    assertThat(tbl.getEntry(12), contains(0));
    assertThat(tbl.getEntry(17), contains(1));
    assertThat(tbl.getEntry(19), nullValue());
    assertThat(tbl.getEntry(-1), nullValue());

    BinaryIndexTable serializedTbl = SerializationUtils.clone(tbl);
    assertThat(serializedTbl.getKeys(), hasSize(2));
    assertThat(serializedTbl.getKeys(), contains(12L, 17L));
    assertThat(serializedTbl.getEntry(12), contains(0));
    assertThat(serializedTbl.getEntry(17), contains(1));
    assertThat(serializedTbl.getEntry(19), nullValue());
    assertThat(serializedTbl.getEntry(-1), nullValue());
}

From source file:org.grouplens.lenskit.data.dao.packed.BinaryRatingDAOTest.java

@Test
public void testSerializedDAO() throws IOException {
    File file = folder.newFile("ratings.bin");
    BinaryRatingPacker packer = BinaryRatingPacker.open(file);
    try {// ww  w.j a  va  2  s. co m
        packer.writeRatings(ratings);
    } finally {
        packer.close();
    }

    BinaryRatingDAO dao = BinaryRatingDAO.open(file);
    BinaryRatingDAO clone = SerializationUtils.clone(dao);
    verifySimpleDAO(clone);
}