1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.util.Comparator ; 22 23 public class PermissionModel extends GenericModel implements Comparator { 24 private int type; 25 private String userLogin; 26 private Integer projectId; 27 private Integer userId; 28 29 public PermissionModel() { 30 } 31 32 public PermissionModel(Integer projectId, int type) { 33 this.projectId = projectId; 34 this.type = type; 35 } 36 37 public PermissionModel(Integer projectId, int type, String userLogin) { 38 this(projectId, type); 39 this.userLogin = userLogin; 40 } 41 42 public PermissionModel(Integer projectId, int type, String userLogin, Integer userId) { 43 this(projectId, type, userLogin); 44 this.userId = userId; 45 } 46 47 public int getPermissionType() { 48 return type; 49 } 50 51 public void setPermissionType(int value) { 52 type = value; 53 } 54 55 public String getUserLogin() { 56 return userLogin; 57 } 58 59 public void setUserLogin(String value) { 60 userLogin = value; 61 } 62 63 public Integer getUserId() { 64 return userId; 65 } 66 67 public void setUserId(Integer value) { 68 userId = value; 69 } 70 71 public Integer getProjectId() { 72 return projectId; 73 } 74 75 public void setProjectId(Integer value) { 76 projectId = value; 77 } 78 79 public String toString() { 80 return "User: " + getUserLogin() + "(" + getUserId() + ") Project: " + getProjectId() + " Permission: " + getPermissionType(); 81 } 82 83 public int compare(Object a, Object b) { 84 return this.new CompareByPermission().compare(a, b); 85 } 86 87 public boolean equals(Object obj) { 88 return this.new CompareByPermission().equals(obj); 89 } 90 91 public int hashCode() { 92 return this.new CompareByPermission().hashCode(); 93 } 94 95 public class CompareByPermission implements Comparator { 96 public int compare(Object a, Object b) { 97 if(! (a instanceof PermissionModel) || ! (b instanceof PermissionModel)) { 98 throw new ClassCastException (); 99 } 100 101 PermissionModel ma = (PermissionModel) a; 102 PermissionModel mb = (PermissionModel) b; 103 104 if(ma.getPermissionType() == mb.getPermissionType()) { 105 return 0; 106 } else if(ma.getPermissionType() > mb.getPermissionType()) { 107 return 1; 108 } else { 109 return -1; 110 } 111 } 112 113 public boolean equals(Object obj) { 114 if(! (obj instanceof PermissionModel)) { 115 return false; 116 } 117 118 try { 119 PermissionModel mo = (PermissionModel) obj; 120 if(PermissionModel.this.getProjectId() == null || mo.getProjectId() == null) { 121 return false; 122 } 123 124 if(PermissionModel.this.getPermissionType() == mo.getPermissionType() && 125 PermissionModel.this.getProjectId().intValue() == mo.getProjectId().intValue()) { 126 return true; 127 } 128 } catch(ClassCastException cce) { 129 } 130 131 return false; 132 } 133 134 public int hashCode() { 135 return ((PermissionModel.this.getProjectId() == null ? 1 : PermissionModel.this.getProjectId().hashCode()) ^ 136 PermissionModel.this.getPermissionType()); 137 } 138 } 139 } | Popular Tags |