/platform/vcs-api/src/com/intellij/openapi/vcs/QuantitySelection.java

https://bitbucket.org/nbargnesi/idea · Java · 117 lines · 81 code · 20 blank · 16 comment · 10 complexity · ae5d4c35d00d1b817e844376bb0dfd4a MD5 · raw file

  1. /*
  2. * Copyright 2000-2010 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.vcs;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. public class QuantitySelection<T> implements SelectionManipulation<T>, SelectionState<T> {
  20. private final Group<T> mySelected;
  21. private final Group<T> myUnselected;
  22. public QuantitySelection(final boolean startFromSelectAll) {
  23. mySelected = new Group<T>();
  24. myUnselected = new Group<T>();
  25. if (startFromSelectAll) {
  26. mySelected.setAll();
  27. } else {
  28. myUnselected.setAll();
  29. }
  30. }
  31. public void add(T t) {
  32. if (mySelected.isAll()) {
  33. myUnselected.remove(t);
  34. } else {
  35. mySelected.add(t);
  36. }
  37. }
  38. public void remove(T t) {
  39. if (mySelected.isAll()) {
  40. myUnselected.add(t);
  41. } else {
  42. mySelected.remove(t);
  43. }
  44. }
  45. public void clearAll() {
  46. mySelected.clearAll();
  47. myUnselected.setAll();
  48. }
  49. public void setAll() {
  50. myUnselected.clearAll();
  51. mySelected.setAll();
  52. }
  53. public SelectionResult<T> getSelected() {
  54. return mySelected;
  55. }
  56. public SelectionResult<T> getUnselected() {
  57. return myUnselected;
  58. }
  59. public boolean isSelected(final T t) {
  60. return mySelected.isAll() && (! myUnselected.hasPoint(t)) || myUnselected.isAll() && mySelected.hasPoint(t);
  61. }
  62. public static class Group<T> implements SelectionManipulation<T>, SelectionResult<T> {
  63. private boolean myAll;
  64. private final Set<T> myMarked;
  65. private Group() {
  66. myMarked = new HashSet<T>();
  67. }
  68. public void add(final T t) {
  69. myMarked.add(t);
  70. }
  71. // +- consistency check not here
  72. public void remove(final T t) {
  73. myAll = false;
  74. myMarked.remove(t);
  75. }
  76. public void clearAll() {
  77. myAll = false;
  78. myMarked.clear();
  79. }
  80. public void setAll() {
  81. myAll = true;
  82. myMarked.clear();
  83. }
  84. public Set<T> getMarked() {
  85. return myMarked;
  86. }
  87. public boolean isAll() {
  88. return myAll;
  89. }
  90. public boolean hasPoint(final T t) {
  91. return myMarked.contains(t);
  92. }
  93. public boolean isIncluded(final T t) {
  94. return myAll || myMarked.contains(t);
  95. }
  96. }
  97. }