1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.io.Serializable ; 22 import java.util.Comparator ; 23 24 29 public class NameValuePairModel implements Comparator , Serializable { 30 private String name = ""; 31 private String value = ""; 32 33 public NameValuePairModel() { 34 } 35 36 public NameValuePairModel(String name, String value) { 37 this.name = (name == null ? "" : name); 38 this.value = (value == null ? "" : value); 39 } 40 41 44 public String getName() { 45 return name; 46 } 47 48 51 public void setName(String value) { 52 name = value; 53 } 54 55 58 public String getValue() { 59 return value; 60 } 61 62 65 public void setValue(String value) { 66 this.value = value; 67 } 68 69 public int compare(Object a, Object b) { 70 return this.new CompareByName().compare(a, b); 71 } 72 73 public class CompareByName implements Comparator { 74 protected boolean isAscending = true; 75 76 public CompareByName() { 77 } 78 79 public CompareByName(boolean isAscending) { 80 setAscending(isAscending); 81 } 82 83 public void setAscending(boolean value) { 84 this.isAscending = value; 85 } 86 87 public int compare(Object a, Object b) { 88 int result = 0; 89 if(! (a instanceof NameValuePairModel) || ! (b instanceof NameValuePairModel)) { 90 throw new ClassCastException (); 91 } 92 93 NameValuePairModel ma = (NameValuePairModel) a; 94 NameValuePairModel mb = (NameValuePairModel) b; 95 96 if(ma.getName() == null && mb.getName() == null) { 97 result = 0; 98 } else if(ma.getName() == null) { 99 result = 1; 100 } else if(mb.getName() == null) { 101 result = -1; 102 } else { 103 result = ma.getName().compareTo(mb.getName()); 104 } 105 106 return (isAscending ? result : result * -1); 107 } 108 } 109 } 110 | Popular Tags |