Guest User

Untitled

a guest
Jan 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. public interface Function {
  2. public void calculate(long t);
  3. }
  4.  
  5. public class ConstantFunction implements Function {
  6. private double constant;
  7.  
  8. @Override
  9. public void calculate(long t) {
  10. // ...
  11. }
  12. }
  13.  
  14. public class LinearFunction implements Function {
  15. private double slope;
  16.  
  17. private double yIntercept;
  18.  
  19. @Override
  20. public void calculate(long t) {
  21. // ...
  22. }
  23. }
  24.  
  25. <myapp>
  26. <function type="ConstantFunction>
  27. <!-- ... -->
  28. </function>
  29. <function type="LinearFunction>
  30. <!-- ... -->
  31. </function>
  32. </myapp>
  33.  
  34. XStream oxmapper = new XStream();
  35. oxmapper.alias("myapp", MyApp.class);
  36. oxmapper.alias("function", ???);
  37.  
  38. public class FunctionFactory implements Function {
  39. private double constant;
  40. private double slope;
  41. private double yIntercept;
  42.  
  43. private Class<? extends Function> concreteClass;
  44.  
  45. @Override
  46. public void calculate(long t) {
  47. // Do nothing. This class is a workaround to limitations with XStream.
  48. return;
  49. }
  50. }
  51.  
  52. oxampper.alias("function", FunctionFactory.class);
  53. oxmapper.aliasField("function", "type", "concreteClass");
  54.  
  55. XStream oxmapper = getConfiguredMapper();
  56. MyApp app = oxmapper.fromXml("<myapp>...</myapp>");
  57.  
  58. FunctionFactory factory = app.getFunction();
  59. Function concretion = factory.getConcreteClass();
  60.  
  61. app.setFunction(concretion);
Add Comment
Please, Sign In to add comment