cleanup
[idea/community.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / annotator / intentions / dynamic / elements / DClassElement.java
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 package org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements;
17
18 import com.intellij.openapi.project.Project;
19 import org.jetbrains.annotations.Nullable;
20 import org.jetbrains.plugins.groovy.annotator.intentions.QuickfixUtil;
21 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicManager;
22 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.MyPair;
23
24 import java.util.*;
25
26 /**
27  * User: Dmitry.Krasilschikov
28  * Date: 20.12.2007
29  */
30 public class DClassElement implements DNamedElement {
31   public String myName;
32   public Set<DPropertyElement> myProperties = new HashSet<DPropertyElement>();
33   public Set<DMethodElement> myMethods = new HashSet<DMethodElement>();
34
35   public DClassElement(Project project, String name) {
36     myName = name;
37     DynamicManager.getInstance(project).getRootElement().mergeAddClass(this);
38   }
39
40   public void addMethod(DMethodElement methodElement) {
41     myMethods.add(methodElement);
42   }
43
44    void addMethods(Collection<DMethodElement> methods) {
45     myMethods.addAll(methods);
46   }
47
48   public void addProperty(DPropertyElement propertyElement) {
49     myProperties.add(propertyElement);
50   }
51
52   protected void addProperties(Collection<DPropertyElement> properties) {
53     for (DPropertyElement property : properties) {
54       addProperty(property);
55     }
56   }
57
58   @Nullable
59   public DPropertyElement getPropertyByName(String propertyName) {
60     for (final DPropertyElement property : myProperties) {
61       if (propertyName.equals(property.getName())) {
62         return property;
63       }
64     }
65     return null;
66   }
67
68   public Collection<DPropertyElement> getProperties() {
69     return myProperties;
70   }
71
72   public Set<DMethodElement> getMethods() {
73     return myMethods;
74   }
75
76   public String getName() {
77     return myName;
78   }
79
80   public void setName(String name) {
81     myName = name;
82   }
83
84   public void removeProperty(DPropertyElement name) {
85     myProperties.remove(name);
86   }
87
88   public boolean removeMethod(DMethodElement methodElement) {
89     return myMethods.remove(methodElement);
90   }
91
92   public boolean equals(Object o) {
93     if (this == o) return true;
94     if (o == null || getClass() != o.getClass()) return false;
95
96     DClassElement that = (DClassElement) o;
97
98     if (myName != null ? !myName.equals(that.myName) : that.myName != null) return false;
99
100     return true;
101   }
102
103   public int hashCode() {
104     int result;
105     result = (myName != null ? myName.hashCode() : 0);
106     result = 31 * result + (myProperties != null ? myProperties.hashCode() : 0);
107     result = 31 * result + (myMethods != null ? myMethods.hashCode() : 0);
108     return result;
109   }
110
111   @Nullable
112   public DMethodElement getMethod(String methodName, String[] parametersTypes) {
113     for (DMethodElement method : myMethods) {
114       final List<MyPair> myPairList = method.getPairs();
115       if (method.getName().equals(methodName)
116           && Arrays.equals(QuickfixUtil.getArgumentsTypes(myPairList), parametersTypes)) return method;
117     }
118     return null;
119   }
120
121   public boolean containsElement(DItemElement itemElement){
122     //noinspection SuspiciousMethodCalls
123     return myProperties.contains(itemElement) ||
124            (itemElement instanceof DMethodElement && myMethods.contains(itemElement));
125   }
126 }