KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
23
24 /**
25   * Class provides basic storage for name values pairs. The name is usually
26   * a key of some type, like a status number, and the value is a localized name
27   * for that key.
28   */

29 public class NameValuePairModel implements Comparator JavaDoc, Serializable JavaDoc {
30     private String JavaDoc name = "";
31     private String JavaDoc value = "";
32
33     public NameValuePairModel() {
34     }
35
36     public NameValuePairModel(String JavaDoc name, String JavaDoc value) {
37         this.name = (name == null ? "" : name);
38         this.value = (value == null ? "" : value);
39     }
40
41     /**
42       * Returns the name of the name/value pair.
43       */

44     public String JavaDoc getName() {
45         return name;
46     }
47
48     /**
49       * Sets the name of the name/value pair.
50       */

51     public void setName(String JavaDoc value) {
52         name = value;
53     }
54
55     /**
56       * Returns the value of the name/value pair.
57       */

58     public String JavaDoc getValue() {
59         return value;
60     }
61
62     /**
63       * Sets the value of the name/value pair.
64       */

65     public void setValue(String JavaDoc value) {
66         this.value = value;
67     }
68
69     public int compare(Object JavaDoc a, Object JavaDoc b) {
70         return this.new CompareByName().compare(a, b);
71     }
72
73     public class CompareByName implements Comparator JavaDoc {
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 JavaDoc a, Object JavaDoc b) {
88             int result = 0;
89             if(! (a instanceof NameValuePairModel) || ! (b instanceof NameValuePairModel)) {
90                 throw new ClassCastException JavaDoc();
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