/platform/lang-impl/src/com/intellij/ide/projectView/impl/ModuleGroupUtil.java

https://bitbucket.org/nbargnesi/idea · Java · 95 lines · 63 code · 11 blank · 21 comment · 6 complexity · a678ba1bd8414fd4a9993dda9506318b MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 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. /*
  17. * Created by IntelliJ IDEA.
  18. * User: Anna.Kozlova
  19. * Date: 16-Jul-2006
  20. * Time: 16:29:04
  21. */
  22. package com.intellij.ide.projectView.impl;
  23. import com.intellij.util.ArrayUtil;
  24. import com.intellij.util.Consumer;
  25. import com.intellij.util.Function;
  26. import java.util.ArrayList;
  27. import java.util.Map;
  28. public class ModuleGroupUtil {
  29. private ModuleGroupUtil() {
  30. }
  31. public static <T> T buildModuleGroupPath(final ModuleGroup group,
  32. T parentNode,
  33. final Map<ModuleGroup, T> map,
  34. final Consumer<ParentChildRelation<T>> insertNode,
  35. final Function<ModuleGroup, T> createNewNode) {
  36. final ArrayList<String> path = new ArrayList<String>();
  37. final String[] groupPath = group.getGroupPath();
  38. for (String pathElement : groupPath) {
  39. path.add(pathElement);
  40. final ModuleGroup moduleGroup = new ModuleGroup(ArrayUtil.toStringArray(path));
  41. T moduleGroupNode = map.get(moduleGroup);
  42. if (moduleGroupNode == null) {
  43. moduleGroupNode = createNewNode.fun(moduleGroup);
  44. map.put(moduleGroup, moduleGroupNode);
  45. insertNode.consume(new ParentChildRelation<T>(parentNode, moduleGroupNode));
  46. }
  47. parentNode = moduleGroupNode;
  48. }
  49. return parentNode;
  50. }
  51. public static <T> T updateModuleGroupPath(final ModuleGroup group,
  52. T parentNode,
  53. final Function<ModuleGroup, T> needToCreateNode,
  54. final Consumer<ParentChildRelation<T>> insertNode,
  55. final Function<ModuleGroup, T> createNewNode) {
  56. final ArrayList<String> path = new ArrayList<String>();
  57. final String[] groupPath = group.getGroupPath();
  58. for (String pathElement : groupPath) {
  59. path.add(pathElement);
  60. final ModuleGroup moduleGroup = new ModuleGroup(ArrayUtil.toStringArray(path));
  61. T moduleGroupNode = needToCreateNode.fun(moduleGroup);
  62. if (moduleGroupNode == null) {
  63. moduleGroupNode = createNewNode.fun(moduleGroup);
  64. insertNode.consume(new ParentChildRelation<T>(parentNode, moduleGroupNode));
  65. }
  66. parentNode = moduleGroupNode;
  67. }
  68. return parentNode;
  69. }
  70. public static class ParentChildRelation<T> {
  71. private final T myParent;
  72. private final T myChild;
  73. public ParentChildRelation(final T parent, final T child) {
  74. myParent = parent;
  75. myChild = child;
  76. }
  77. public T getParent() {
  78. return myParent;
  79. }
  80. public T getChild() {
  81. return myChild;
  82. }
  83. }
  84. }