KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > CustomFieldValueModel


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.models;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.*;
23
24 import cowsultants.itracker.ejb.client.resources.*;
25 import cowsultants.itracker.ejb.client.util.*;
26
27 public class CustomFieldValueModel extends GenericModel implements Comparator {
28     private Integer JavaDoc customFieldId;
29     private String JavaDoc optionValue;
30     private String JavaDoc name;
31     private int sortOrder;
32
33     public CustomFieldValueModel() {
34     }
35
36     public CustomFieldValueModel(Integer JavaDoc customFieldId, String JavaDoc value, String JavaDoc name) {
37         setCustomFieldId(customFieldId);
38         setValue(value);
39         setName(name);
40     }
41
42     public String JavaDoc getValue() {
43         return (optionValue == null ? "" : optionValue);
44     }
45
46     public void setValue(String JavaDoc value) {
47         optionValue = value;
48     }
49
50     public int getSortOrder() {
51         return sortOrder;
52     }
53
54     public void setSortOrder(int value) {
55         sortOrder = value;
56     }
57
58     public Integer JavaDoc getCustomFieldId() {
59         return (customFieldId == null ? new Integer JavaDoc(-1) : customFieldId);
60     }
61
62     public void setCustomFieldId(Integer JavaDoc value) {
63         customFieldId = value;
64     }
65
66     public String JavaDoc getName() {
67         return (name == null ? "" : name);
68     }
69
70     public void setName(String JavaDoc value) {
71         name = value;
72     }
73
74     public int compare(Object JavaDoc a, Object JavaDoc b) {
75         return this.new CompareBySortOrder().compare(a, b);
76     }
77
78     public boolean equals(Object JavaDoc obj) {
79         return this.new CompareBySortOrder().equals(obj);
80     }
81
82     public int hashCode() {
83         return this.new CompareBySortOrder().hashCode();
84     }
85
86     public String JavaDoc toString() {
87         return "CustomFieldValue [" + getId() + "] Value: " + getValue() + " CustomField: " + getCustomFieldId();
88     }
89
90     public abstract class CustomFieldValueModelComparator implements Comparator {
91         protected boolean isAscending = true;
92
93         public CustomFieldValueModelComparator() {
94         }
95
96         public CustomFieldValueModelComparator(boolean isAscending) {
97             setAscending(isAscending);
98         }
99
100         public void setAscending(boolean value) {
101             this.isAscending = value;
102         }
103
104         protected abstract int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb);
105
106         public final boolean equals(Object JavaDoc obj) {
107             if(! (obj instanceof CustomFieldValueModel)) {
108                 return false;
109             }
110
111             try {
112                 CustomFieldValueModel mo = (CustomFieldValueModel) obj;
113                 if(CustomFieldValueModel.this.getId() == mo.getId()) {
114                     return true;
115                 }
116             } catch(ClassCastException JavaDoc cce) {
117             }
118
119             return false;
120         }
121
122         public int hashCode() {
123             return (CustomFieldValueModel.this.getId() == null ? 1 : CustomFieldValueModel.this.getId().intValue());
124         }
125
126         public final int compare(Object JavaDoc a, Object JavaDoc b) {
127             if(! (a instanceof CustomFieldValueModel) || ! (b instanceof CustomFieldValueModel)) {
128                 throw new ClassCastException JavaDoc();
129             }
130
131             CustomFieldValueModel ma = (CustomFieldValueModel) a;
132             CustomFieldValueModel mb = (CustomFieldValueModel) b;
133
134             int result = doComparison(ma, mb);
135             if(! isAscending) {
136                 result = result * -1;
137             }
138             return result;
139         }
140     }
141
142     public class CompareBySortOrder extends CustomFieldValueModelComparator {
143         public CompareBySortOrder(){
144           super();
145         }
146
147         public CompareBySortOrder(boolean isAscending) {
148           super(isAscending);
149         }
150
151         protected int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb) {
152             if(ma.getSortOrder() == mb.getSortOrder()) {
153                 return 0;
154             } else if(ma.getSortOrder() > mb.getSortOrder()) {
155                 return 1;
156             } else if(mb.getSortOrder() < mb.getSortOrder()) {
157                 return -1;
158             }
159
160             return 0;
161         }
162     }
163
164     public class CompareByName extends CustomFieldValueModelComparator {
165         public CompareByName(){
166           super();
167         }
168
169         public CompareByName(boolean isAscending) {
170           super(isAscending);
171         }
172
173         protected int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb) {
174             if(ma.getName().equals(mb.getName())) {
175                 if(ma.getSortOrder() > mb.getSortOrder()) {
176                     return 1;
177                 } else if(mb.getSortOrder() < mb.getSortOrder()) {
178                     return -1;
179                 } else {
180                     return 0;
181                 }
182             } else {
183                 return ma.getName().compareTo(mb.getName());
184             }
185         }
186     }
187 }
188
Popular Tags