java - Iterable Object conversion to another Iterable Object while iterating with Serialization support -


at beginning: did not found information might helpful on internet resources.

i have issue while trying convert iterated object new 1 when serialization support required. lets assume complicated process in application ap1 giving iterable interface large collection of objects i.e. baseclass type. part of data received these objects should used in application ap2 new object i.e. targetclass type. conversion between these 2 objects baseclass->targetclass quite complicated , needs performed before object sent target application. any ideas how achieve that?

some simplified example placed below presents issue. code runnable. 1 test "testfortargetdatasource_with_serialization()" of 4 test cases fails cause "java.lang.classcastexception: iterabletest$targetdatasource$1 cannot cast java.io.serializable".

the cause of exception quite obvious while in next() method i'm using plenty of different objects make conversion , these objects not serializable (you can refer public targetclass next() - in real example complex.).

public targetclass next() {     baseclass bcobj = dsiterator.next();     return new targetclass(bcobj.getfirstname(), bcobj.getsecondname()); } 

same issue hasnext() method of targetdatasource.

example:

import static org.junit.assert.assertequals; import static org.junit.assert.assertnotsame;  import java.io.serializable; import java.util.arrays; import java.util.iterator; import java.util.list;  import org.apache.commons.lang3.serializationutils; import org.junit.test;  public class iterabletest implements serializable {  @test public void testforstandarddatasource() {     datasource ds = new datasource();     iterator<baseclass> iterator = ds.getdata().iterator();     assertequals("b", iterator.next().getsecondname());     assertequals("c", iterator.next().getsecondname());     assertequals("d", iterator.next().getsecondname()); }  @test public void testfortargetdatasource() {     targetdatasource tdc = new targetdatasource();     iterator<targetclass> iter = tdc.getdata().iterator();     while (iter.hasnext()) {         system.out.println(iter.next().getfullname());     } }  @test public void testforstandarddatasource_with_serialization() {     datasource ds = new datasource();     iterable<baseclass> iter = ds.getdata();      byte[] bytes = serializationutils.serialize((serializable) iter);     @suppresswarnings("unchecked")     iterable<baseclass> iter2 = (iterable<baseclass>) serializationutils.deserialize(bytes);     assertnotsame(iter, iter2);      iterator<baseclass> iterator = iter2.iterator();     assertequals("b", iterator.next().getsecondname());     assertequals("c", iterator.next().getsecondname());     assertequals("d", iterator.next().getsecondname()); }  @test public void testfortargetdatasource_with_serialization() {     targetdatasource tdc = new targetdatasource();     iterable<targetclass> iter = tdc.getdata();      byte[] bytes = serializationutils.serialize((serializable) iter);     @suppresswarnings("unchecked")     iterable<targetclass> iter2 = (iterable<targetclass>) serializationutils.deserialize(bytes);     assertnotsame(iter, iter2);      iterator<targetclass> iterator = iter2.iterator();     assertequals("a b", iterator.next().getfullname());     assertequals("a c", iterator.next().getfullname());     assertequals("a d", iterator.next().getfullname()); }  class baseclass implements serializable {     private string firstname;     private string secondname;      public baseclass(string firstname, string secondname) {         super();         this.firstname = firstname;         this.secondname = secondname;     }      string getfirstname() {         return firstname;     }      string getsecondname() {         return secondname;     } }  class targetclass implements serializable {      private string fullname;      public targetclass() {     }      public targetclass(string firstname, string lastname) {         super();         this.fullname = firstname + " " + lastname;     }      public string getfullname() {         return fullname;     }  }  class datasource {     private final list<baseclass> data = arrays.aslist(new baseclass("a", "b"), new baseclass("a", "c"),             new baseclass("a", "d"));      public iterable<baseclass> getdata() {         return data;     } }  class targetdatasource {     final datasource ds = new datasource();      iterator<baseclass> dsiterator = ds.getdata().iterator();      public iterable<targetclass> getdata() {         return new iterable<targetclass>() {              public iterator<targetclass> iterator() {                 return new iterator<targetclass>() {                      public boolean hasnext() {                         return dsiterator.hasnext();                     }                      public targetclass next() {                         baseclass bcobj = dsiterator.next();                         return new targetclass(bcobj.getfirstname(), bcobj.getsecondname());                     }                      public void remove() {                         // nothing                     }                 };             }           };     } } 

}


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -