Login.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package godau.fynn.dsbdirect;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4. import javax.annotation.Nullable;
  5. public class Login {
  6. private String id, pass;
  7. @Nullable private String displayName;
  8. public Login(String id, String pass) {
  9. this.id = id;
  10. this.pass = pass;
  11. }
  12. public Login(String id, String pass, String displayName) {
  13. this.id = id;
  14. this.pass = pass;
  15. this.displayName = displayName;
  16. }
  17. /**
  18. * Deserialize a serialized login in this format: {@code "display name" id / pass}
  19. * @param serialized A String in the above format
  20. */
  21. public Login(String serialized) {
  22. if (serialized.startsWith("\"")) {
  23. String[] split = serialized.split("\"");
  24. displayName = split[1];
  25. serialized = serialized.replace("\"" + displayName + "\" ", "");
  26. }
  27. String[] split = serialized.split(" / ");
  28. // Check whether amount is valid
  29. if (split.length < 2) {
  30. throw new IllegalArgumentException("serialized string does not contain at least one \" / \" divider");
  31. }
  32. pass = "";
  33. for (String s :
  34. split) {
  35. if (id == null) id = s;
  36. else pass = pass + s;
  37. }
  38. }
  39. /**
  40. * @return The name that this login should be displayed as
  41. */
  42. public String getDisplayName() {
  43. if (displayName != null) {
  44. return displayName;
  45. } else {
  46. return id;
  47. }
  48. }
  49. /**
  50. * @return Whether {@link #displayName} is not null
  51. */
  52. public boolean hasDisplayName() {
  53. return displayName != null;
  54. }
  55. public void setDisplayName(String displayName) {
  56. this.displayName = displayName;
  57. }
  58. public String getId() {
  59. return id;
  60. }
  61. /**
  62. * @return {@code true} if {@link #id} and {@link #pass} both have a length larger than 0, otherwise {@code false}
  63. */
  64. public boolean isNonZeroLength() {
  65. return id.length() > 0 && pass.length() > 0;
  66. }
  67. /**
  68. * Inserts credentials into query json body
  69. * @param body Body to insert credentials into
  70. */
  71. public void put(JSONObject body) throws JSONException {
  72. body.put("UserId", id)
  73. .put("UserPw", pass);
  74. }
  75. /**
  76. * Serialize this login to a String
  77. * @return The login serialized to the format {@code "displayName" id / pass} or {@code id / pass}
  78. */
  79. public String serialize() {
  80. if (displayName == null) {
  81. return id + " / " + pass;
  82. } else {
  83. return "\"" + displayName + "\" " + id + " / " + pass;
  84. }
  85. }
  86. /**
  87. * @return {@code true} if object is a login with the same id, otherwise {@code super.equals(obj)}.
  88. */
  89. @Override
  90. public boolean equals(Object obj) {
  91. if (obj.getClass().equals(Login.class)) {
  92. return ((Login) obj).getId().equals(id);
  93. } else {
  94. return super.equals(obj);
  95. }
  96. }
  97. }