|
1 | 1 | # JavaEntityGenerator
|
2 | 2 | Python script to generate Java class entities
|
| 3 | + |
| 4 | +## Example |
| 5 | + |
| 6 | + python ./entity_gen.py --equals --toString --hashCode --get --set --class MyClass --builder "Optional<String>:testVariable" "Integer:id" |
| 7 | + |
| 8 | +Result: |
| 9 | + |
| 10 | + import java.util.Objects; |
| 11 | + import com.google.common.base.MoreObjects; |
| 12 | + public final class MyClass{ |
| 13 | + private Optional<String> testVariable; |
| 14 | + private Integer id; |
| 15 | + |
| 16 | + public final Optional<String> getTestVariable(){ |
| 17 | + return testVariable; |
| 18 | + } |
| 19 | + |
| 20 | + public final Integer getId(){ |
| 21 | + return id; |
| 22 | + } |
| 23 | + |
| 24 | + public final void setTestVariable(Optional<String> pTestVariable){ |
| 25 | + testVariable = pTestVariable; |
| 26 | + } |
| 27 | + |
| 28 | + public final void setId(Integer pId){ |
| 29 | + id = pId; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public boolean equals(Object object){ |
| 34 | + if(object == this){ |
| 35 | + return true; |
| 36 | + } |
| 37 | + if(object instanceof MyClass){ |
| 38 | + MyClass myclass = (MyClass) object; |
| 39 | + if(Objects.equals(testVariable, myclass.testVariable) && Objects.equals(id, myclass.id)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | + } |
| 45 | + @Override |
| 46 | + public int hashCode(){ |
| 47 | + return Objects.hash(testVariable, id); |
| 48 | + } |
| 49 | + @Override |
| 50 | + public String toString(){ |
| 51 | + return MoreObjects.toStringHelper(this) |
| 52 | + .add("testVariable", testVariable) |
| 53 | + .add("id", id).toString(); |
| 54 | + } |
| 55 | + private MyClass(Builder pBuilder){ |
| 56 | + testVariable = pBuilder.testVariable; |
| 57 | + id = pBuilder.id; |
| 58 | + } |
| 59 | + |
| 60 | + public static Builder builder(){ |
| 61 | + return new Builder(); |
| 62 | + } |
| 63 | + |
| 64 | + public static class Builder{ |
| 65 | + |
| 66 | + private Optional<String> testVariable = Optional.empty(); |
| 67 | + private Integer id; |
| 68 | + |
| 69 | + public Builder testVariable(Optional<String> pTestVariable){ |
| 70 | + testVariable = pTestVariable; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public Builder id(Integer pId){ |
| 75 | + id = pId; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public MyClass build(){ |
| 80 | + return new MyClass(this); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + } |
0 commit comments