Passing a collection of a subclass as collection of superclass - java

I have a POJO that looks more or less like this:
public class Action {
private String eventId;
private List<ActionArgument> arguments;
//action to perform when this action is done
private List<Action> onCompleteActions;
public Action() {
}
public Action(String eventId, List<ActionArgument> arguments, List<Action> onCompleteActions) {
this.eventId = eventId;
this.arguments = arguments;
this.onCompleteActions = onCompleteActions;
}
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public List<ActionArgument> getArguments() {
return arguments;
}
public void setArguments(List<ActionArgument> arguments) {
this.arguments = arguments;
}
public List<Action> getOnCompleteActions() {
return onCompleteActions;
}
public void setOnCompleteAction(List<Action> onCompleteActions) {
this.onCompleteActions = onCompleteActions;
}
}
and I have an extending class that looks like this:
public class UserDefinedAction extends Action {
//for reordering actions with the default actions
private String doBefore;
private String doAfter;
private String doAfterComplete;
public String getDoBefore() {
return doBefore;
}
public void setDoBefore(String doBefore) {
this.doBefore = doBefore;
}
public String getDoAfter() {
return doAfter;
}
public void setDoAfter(String doAfter) {
this.doAfter = doAfter;
}
public String getDoAfterComplete() {
return doAfterComplete;
}
public void setDoAfterComplete(String doAfterComplete) {
this.doAfterComplete = doAfterComplete;
}
}
Elsewhere I have a service I would like to do this:
...
UserDefinedAction udAction = new UserDefinedAction();
udAction.setOnCompleteAction(new ArrayList<UserDefinedAction>());
I thought this should work because a UserDefinedAction IS an Action because its extending it right?

List<UserDefinedAction> is not a subclass of List<Action> even if UserDefinedAction extends Action. In order you can pass a List<UserDefinedAction> to your service, change the UserDefinedAction#setOnCompleteAction method to receive a List<? extends Action>, now you can pass a new ArrayList<UserDefinedAction>().
More info:
Generics: Wildcards

Your UserDefinedAction may be an Action, but a List<Subclass> is not a List<Superclass>. As you have defined it, your setOnCompleteAction method must take a List<Action>, so it cannot accept a List<UserDefinedAction>.

Related

Dependency Injection: How to get access to the corresponding ProductView that executes the Command?

I want to execute commands for each product view. Consider 10 products views, and each of them can execute the PrintProductViewCommand. This command, takes in constructor a ProductView and prints its name. Since it is #Injectable, the container will create a new ProductView each time the command is created. The following example shows what I want to do:
public class InjectionTest {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AbstractModule() {
#Override
protected void configure() {
bind(ProductView.class);
bind(CommandExecutor.class);
bind(PrintProductViewNameCommand.class);
install(new FactoryModuleBuilder().implement(ProductView.class, ProductView.class)
.build(ProductViewFactory.class));
}
});
List<ProductView> productViews = new ArrayList<>();
ProductViewFactory factory = injector.getInstance(ProductViewFactory.class);
for (int i = 0; i < 10; i++) {
productViews.add(factory.create("Name: " + String.valueOf(i)));
}
System.out.println("Done creating");
//Now sometime in future, each product view calls print method
productViews.forEach(ProductView::print);
}
private static interface ProductViewFactory {
ProductView create(String name);
}
private static class ProductView {
private String name; //simulate a property
private CommandExecutor executor;
public ProductView() {
//Guice throws exception when this is missing
//Probably because it is being asked in PrintProductViewCommand
}
#AssistedInject
public ProductView(#Assisted String name, CommandExecutor executor) {
this.name = name;
this.executor = executor;
}
public String getName() {
return name;
}
//assume some time product view it self calls this method
public void print() {
executor.execute(PrintProductViewNameCommand.class);
}
}
#Singleton
private static class CommandExecutor {
#Inject
private Injector injector;
public void execute(Class<? extends Command> cmdType) {
injector.getInstance(cmdType).execute();
}
}
private static class PrintProductViewNameCommand implements Command {
private ProductView view;
#Inject
public PrintProductViewNameCommand(ProductView view) {
this.view = view;
}
#Override
public void execute() {
//Want to print "Name: something" here
System.out.println(view.getName());
}
}
private static interface Command {
void execute();
}
}
This problem is solved if I add a parameter to Command interface, and make it Command<T>. Then CommandExecutor will have this method:
public <T> void execute(Class<? extends Command<T>> cmdType, T parameter) {
injector.getInstance(cmdType).execute(parameter);
}
So, my PrintProductViewNameCommand is now class PrintProductViewNameCommand implements Command<ProductView>, and in product view :
public void print() {
executor.execute(PrintProductViewNameCommand.class,this);
}
However, the Command Pattern has no parameter in execute(). I have also seen somewhere that adding a parameter is an anti-pattern.
Of course, the command is simple. Assume that the command has other dependencies too, like Services etc.
Is there a way I can achieve it? Perhaps I am doing something wrong, probably with the whole DI situation.
When not using dependency injection, I would do something like this:
ProductView view = new ProductView();
Command command = new PrintProductViewNameCommand(view);
view.setPrintCommand(command);
But how to it while using DI?
So this works, although I'm not sure if it's 100% what you want to do.
public class InjectionTest {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AbstractModule() {
#Override
protected void configure() {
bind(CommandExecutor.class);
bind(ProductView.class);
install(new FactoryModuleBuilder()
.implement(ProductView.class, ProductView.class)
.build(ProductViewFactory.class));
install(new FactoryModuleBuilder()
.implement(PrintProductViewNameCommand.class, PrintProductViewNameCommand.class)
.build(PrintProductViewNameCommand.Factory.class));
}
});
ProductViewFactory factory = injector.getInstance(ProductViewFactory.class);
List<ProductView> productViews = new ArrayList<>();
for (int i = 0; i < 10; i++) {
productViews.add(factory.create("Name: " + i));
}
System.out.println("Done creating");
//Now sometime in future, each product view calls print method
productViews.forEach(ProductView::print);
}
private interface ProductViewFactory {
ProductView create(String name);
}
private static class ProductView {
private String name;
private CommandExecutor executor;
private PrintProductViewNameCommand printProductViewNameCommand;
#AssistedInject
public ProductView(#Assisted String name, PrintProductViewNameCommand.Factory printProductViewNameCommandFactory, CommandExecutor executor) {
this.name = name;
this.executor = executor;
this.printProductViewNameCommand = printProductViewNameCommandFactory.create(this);
}
public ProductView() {}
public String getName() {
return name;
}
//assume some time product view it self calls this method
public void print() {
executor.execute(printProductViewNameCommand);
}
}
#Singleton
private static class CommandExecutor {
public void execute(Command command) {
command.execute();
}
}
private static class PrintProductViewNameCommand implements Command {
private final ProductView view;
#AssistedInject
public PrintProductViewNameCommand(#Assisted ProductView view) {
this.view = view;
}
static interface Factory {
PrintProductViewNameCommand create(ProductView productView);
}
#Override
public void execute() {
//Want to print "Name: something" here
System.out.println(view.getName());
}
}
private static interface Command {
void execute();
}
}
Basically what you're running into is a cyclic dependency problem (https://github.com/google/guice/wiki/CyclicDependencies#use-factory-methods-to-tie-two-objects-together) that's also exasperated by a bit by the fact that you have an additional AssistedInject in the ProductView.
By the way I'm using Guice 3 in this example.

Creating a generic function in Java for builders of different implementations of interface

public interface A extends C {
String getCh();
String getId();
String getReview();
}
public interface B extends C {
String getCh();
String getId();
String getReview();
}
#Data
#Builder
public class AImpl implements A{
private String ch;
private String id;
private String review;
}
#Data
#Builder
public class BImpl implements B{
private String ch;
private String id;
private String review;
}
so now to use the builders of these I do:
return AImpl.builder()
.ch("ch")
.id("id")
.review("somerview");
For B I do:
return BImpl.builder()
.ch("ch1")
.id("id1")
.review("some new review");
Is there a way where I can make this builder part into a function? I dont like the idea of repeating the same code again. Like where I can pass id channel and review in a function and I can the object?
Disclaimer: I have never really dealt with builders so there might be a really much better option :D
This approach writes builders for each interface individually.
This does require that the interfaces provide a setter method.
Using generics, the methods of the RootBuilder and BaseABuilder return an instance of the ImplABuilder so that the chain can continue properly.
This is a very simple implementation of the Thistype generic which in other languages exists by default. This implementation also relies on casting to the actual Thistype but if you set the generics properly, that shouldnt be an issue.
public class Test
{
public static void main(String[] args)
{
ImplA implA = ImplA
.builder()
.id("id")
.description("description")
.valueA("a")
.build();
}
}
public interface Root
{
String getId();
void setId(String id);
String getDescription();
void setDescription(String description);
}
public class RootBuilder<Thistype extends RootBuilder<Thistype, Instance>, Instance extends Root>
{
protected final Instance object;
RootBuilder(Instance object)
{
this.object = object;
}
public Thistype id(String value)
{
object.setId(value);
return (Thistype)this;
}
public Thistype description(String value)
{
object.setDescription(value);
return (Thistype)this;
}
public Instance build()
{
return object;
}
}
public interface BaseA extends Root
{
String getValueA();
void setValueA(String valueA);
}
public class BaseABuilder<Thistype extends BaseABuilder<Thistype, Instance>, Instance extends BaseA> extends RootBuilder<Thistype, Instance>
{
protected Instance object;
BaseABuilder(Instance object)
{
super(object);
}
public Thistype valueA(String value)
{
object.setValueA(value);
return (Thistype)this;
}
}
public interface BaseB extends Root
{
String getValueB();
void setValueB(String valueB);
}
public interface BaseC extends Root
{
String getValueC();
void setValueC(String valueC);
}
public final class ImplA implements BaseA
{
private String id;
private String description;
private String valueA;
private ImplA() { }
public static ImplABuilder builder()
{
return new ImplABuilder(new ImplA());
}
private static class ImplABuilder extends BaseABuilder<ImplABuilder, ImplA> // assuming ImplA is final
{
ImplABuilder(ImplA object)
{
super(object);
}
// additional methods for ImplA class
}
}

Generifying array type such as Object[].class

I am trying to generify following class:
public class FooService {
private Client client;
public Foo get(Long id) {
return client.get(id, Foo.class);
}
public List<Foo> query() {
return Arrays.asList(client.get(Foo[].class));
}
}
Everything is alright except Foo[].class:
public abstract class BaseService<T, I> {
private Client client;
private Class<T> type;
public BaseService(Class<T> type) {
this.type = type;
}
public T get(I id) {
return client.get(id, type);
}
public List<T> query() {
return Arrays.asList(client.get(/* What to pass here? */));
}
How can I solve this issue without passing Foo[].class in the constructor like I have done with Foo.class?
Java lacks facilities to obtain an array class from element class directly. A common work-around is to obtain the class from a zero-length array:
private Class<T> type;
private Class arrType;
public BaseService(Class<T> type) {
this.type = type;
arrType = Array.newInstance(type, 0).getClass();
}
You can now pass arrType to the client.get(...) method.
Why don't you do something like this:
public class Client<T> {
T instance;
T get(long id) {
return instance;
}
List<T> get(){
return new ArrayList<>();
}
}
class FooService<T> {
private Client<T> client;
public T get(Long id) {
return client.get(id);
}
public List<T> query() {
return client.get();
}
}

On reload of page, mvp4g history mechanism fails

I have implemented a history mechanism for my mvp4g project. When I traverse through the pages, I can see the url also getting changed. But on reload of any page other than home page, always home page gets displayed instead of the desired page?
This is my implementation:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return eventName;
}
public String convertToToken(String eventName, String name, String type) {
return name;
}
public boolean isCrawlable() {
return false;
}
}
and event bus related code :
#Events(startPresenter=PageOnePresenter.class,historyOnStart=true)
public interface CustomEventBus extends EventBusWithLookup {
#Start
#Event(handlers = PageOnePresenter.class)
void start();
#InitHistory
#Event(handlers = PageOnePresenter.class)
void init();
#Event(handlers = PageTwoPresenter.class, name = "page2", historyConverter = CustomHistoryConverter.class)
void getPageTwo();
#Event(handlers = PageThreePresenter.class, name = "page3", historyConverter=CustomHistoryConverter.class)
void getPageThree();
#Event(handlers=PageOnePresenter.class, name = "page1", historyConverter=CustomHistoryConverter.class)
void getPageOne();
#Event(handlers=PageOnePresenter.class)
void setPageTwo(HistoryPageTwoView view);
#Event(handlers=PageOnePresenter.class)
void setPageThree(HistoryPageThreeView view);
}
The HistoryConverter needs to be improved.
In fact, that the event has no parameter, you should return an empty string. Update the HistoryConverter that it looks like that:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
// TODO handle the param in cases where you have more than one parameter
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return "";
}
public String convertToToken(String eventName, String name, String type) {
return name - "-!-" type;
}
public boolean isCrawlable() {
return false;
}
}
Hope that helps.

JSON parse with Objectmapper JAVA

This is the JSON string:
{
"d":{
"results":[
{
"__metadata":{
"uri":"http://blabla1",
"type":"type1"
},
"Synonym":"miami"
},
{
"__metadata":{
"uri":"http://blabla2",
"type":"type2"
},
"Synonym":"florida"
}
]
}
}
This is the code:
public class Test{
#JsonIgnoreProperties(ignoreUnknown = true)
public static class d {
public List<Results> results;
public d() {
results = new ArrayList<Results>();
}
public static class Results {
public Metadata __metadata;
public String Synonym;
public Results() {}
}
public static class Metadata {
public String uri;
public String type;
public Metadata() {}
}
}
}
With the following mapper:
ObjectMapper mapper = new ObjectMapper();
Test.d readValue = mapper.readValue(jsonString, Test.d.class);
for (Test.d.Results k : readValue.results) {
System.out.println("synonym: "+k.Synonym);
}
It gives me no error, just an empty arraylist of results...
p.s. i have made a lot of changes in between time, sorry for the inconvenience
you must create an object that fits with the jSon answer, something like this (not tested):
class d {
public List<Results> results;
public d() {
}
}
class Results {
public Metadata metadata;
public String synonym;
public Results() {
}
}
class Metadata {
public String uri;
public String type;
public Metadata() {
}
}
Hope it helps!
I have managed to solve it.
i forgot to make setters and getters for class 'd'.
public class Test {
private d d;
public d getD() {return d;}
public void setD(d d) {this.d = d;}
public static class d{
private List<Results> results;
public List<Results> getResults() {return results;}
public void setResults(List<Results> results) {this.results = results;}
}
public static class Results {
public Metadata __metadata;
public String Synonym;
}
public static class Metadata {
private String uri;
private String type;
public String getUri() {return uri;}
public void setUri(String uri) {this.uri = uri;}
public String getType() {return type;}
public void setType(String type) {this.type = type;}
}
}
This is the map:
Test test = mapper.readValue(json, KeyPhrase.class);
System.out.println("cp");
for(Test.Results res : test.getD().getResults()){
System.out.println(res.Synonym);
}

Categories

Resources