View Javadoc

1   /*
2    * @(#) FieldComparator.java Feb 18, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package nl.tudelft.simulation.messaging.comparators;
15  
16  import java.io.Serializable;
17  import java.lang.reflect.Field;
18  import java.util.Comparator;
19  
20  import nl.tudelft.simulation.language.reflection.ClassUtil;
21  
22  /***
23   * A FieldComparator <br>
24   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
25   * Delft, the Netherlands. All rights reserved.
26   * 
27   * See for project information <a href="http://www.simulation.tudelft.nl/">
28   * www.simulation.tudelft.nl </a>.
29   * 
30   * The source code and binary code of this software is proprietary information
31   * of Delft University of Technology.
32   * 
33   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
34   *         Jacobs </a>
35   * @version $$Revision: 1.3 $$ $$Date: 2005/04/08 11:29:12 $$
36   */
37  public class FieldComparator implements Comparator, Serializable
38  {
39  	/*** the serial version uid */
40  	private static final long serialVersionUID = 12L;
41  
42  	/*** the field to question */
43  	protected String fieldName = null;
44  
45  	/*** the next comparator in the chain */
46  	protected Comparator next = new NullComparator();
47  
48  	/*** normal of negative operation */
49  	protected boolean negative = false;
50  
51  	/***
52  	 * constructs a new FieldComparator
53  	 * 
54  	 * @param field the field to compare to
55  	 */
56  	public FieldComparator(final String field)
57  	{
58  		super();
59  		this.fieldName = field;
60  	}
61  
62  	/***
63  	 * constructs a new FieldComparator
64  	 * 
65  	 * @param field the field to compare to
66  	 * @param next the next comparator to use
67  	 */
68  	public FieldComparator(final String field, final Comparator next)
69  	{
70  		this(field);
71  		this.next = next;
72  	}
73  
74  	/***
75  	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
76  	 */
77  	public int compare(final Object o1, final Object o2)
78  	{
79  		try
80  		{
81  			Field field = ClassUtil.resolveField(o1.getClass(), this.fieldName);
82  			field.setAccessible(true);
83  			Comparable value1 = (Comparable) field.get(o1);
84  			Comparable value2 = (Comparable) field.get(o2);
85  			int compare = value1.compareTo(value2);
86  			if (compare == 0)
87  			{
88  				return this.next.compare(o1, o2);
89  			}
90  			if (this.negative)
91  			{
92  				return -1 * compare;
93  			}
94  			return compare;
95  		} catch (Exception exception)
96  		{
97  			return 0;
98  		}
99  	}
100 
101 	/***
102 	 * INNER CLASS defining a default NullComparator that does not do any real
103 	 * comparisons <br>
104 	 * <br>
105 	 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628
106 	 * BX Delft, the Netherlands. All rights reserved.
107 	 * 
108 	 * See for project information <a href="http://www.simulation.tudelft.nl/">
109 	 * www.simulation.tudelft.nl </a>.
110 	 * 
111 	 * The source code and binary code of this software is proprietary
112 	 * information of Delft University of Technology.
113 	 * 
114 	 * @author <a
115 	 *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
116 	 *         Verbraeck </a>
117 	 * @version $$Revision: 1.3 $$ $$Date: 2005/04/08 11:29:12 $$
118 	 */
119 	private static class NullComparator implements Comparator, Serializable
120 	{
121 		/*** the serial version uid */
122 		private static final long serialVersionUID = 12L;
123 
124 		/***
125 		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
126 		 */
127 		public int compare(final Object o1, final Object o2)
128 		{
129 			return 0;
130 		}
131 	}
132 
133 	/***
134 	 * @return Returns the negative.
135 	 */
136 	public boolean isNegative()
137 	{
138 		return this.negative;
139 	}
140 
141 	/***
142 	 * @param negative The negative to set.
143 	 */
144 	public void setNegative(final boolean negative)
145 	{
146 		this.negative = negative;
147 	}
148 }