/xml/impl/src/com/intellij/xml/impl/dtd/XmlElementsGroupImpl.java

https://bitbucket.org/nbargnesi/idea · Java · 106 lines · 77 code · 11 blank · 18 comment · 3 complexity · 7ae4016582de865a143fcf3023b9813d 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.xml.impl.dtd;
  17. import com.intellij.openapi.util.NotNullLazyValue;
  18. import com.intellij.psi.xml.XmlContentParticle;
  19. import com.intellij.util.Function;
  20. import com.intellij.util.containers.ContainerUtil;
  21. import com.intellij.xml.XmlElementDescriptor;
  22. import com.intellij.xml.XmlElementsGroup;
  23. import org.jetbrains.annotations.NotNull;
  24. import java.util.List;
  25. /**
  26. * @author Dmitry Avdeev
  27. */
  28. public class XmlElementsGroupImpl implements XmlElementsGroup {
  29. private final XmlContentParticle myParticle;
  30. private final XmlElementsGroup myParent;
  31. private final NotNullLazyValue<List<XmlElementsGroup>> mySubGroups = new NotNullLazyValue<List<XmlElementsGroup>>() {
  32. @NotNull
  33. @Override
  34. protected List<XmlElementsGroup> compute() {
  35. return ContainerUtil.map(myParticle.getSubParticles(), new Function<XmlContentParticle, XmlElementsGroup>() {
  36. @Override
  37. public XmlElementsGroup fun(XmlContentParticle xmlContentParticle) {
  38. return new XmlElementsGroupImpl(xmlContentParticle, XmlElementsGroupImpl.this);
  39. }
  40. });
  41. }
  42. };
  43. public XmlElementsGroupImpl(@NotNull XmlContentParticle particle, XmlElementsGroup parent) {
  44. myParticle = particle;
  45. myParent = parent;
  46. }
  47. @Override
  48. public int getMinOccurs() {
  49. switch (myParticle.getQuantifier()) {
  50. case ONE_OR_MORE:
  51. case REQUIRED:
  52. return 1;
  53. case ZERO_OR_MORE:
  54. case OPTIONAL:
  55. return 0;
  56. }
  57. throw new AssertionError(myParticle.getQuantifier());
  58. }
  59. @Override
  60. public int getMaxOccurs() {
  61. switch (myParticle.getQuantifier()) {
  62. case ONE_OR_MORE:
  63. case ZERO_OR_MORE:
  64. return Integer.MAX_VALUE;
  65. case OPTIONAL:
  66. case REQUIRED:
  67. return 1;
  68. }
  69. throw new AssertionError(myParticle.getQuantifier());
  70. }
  71. @Override
  72. public Type getGroupType() {
  73. switch (myParticle.getType()) {
  74. case SEQUENCE:
  75. return Type.SEQUENCE;
  76. case CHOICE:
  77. return Type.CHOICE;
  78. case ELEMENT:
  79. return Type.LEAF;
  80. }
  81. throw new AssertionError(myParticle.getType());
  82. }
  83. @Override
  84. public XmlElementsGroup getParentGroup() {
  85. return myParent;
  86. }
  87. @Override
  88. public List<XmlElementsGroup> getSubGroups() {
  89. return mySubGroups.getValue();
  90. }
  91. @Override
  92. public XmlElementDescriptor getLeafDescriptor() {
  93. return myParticle.getElementDescriptor();
  94. }
  95. }