PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.comparators.ComparatorChain.cs

https://github.com/gadfly/nofs
C# | 389 lines | 170 code | 31 blank | 188 comment | 35 complexity | a63201c90139ae3d028cc97f21971714 MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. namespace org.apache.commons.collections.comparators
  18. {
  19. /**
  20. * <p>A ComparatorChain is a Comparator that wraps one or
  21. * more Comparators in sequence. The ComparatorChain
  22. * calls each Comparator in sequence until either 1)
  23. * any single Comparator returns a non-zero result
  24. * (and that result is then returned),
  25. * or 2) the ComparatorChain is exhausted (and zero is
  26. * returned). This type of sorting is very similar
  27. * to multi-column sorting in SQL, and this class
  28. * allows Java classes to emulate that kind of behaviour
  29. * when sorting a List.</p>
  30. *
  31. * <p>To further facilitate SQL-like sorting, the order of
  32. * any single Comparator in the list can be reversed.</p>
  33. *
  34. * <p>Calling a method that adds new Comparators or
  35. * changes the ascend/descend sort <i>after compare(Object,
  36. * Object) has been called</i> will result in an
  37. * UnsupportedOperationException. However, <i>take care</i>
  38. * to not alter the underlying List of Comparators
  39. * or the BitSet that defines the sort order.</p>
  40. *
  41. * <p>Instances of ComparatorChain are not synchronized.
  42. * The class is not thread-safe at construction time, but
  43. * it <i>is</i> thread-safe to perform multiple comparisons
  44. * after all the setup operations are complete.</p>
  45. *
  46. * @since Commons Collections 2.0
  47. * @author Morgan Delagrange
  48. * @version $Revision$ $Date$
  49. */
  50. [Serializable]
  51. public class ComparatorChain : java.util.Comparator<Object>, java.io.Serializable
  52. {
  53. /** Serialization version from Collections 2.0. */
  54. private static readonly long serialVersionUID = -721644942746081630L;
  55. /** The list of comparators in the chain. */
  56. protected java.util.List<Object> comparatorChain = null;
  57. /** Order - false (clear) = ascend; true (set) = descend. */
  58. protected java.util.BitSet orderingBits = null;
  59. /** Whether the chain has been "locked". */
  60. protected bool isLockedJ = false;
  61. //-----------------------------------------------------------------------
  62. /**
  63. * Construct a ComparatorChain with no Comparators.
  64. * You must add at least one Comparator before calling
  65. * the compare(Object,Object) method, or an
  66. * UnsupportedOperationException is thrown
  67. */
  68. public ComparatorChain() :
  69. this(new java.util.ArrayList<Object>(), new java.util.BitSet())
  70. {
  71. }
  72. /**
  73. * Construct a ComparatorChain with a single Comparator,
  74. * sorting in the forward order
  75. *
  76. * @param comparator First comparator in the Comparator chain
  77. */
  78. public ComparatorChain(java.util.Comparator<Object> comparator) :
  79. this(comparator, false)
  80. {
  81. }
  82. /**
  83. * Construct a Comparator chain with a single Comparator,
  84. * sorting in the given order
  85. *
  86. * @param comparator First Comparator in the ComparatorChain
  87. * @param reverse false = forward sort; true = reverse sort
  88. */
  89. public ComparatorChain(java.util.Comparator<Object> comparator, bool reverse)
  90. {
  91. comparatorChain = new java.util.ArrayList<Object>();
  92. comparatorChain.add(comparator);
  93. orderingBits = new java.util.BitSet(1);
  94. if (reverse == true)
  95. {
  96. orderingBits.set(0);
  97. }
  98. }
  99. /**
  100. * Construct a ComparatorChain from the Comparators in the
  101. * List. All Comparators will default to the forward
  102. * sort order.
  103. *
  104. * @param list List of Comparators
  105. * @see #ComparatorChain(List,BitSet)
  106. */
  107. public ComparatorChain(java.util.List<Object> list) :
  108. this(list, new java.util.BitSet(list.size()))
  109. {
  110. }
  111. /**
  112. * Construct a ComparatorChain from the Comparators in the
  113. * given List. The sort order of each column will be
  114. * drawn from the given BitSet. When determining the sort
  115. * order for Comparator at index <i>i</i> in the List,
  116. * the ComparatorChain will call BitSet.get(<i>i</i>).
  117. * If that method returns <i>false</i>, the forward
  118. * sort order is used; a return value of <i>true</i>
  119. * indicates reverse sort order.
  120. *
  121. * @param list List of Comparators. NOTE: This constructor does not perform a
  122. * defensive copy of the list
  123. * @param bits Sort order for each Comparator. Extra bits are ignored,
  124. * unless extra Comparators are added by another method.
  125. */
  126. public ComparatorChain(java.util.List<Object> list, java.util.BitSet bits)
  127. {
  128. comparatorChain = list;
  129. orderingBits = bits;
  130. }
  131. //-----------------------------------------------------------------------
  132. /**
  133. * Add a Comparator to the end of the chain using the
  134. * forward sort order
  135. *
  136. * @param comparator Comparator with the forward sort order
  137. */
  138. public void addComparator(java.util.Comparator<Object> comparator)
  139. {
  140. addComparator(comparator, false);
  141. }
  142. /**
  143. * Add a Comparator to the end of the chain using the
  144. * given sort order
  145. *
  146. * @param comparator Comparator to add to the end of the chain
  147. * @param reverse false = forward sort order; true = reverse sort order
  148. */
  149. public void addComparator(java.util.Comparator<Object> comparator, bool reverse)
  150. {
  151. checkLocked();
  152. comparatorChain.add(comparator);
  153. if (reverse == true)
  154. {
  155. orderingBits.set(comparatorChain.size() - 1);
  156. }
  157. }
  158. /**
  159. * Replace the Comparator at the given index, maintaining
  160. * the existing sort order.
  161. *
  162. * @param index index of the Comparator to replace
  163. * @param comparator Comparator to place at the given index
  164. * @exception IndexOutOfBoundsException
  165. * if index &lt; 0 or index &gt;= size()
  166. */
  167. public void setComparator(int index, java.util.Comparator<Object> comparator)
  168. {//throws IndexOutOfBoundsException {
  169. setComparator(index, comparator, false);
  170. }
  171. /**
  172. * Replace the Comparator at the given index in the
  173. * ComparatorChain, using the given sort order
  174. *
  175. * @param index index of the Comparator to replace
  176. * @param comparator Comparator to set
  177. * @param reverse false = forward sort order; true = reverse sort order
  178. */
  179. public void setComparator(int index, java.util.Comparator<Object> comparator, bool reverse)
  180. {
  181. checkLocked();
  182. comparatorChain.set(index, comparator);
  183. if (reverse == true)
  184. {
  185. orderingBits.set(index);
  186. }
  187. else
  188. {
  189. orderingBits.clear(index);
  190. }
  191. }
  192. /**
  193. * Change the sort order at the given index in the
  194. * ComparatorChain to a forward sort.
  195. *
  196. * @param index Index of the ComparatorChain
  197. */
  198. public void setForwardSort(int index)
  199. {
  200. checkLocked();
  201. orderingBits.clear(index);
  202. }
  203. /**
  204. * Change the sort order at the given index in the
  205. * ComparatorChain to a reverse sort.
  206. *
  207. * @param index Index of the ComparatorChain
  208. */
  209. public void setReverseSort(int index)
  210. {
  211. checkLocked();
  212. orderingBits.set(index);
  213. }
  214. /**
  215. * Number of Comparators in the current ComparatorChain.
  216. *
  217. * @return Comparator count
  218. */
  219. public int size()
  220. {
  221. return comparatorChain.size();
  222. }
  223. /**
  224. * Determine if modifications can still be made to the
  225. * ComparatorChain. ComparatorChains cannot be modified
  226. * once they have performed a comparison.
  227. *
  228. * @return true = ComparatorChain cannot be modified; false =
  229. * ComparatorChain can still be modified.
  230. */
  231. public bool isLocked()
  232. {
  233. return isLockedJ;
  234. }
  235. // throw an exception if the ComparatorChain is locked
  236. private void checkLocked()
  237. {
  238. if (isLockedJ == true)
  239. {
  240. throw new java.lang.UnsupportedOperationException("Comparator ordering cannot be changed after the first comparison is performed");
  241. }
  242. }
  243. private void checkChainIntegrity()
  244. {
  245. if (comparatorChain.size() == 0)
  246. {
  247. throw new java.lang.UnsupportedOperationException("ComparatorChains must contain at least one Comparator");
  248. }
  249. }
  250. //-----------------------------------------------------------------------
  251. /**
  252. * Perform comparisons on the Objects as per
  253. * Comparator.compare(o1,o2).
  254. *
  255. * @param o1 the first object to compare
  256. * @param o2 the second object to compare
  257. * @return -1, 0, or 1
  258. * @exception UnsupportedOperationException
  259. * if the ComparatorChain does not contain at least one
  260. * Comparator
  261. */
  262. public int compare(Object o1, Object o2)
  263. {//throws UnsupportedOperationException {
  264. if (isLockedJ == false)
  265. {
  266. checkChainIntegrity();
  267. isLockedJ = true;
  268. }
  269. // iterate over all comparators in the chain
  270. java.util.Iterator<Object> comparators = comparatorChain.iterator();
  271. for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex)
  272. {
  273. java.util.Comparator<Object> comparator = (java.util.Comparator<Object>)comparators.next();
  274. int retval = comparator.compare(o1, o2);
  275. if (retval != 0)
  276. {
  277. // invert the order if it is a reverse sort
  278. if (orderingBits.get(comparatorIndex) == true)
  279. {
  280. if (java.lang.Integer.MIN_VALUE == retval)
  281. {
  282. retval = java.lang.Integer.MAX_VALUE;
  283. }
  284. else
  285. {
  286. retval *= -1;
  287. }
  288. }
  289. return retval;
  290. }
  291. }
  292. // if comparators are exhausted, return 0
  293. return 0;
  294. }
  295. //-----------------------------------------------------------------------
  296. /**
  297. * Implement a hash code for this comparator that is consistent with
  298. * {@link #equals(Object) equals}.
  299. *
  300. * @return a suitable hash code
  301. * @since Commons Collections 3.0
  302. */
  303. public override int GetHashCode()
  304. {
  305. int hash = 0;
  306. if (null != comparatorChain)
  307. {
  308. hash ^= comparatorChain.GetHashCode();
  309. }
  310. if (null != orderingBits)
  311. {
  312. hash ^= orderingBits.GetHashCode();
  313. }
  314. return hash;
  315. }
  316. /**
  317. * Returns <code>true</code> iff <i>that</i> Object is
  318. * is a {@link Comparator} whose ordering is known to be
  319. * equivalent to mine.
  320. * <p>
  321. * This implementation returns <code>true</code>
  322. * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
  323. * equals <code>this.getClass()</code>, and the underlying
  324. * comparators and order bits are equal.
  325. * Subclasses may want to override this behavior to remain consistent
  326. * with the {@link Comparator#equals(Object)} contract.
  327. *
  328. * @param object the object to compare with
  329. * @return true if equal
  330. * @since Commons Collections 3.0
  331. */
  332. public override bool Equals(Object obj)
  333. {
  334. if (this == obj)
  335. {
  336. return true;
  337. }
  338. else if (null == obj)
  339. {
  340. return false;
  341. }
  342. else if (obj.getClass().Equals(this.getClass()))
  343. {
  344. ComparatorChain chain = (ComparatorChain)obj;
  345. return ((null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits))
  346. && (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)));
  347. }
  348. else
  349. {
  350. return false;
  351. }
  352. }
  353. public virtual bool equals(Object obj)
  354. {
  355. return Equals(obj);
  356. }
  357. }
  358. }