| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | package com.github.javaparser.ast.body; |
| 22 | |
| 23 | import com.github.javaparser.TokenRange; |
| 24 | import com.github.javaparser.ast.*; |
| 25 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 26 | import com.github.javaparser.ast.expr.SimpleName; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithImplements; |
| 28 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 29 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
| 30 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 31 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 32 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 33 | import com.github.javaparser.metamodel.EnumDeclarationMetaModel; |
| 34 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 35 | import com.github.javaparser.resolution.Resolvable; |
| 36 | import com.github.javaparser.resolution.declarations.ResolvedEnumDeclaration; |
| 37 | import java.util.Optional; |
| 38 | import java.util.function.Consumer; |
| 39 | import static com.github.javaparser.utils.Utils.assertNonEmpty; |
| 40 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 41 | import com.github.javaparser.ast.Node; |
| 42 | import com.github.javaparser.ast.Generated; |
| 43 | |
| 44 | /** |
| 45 | * The declaration of an enum.<br>{@code enum X { ... }} |
| 46 | * |
| 47 | * @author Julio Vilmar Gesser |
| 48 | */ |
| 49 | public class EnumDeclaration extends TypeDeclaration<EnumDeclaration> implements NodeWithImplements<EnumDeclaration>, Resolvable<ResolvedEnumDeclaration> { |
| 50 | |
| 51 | private NodeList<ClassOrInterfaceType> implementedTypes; |
| 52 | |
| 53 | private NodeList<EnumConstantDeclaration> entries; |
| 54 | |
| 55 | public EnumDeclaration() { |
| 56 | this(null, new NodeList<>(), new NodeList<>(), new SimpleName(), new NodeList<>(), new NodeList<>(), new NodeList<>()); |
| 57 | } |
| 58 | |
| 59 | public EnumDeclaration(NodeList<Modifier> modifiers, String name) { |
| 60 | this(null, modifiers, new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new NodeList<>()); |
| 61 | } |
| 62 | |
| 63 | @AllFieldsConstructor |
| 64 | public EnumDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<EnumConstantDeclaration> entries, NodeList<BodyDeclaration<?>> members) { |
| 65 | this(null, modifiers, annotations, name, implementedTypes, entries, members); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This constructor is used by the parser and is considered private. |
| 70 | */ |
| 71 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 72 | public EnumDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<EnumConstantDeclaration> entries, NodeList<BodyDeclaration<?>> members) { |
| 73 | super(tokenRange, modifiers, annotations, name, members); |
| 74 | setImplementedTypes(implementedTypes); |
| 75 | setEntries(entries); |
| 76 | customInitialization(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 81 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 82 | return v.visit(this, arg); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 87 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 88 | v.visit(this, arg); |
| 89 | } |
| 90 | |
| 91 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 92 | public NodeList<EnumConstantDeclaration> getEntries() { |
| 93 | return entries; |
| 94 | } |
| 95 | |
| 96 | public EnumConstantDeclaration getEntry(int i) { |
| 97 | return getEntries().get(i); |
| 98 | } |
| 99 | |
| 100 | public EnumDeclaration setEntry(int i, EnumConstantDeclaration element) { |
| 101 | getEntries().set(i, element); |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | public EnumDeclaration addEntry(EnumConstantDeclaration element) { |
| 106 | getEntries().add(element); |
| 107 | return this; |
| 108 | } |
| 109 | |
| 110 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 111 | public NodeList<ClassOrInterfaceType> getImplementedTypes() { |
| 112 | return implementedTypes; |
| 113 | } |
| 114 | |
| 115 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 116 | public EnumDeclaration setEntries(final NodeList<EnumConstantDeclaration> entries) { |
| 117 | assertNotNull(entries); |
| 118 | if (entries == this.entries) { |
| 119 | return (EnumDeclaration) this; |
| 120 | } |
| 121 | notifyPropertyChange(ObservableProperty.ENTRIES, this.entries, entries); |
| 122 | if (this.entries != null) |
| 123 | this.entries.setParentNode(null); |
| 124 | this.entries = entries; |
| 125 | setAsParentNodeOf(entries); |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 130 | public EnumDeclaration setImplementedTypes(final NodeList<ClassOrInterfaceType> implementedTypes) { |
| 131 | assertNotNull(implementedTypes); |
| 132 | if (implementedTypes == this.implementedTypes) { |
| 133 | return (EnumDeclaration) this; |
| 134 | } |
| 135 | notifyPropertyChange(ObservableProperty.IMPLEMENTED_TYPES, this.implementedTypes, implementedTypes); |
| 136 | if (this.implementedTypes != null) |
| 137 | this.implementedTypes.setParentNode(null); |
| 138 | this.implementedTypes = implementedTypes; |
| 139 | setAsParentNodeOf(implementedTypes); |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | public EnumConstantDeclaration addEnumConstant(String name) { |
| 144 | assertNonEmpty(name); |
| 145 | EnumConstantDeclaration enumConstant = new EnumConstantDeclaration(name); |
| 146 | getEntries().add(enumConstant); |
| 147 | return enumConstant; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 152 | public boolean remove(Node node) { |
| 153 | if (node == null) |
| 154 | return false; |
| 155 | for (int i = 0; i < entries.size(); i++) { |
| 156 | if (entries.get(i) == node) { |
| 157 | entries.remove(i); |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | for (int i = 0; i < implementedTypes.size(); i++) { |
| 162 | if (implementedTypes.get(i) == node) { |
| 163 | implementedTypes.remove(i); |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | return super.remove(node); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 172 | public EnumDeclaration clone() { |
| 173 | return (EnumDeclaration) accept(new CloneVisitor(), null); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 178 | public EnumDeclarationMetaModel getMetaModel() { |
| 179 | return JavaParserMetaModel.enumDeclarationMetaModel; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 184 | public boolean replace(Node node, Node replacementNode) { |
| 185 | if (node == null) |
| 186 | return false; |
| 187 | for (int i = 0; i < entries.size(); i++) { |
| 188 | if (entries.get(i) == node) { |
| 189 | entries.set(i, (EnumConstantDeclaration) replacementNode); |
| 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | for (int i = 0; i < implementedTypes.size(); i++) { |
| 194 | if (implementedTypes.get(i) == node) { |
| 195 | implementedTypes.set(i, (ClassOrInterfaceType) replacementNode); |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | return super.replace(node, replacementNode); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 204 | public boolean isEnumDeclaration() { |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 210 | public EnumDeclaration asEnumDeclaration() { |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 215 | public void ifEnumDeclaration(Consumer<EnumDeclaration> action) { |
| 216 | action.accept(this); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public ResolvedEnumDeclaration resolve() { |
| 221 | return getSymbolResolver().resolveDeclaration(this, ResolvedEnumDeclaration.class); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 226 | public Optional<EnumDeclaration> toEnumDeclaration() { |
| 227 | return Optional.of(this); |
| 228 | } |
| 229 | } |
| 230 |
Members