fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.math.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. SomeClass someClass = new SomeClass();
  14. EntityOne one = new EntityOne();
  15. MyResult myResult = someClass.getSomething(one, 1L);
  16. System.out.println(myResult.getAdvice());
  17.  
  18. myResult = someClass.getSomething(null, 1L);
  19. System.out.println(myResult.getAdvice());
  20. }
  21. }
  22.  
  23. class SomeClass {
  24.  
  25. //@EJB
  26. SomeEJB someEJB = new SomeEJB();
  27.  
  28. public MyResult getSomething(EntityOne entityOne, Long someId) {
  29. BigDecimal value = BigDecimal.ZERO;
  30. Date date = null;
  31. boolean isOne = false;
  32. if (entityOne != null) {
  33. value = entityOne.getOneValue();
  34. date = entityOne.getOneDate();
  35. isOne = true; // i need to know if the values are from 'one' or 'x'
  36. } else {
  37. EntityX entityX = this.someEJB.findInDatabase(someId);
  38. value = entityX.getSomeValue();
  39. date = entityX.getDateHour();
  40. }
  41.  
  42. return new MyResult(value, date, isOne);
  43. }
  44.  
  45. }
  46.  
  47. class EntityOne {
  48. public BigDecimal getOneValue() {
  49. return new BigDecimal("1.00");
  50. }
  51.  
  52. public Date getOneDate() {
  53. return new Date();
  54. }
  55. }
  56.  
  57.  
  58. class MyResult {
  59. private BigDecimal value;
  60. private Date date;
  61. private boolean isOne;
  62. public MyResult(BigDecimal value, Date data, boolean isOne){
  63. this.value = value;
  64. this.date = date;
  65. this.isOne = isOne;
  66. }
  67. public String getAdvice() {
  68. String answer = this.isOne ? "yes" : "no";
  69. return "Hi, user! This value and date is from One? " + answer + ". So, be sure about that when you do your stuff.";
  70. }
  71. }
  72.  
  73. class SomeEJB {
  74. public EntityX findInDatabase(Long id) {
  75. return new EntityX();
  76. }
  77. }
  78.  
  79. class EntityX {
  80. public BigDecimal getSomeValue() {
  81. return new BigDecimal("2.00");
  82. }
  83.  
  84. public Date getDateHour() {
  85. return new Date();
  86. }
  87. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Hi, user! This value and date is from One? yes. So, be sure about that when you do your stuff.
Hi, user! This value and date is from One? no. So, be sure about that when you do your stuff.