/plugins/SVNPlugin/tags/0.6.1/src/ise/plugin/svn/library/ListOps.java

# · Java · 147 lines · 40 code · 12 blank · 95 comment · 5 complexity · e890573b26157c4f21f6fd8d2b594d92 MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.library;
  26. import java.util.*;
  27. /**
  28. * <b>NOTE: I've genericized this version of ListOps. This means it is NOT
  29. * backward compatible with other versions of this class.</b>
  30. * <p>
  31. * Set operations for Lists and Sets:
  32. * <ul>
  33. * <li>union: a combined set consisting of the unique elements of two sets</li>
  34. * <li>intersection: a set containing elements that are members of both sets</li>
  35. * <li>symmetric difference: a set containing elements that are in one or the other of, but not both, sets</li>
  36. * <li>difference: a set containing the elements of one set minus any elements in the second set</li>
  37. * <li>cartesian product: the set of all ordered pairs with the first element of each pair selected from one set and the second element selected from the other.
  38. * </ul>
  39. * and some utility functions:
  40. * <ul>
  41. * <li>isSubset: returns true if all elements of the second set are elements of the first set</li>
  42. * <li>equals: returns true if both sets contain the same elements</li>
  43. * <li>disjoint: returns true if no elements of either set are contained in the other</li>
  44. * <li>toSet: converts a list to a set</li>
  45. * <li>toList: converts a set to a list</li>
  46. * </ul>
  47. * There are some interesting side effects: Suppose you have an ArrayList, <code>a</code>, that
  48. * may contain duplicates. Calling <code>toList(toSet(a))</code> return a new ArrayList sorted
  49. * in the same order as <code>a</code> but containing no duplicates.
  50. * @author Dale Anson
  51. */
  52. public class ListOps {
  53. /**
  54. * Performs a union of two collections. The elements of the returned collection will be
  55. * ordered with elements of <code>a</code> first (in their original order),
  56. * followed by elements of <code>b</code>, in their original order.
  57. * @param a one collection
  58. * @param b the other collection
  59. * @return a collection containing the elements of both collections with no duplicates.
  60. */
  61. public static <T> Collection<T> union( Collection<T> a, Collection<T> b ) {
  62. LinkedHashSet<T> union = new LinkedHashSet<T>( a );
  63. union.addAll( b );
  64. return union;
  65. }
  66. /**
  67. * Finds the intersection of two sets. Ordering is the order that the elements
  68. * are in in set <code>a</code>.
  69. * @param a one set
  70. * @param b the other set
  71. * @return the intersection of the two sets, may be empty, will not be null.
  72. */
  73. public static <T> Collection<T> intersection( Collection<T> a, Collection<T> b ) {
  74. LinkedHashSet<T> intersection = new LinkedHashSet<T>();
  75. for (T o : a) {
  76. if (b.contains(o))
  77. intersection.add(o);
  78. }
  79. return intersection;
  80. }
  81. /**
  82. * Finds the difference of set <code>a</code> and set <code>b</code>.
  83. * @param a the first set
  84. * @param b the other set
  85. * @return a set containing the elements of set <code>a</code> that are NOT also
  86. * in set <code>b</code>.
  87. */
  88. public static <T> Collection<T> difference( Collection<T> a, Collection<T> b ) {
  89. LinkedHashSet<T> difference = new LinkedHashSet<T>();
  90. for (T o : a) {
  91. if (!b.contains(o))
  92. difference.add(o);
  93. }
  94. return difference;
  95. }
  96. /**
  97. * Finds the symmetric difference of set <code>a</code> and set <code>b</code>.
  98. * @param a the first set
  99. * @param b the other set
  100. * @return a set containing the elements of set <code>a</code> that are NOT also
  101. * in set <code>b</code> unioned with the elements of set <code>b</code> that
  102. * are NOT also in set <code>a</code>.
  103. */
  104. public static <T> Collection<T> symmetricDifference( Collection<T> a, Collection<T> b ) {
  105. return union( difference( a, b ), difference( b, a ) );
  106. }
  107. /**
  108. * @return true if all elements of <code>b</code> are also in <code>a</code>.
  109. */
  110. public static <T> boolean isSubset( Collection<T> a, Collection<T> b ) {
  111. return a.containsAll( b );
  112. }
  113. /**
  114. * @return true if both sets contain the same elements.
  115. */
  116. public static <T> boolean equals( Collection<T> a, Collection<T> b ) {
  117. return isSubset( a, b ) && isSubset( b, a );
  118. }
  119. /**
  120. * Converts a List to a Set. This has the effect of removing all duplicates
  121. * from the list.
  122. */
  123. public static <T> Set<T> toSet( List<T> a ) {
  124. return new LinkedHashSet<T>( a );
  125. }
  126. /**
  127. * Converts a Set to a List.
  128. */
  129. public static <T> List<T> toList( Set<T> a ) {
  130. return new ArrayList<T>( a );
  131. }
  132. }