1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.util.Comparator ; 22 23 public class VersionModel extends GenericModel implements Comparator { 24 private String number; 25 private int major; 26 private int minor; 27 private String description; 28 private Integer projectId; 29 30 public VersionModel() { 31 major = 0; 32 minor = 0; 33 number = ""; 34 description = ""; 35 projectId = new Integer (-1); 36 } 37 38 public VersionModel(Integer id) { 39 super(id); 40 } 41 42 public String getNumber() { 43 return (number == null ? "" : number); 44 } 45 46 public void setNumber(String value) { 47 number = value; 48 } 49 50 public int getMajor() { 51 return major; 52 } 53 54 public void setMajor(int value) { 55 major = value; 56 } 57 58 public int getMinor() { 59 return minor; 60 } 61 62 public void setMinor(int value) { 63 minor = value; 64 } 65 66 public String getDescription() { 67 return (description == null ? "" : description); 68 } 69 70 public void setDescription(String value) { 71 description = value; 72 } 73 74 public Integer getProjectId() { 75 return projectId; 76 } 77 78 public void setProjectId(Integer value) { 79 projectId = value; 80 } 81 82 89 public void setVersionInfo(String versionInfo) { 90 setNumber(versionInfo); 91 92 String versionNumber = getNumber().trim(); 93 int firstDot = versionNumber.indexOf('.'); 94 String major = "0"; 95 major = (firstDot > 0 ? versionNumber.substring(0, firstDot).trim() : versionNumber.trim()); 96 97 try { 98 setMajor(Integer.parseInt(major)); 99 } catch(NumberFormatException nfe) { 100 setMajor(0); 101 } 102 103 int secondDot = (firstDot > -1 ? versionNumber.indexOf('.', firstDot + 1) : -1); 104 String minor = (secondDot > -1 ? versionNumber.substring(firstDot + 1, secondDot).trim() : versionNumber.substring(firstDot + 1).trim()); 105 try { 106 setMinor(Integer.parseInt(minor)); 107 } catch(NumberFormatException nfe) { 108 setMinor(0); 109 } 110 } 111 112 public String toString() { 113 return "Version [" + this.getId() + "] Project: " + this.getProjectId() + " Number: " + this.getNumber(); 114 } 115 116 public int compare(Object a, Object b) { 117 return this.new CompareByMajorMinor(false).compare(a, b); 118 } 119 120 public boolean equals(Object obj) { 121 return this.new CompareByMajorMinor(false).equals(obj); 122 } 123 124 public int hashCode() { 125 return this.new CompareByMajorMinor().hashCode(); 126 } 127 128 public class CompareByMajorMinor implements Comparator { 129 private boolean isAscending = true; 130 131 public CompareByMajorMinor() { 132 } 133 134 public CompareByMajorMinor(boolean isAscending) { 135 setAscending(isAscending); 136 } 137 138 public void setAscending(boolean value) { 139 this.isAscending = value; 140 } 141 142 public int compare(Object a, Object b) { 143 int result = 0; 144 145 if(! (a instanceof VersionModel) || ! (b instanceof VersionModel)) { 146 throw new ClassCastException (); 147 } 148 149 VersionModel ma = (VersionModel) a; 150 VersionModel mb = (VersionModel) b; 151 152 if(ma.getMajor() == mb.getMajor()) { 153 if(ma.getMinor() == mb.getMinor()) { 154 if(ma.getNumber() == null) { 155 result = -1; 156 } else if(mb.getNumber() == null) { 157 result = 1; 158 } else { 159 result = ma.getNumber().compareTo(mb.getNumber()); 160 } 161 } else if(ma.getMinor() > mb.getMinor()) { 162 result = 1; 163 } else { 164 result = -1; 165 } 166 } else if(ma.getMajor() > mb.getMajor()) { 167 result = 1; 168 } else { 169 result = -1; 170 } 171 172 return (isAscending ? result : result * -1); 173 } 174 175 public boolean equals(Object obj) { 176 if(! (obj instanceof VersionModel)) { 177 return false; 178 } 179 180 try { 181 VersionModel mo = (VersionModel) obj; 182 if(VersionModel.this.getMajor() == mo.getMajor() && VersionModel.this.getMinor() == mo.getMinor()) { 183 return true; 184 } 185 } catch(ClassCastException cce) { 186 } 187 188 return false; 189 } 190 191 public int hashCode() { 192 return (VersionModel.this.getMajor() ^ VersionModel.this.getMinor()); 193 } 194 } 195 } 196 | Popular Tags |