i m beginner in java, so need your professional suggestions.
Our problem now:
we have 2+ data sources, like DB, files, libs.
we need to create like orm hibernate objects, but with all data of all this sources.
to update such objects, there must be used only one main source, other sources only for retreiving the data.
For example i need a Foo object, that composite Foo from DB as hibernate entity, another Foo from files and maybe more. By initialization i do new Foo(mainDBCommitObject, justforDataObject, justforDataObject2)
Foo.addId("value") means only mainDBFooObject.setId() is called.
Foo.getAllIds() means the ids are loaded from all the data sources.
For now, i use Foo interface, to implement CompositeFoo, this composite Foo uses other Foo objects.
If i want not only Foo, but Bar, i need to implement new CompositeBar, with its own methods. But all i need is to make sure what methods are only for retreiving and what for changing information.
All the methods of retreiving info should just add result of every containing_object_method_call to collection and return it.
all the change methods, should just call the main objects change method.
now the question: is it possible to create such magic generic class?
here some example for more understanding:
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public abstract class Foo {
abstract Set<String> getIds();
abstract void addId(String id);
}
//data source from file, will be used for retreiving information only
class FooFromFile extends Foo {
@Override
Set<String> getIds() {
Set<String> retSet = new HashSet<>();
retSet.add("IdFromFile");
return retSet;//in real example : loaded from file
}
@Override
void addId(String id) {
//changes file
}
}
//data source from DB will be use as main data source
class FooFromDB extends Foo {
@Override
Set<String> getIds() {
//is a hibernate object proxy
Set<String> retSet = new HashSet<>();
retSet.add("Ids From database");
return retSet;//in real example : loaded from db, hibernateFoo.getIds();
}
@Override
void addId(String id) {
//saves id to the hibernate/db
//hibernateFoo.addId(id);
//session.save(hibernateFoo);
}
}
//gets info from all, updates only main data source
class CompositeFoo extends Foo {
private Foo mainFoo;
private Set<Foo> others;
public CompositeFoo(Foo mainSource, Foo ...otherSources ) {
mainFoo = mainSource;
others = new HashSet<>();
Collections.addAll(others, otherSources);
}
@Override
Set<String> getIds() {
HashSet<String> result = new HashSet<>();
//load from main source
result.addAll(mainFoo.getIds());
//and from other sources
for (Foo otherFoo : others ) {
result.addAll(otherFoo.getIds());
}
return result;
}
@Override
void addId(String id) {
//uses only main source
mainFoo.addId(id);
}
}
this magic i want to realize:
class MagicComposite<Clazz> {
private Clazz mainFoo;
private Set<Clazz> others;
public MagicComposite(Clazz mainSource, Clazz... otherSources) {
mainFoo = mainSource;
others = new HashSet<>();
Collections.addAll(others, otherSources);
}
//other methods should be generated from Clazz
public static void main(String[] args) {
FooFromDB dbFooMain = new FooFromDB();
FooFromFile fileFoo = new FooFromFile();
MagicComposite<Foo> magic = new MagicComposite<>(dbFooMain, fileFoo);
//this methods should be generated
magic.add("new id"); //adds to dbFooMain only
magic.getIds(); //gets from all sources
}
}
class Bar{
@Override
@Magic(Type = retrieveInformationOnly)
Set<String> getIds() {return null; }
@Override
@Magic(Type = addInformation)
void addId(String id) {}
}
The implementation could be something else. Here is just to clarify what i need. Is it possible to use some generation function? somthing like
Foo compFoo = Proxy.createComposite(Foo.class, mainSource, otherSource);
to generate such behavior composite object depends on parameters and generic type, by using annotations over Foo methods?
Aucun commentaire:
Enregistrer un commentaire