java - local::reference passing along a reference to local -
i have constructor looks follows:
public sqlreader(schema schema) { super(schema::readrow,schema::initialize); }
the main part of code follows:
schema schema = new schema(schemadefinition); sqlreader sqlsource = new sqlreader(schema); schema.groupby(sqlsource,column.column("market"),column.sum("sales")); result = schema.execute();
this fictional piece of code allow me explain dealing here lazy execution, schema.groupby
modify schema, , sqlreader
depends on original schema.
the problem here using schema::readrow
, pass along actual reference schema
member reference , not copy of it, translates anonymous inner class pass along schema
.
how can efficiently improved?
a first attempt might follows:
public sqlreader(schema schema) { super(schema.clone()::readrow,schema.clone()::initialize); }
overhead obvious: have created twice clone. might come solution, instead of in api (sqlreader), in custom coded part:
sqlreader sqlsource = new sqlreader(schema.clone());
issue here require user of api aware of issue, might complex of topic user understand or remember.
so come new approach: using factory, builder, or static creator:
public static create(schema schema) { schema schemacopy = schema.clone(); return new sqlreader(schemacopy::readrow,schemacopy::initialize); }
it seems such common issue parallelized streams, lambda, , method references, there must more elegant way address this?
Comments
Post a Comment