View Javadoc

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 org.itracker.model;
20  
21  import java.io.Serializable;
22  import java.util.Comparator;
23  
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  
26  /**
27   * Class provides basic storage for name values pairs. The name is usually a key
28   * of some type, like a status number, and the value is a localized name for
29   * that key.
30   */
31  public class NameValuePair extends AbstractEntity {
32  
33  	/**
34  	 * 
35  	 */
36  	private static final long serialVersionUID = 1L;
37  
38  	private String name = "";
39  
40  	private String value = "";
41  
42  	private static final class NameComparator implements Comparator<NameValuePair>, Serializable {
43  		/**
44  		 * 
45  		 */
46  		private static final long serialVersionUID = 1L;
47  		public int compare(NameValuePair o1, NameValuePair o2) {
48  
49  			return o1.name.compareTo(o2.name);
50  		};
51  	}
52  	private static final class ValueComparator implements Comparator<NameValuePair>, Serializable {
53  		/**
54  		 * 
55  		 */
56  		private static final long serialVersionUID = 1L;
57  		public int compare(NameValuePair o1, NameValuePair o2) {
58  			return o1.value.compareTo(o2.value);
59  		};
60  	}
61  	public static final Comparator<NameValuePair> KEY_COMPARATOR = new NameComparator();
62  	public static final Comparator<NameValuePair> VALUE_COMPARATOR = new ValueComparator();
63  
64  	public NameValuePair(String name, String value) {
65  		setName(name);
66  		setValue(value);
67  	}
68  
69  	/**
70  	 * Returns the name of the name/value pair.
71  	 */
72  	public String getName() {
73  		return name;
74  	}
75  
76  	/**
77  	 * Sets the name of the name/value pair.
78  	 */
79  	public void setName(String name) {
80  		if (name == null) {
81  			name = "";
82  		}
83  		this.name = name;
84  	}
85  
86  	/**
87  	 * Returns the value of the name/value pair.
88  	 */
89  	public String getValue() {
90  		return value;
91  	}
92  
93  	/**
94  	 * Sets the value of the name/value pair.
95  	 */
96  	public void setValue(String value) {
97  		this.value = value;
98  	}
99  
100 	public int compareKeyTo(NameValuePair other) {
101 		return KEY_COMPARATOR.compare(this, other);
102 	}
103 
104 	public int compareValueTo(NameValuePair other) {
105 		return VALUE_COMPARATOR.compare(this, other);
106 	}
107 
108 
109 //
110 //	@Override
111 //	public boolean equals(Object obj) {
112 //		if (this == obj) {
113 //			return true;
114 //		}
115 //
116 //		if (obj instanceof NameValuePair) {
117 //			final NameValuePair other = (NameValuePair) obj;
118 //			return new EqualsBuilder().append(name, other.name).append(value,
119 //					other.value).isEquals();
120 //
121 //		}
122 //		return false;
123 //	}
124 //
125 //	@Override
126 //	public int hashCode() {
127 //		return new HashCodeBuilder().append(this.name).append(this.value)
128 //				.toHashCode();
129 //	}
130 
131 	@Override
132 	public String toString() {
133 		return new ToStringBuilder(this).append("name", name).append("value",
134 				value).toString();
135 	}
136 
137 }