IDEA-12028
[fedora-idea.git] / dom / impl / src / com / intellij / util / xml / impl / CollectionChildDescriptionImpl.java
blob847c828d60359c4e3460332eebfc201cc351f85a
1 /*
2 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3 */
4 package com.intellij.util.xml.impl;
6 import com.intellij.openapi.util.text.StringUtil;
7 import com.intellij.util.ArrayUtil;
8 import com.intellij.util.IncorrectOperationException;
9 import com.intellij.util.xml.DomElement;
10 import com.intellij.util.xml.DomNameStrategy;
11 import com.intellij.util.xml.JavaMethod;
12 import com.intellij.util.xml.reflect.DomCollectionChildDescription;
13 import org.jetbrains.annotations.NonNls;
14 import org.jetbrains.annotations.NotNull;
15 import org.jetbrains.annotations.Nullable;
17 import java.lang.annotation.Annotation;
18 import java.lang.reflect.AnnotatedElement;
19 import java.lang.reflect.Type;
20 import java.util.List;
22 /**
23 * @author peter
25 public class CollectionChildDescriptionImpl extends DomChildDescriptionImpl implements DomCollectionChildDescription {
26 private final JavaMethod myGetterMethod;
27 private final JavaMethod myAdderMethod;
28 private final JavaMethod myIndexedAdderMethod;
29 private final JavaMethod myClassAdderMethod;
30 private final JavaMethod myIndexedClassAdderMethod;
31 private final JavaMethod myInvertedIndexedClassAdderMethod;
32 @NonNls private static final String ES = "es";
34 public CollectionChildDescriptionImpl(final XmlName tagName,
35 final Type type,
36 final JavaMethod adderMethod,
37 final JavaMethod classAdderMethod,
38 final JavaMethod getterMethod,
39 final JavaMethod indexedAdderMethod,
40 final JavaMethod indexedClassAdderMethod,
41 final JavaMethod invertedIndexedClassAdderMethod) {
42 super(tagName, type);
43 myAdderMethod = adderMethod;
44 myClassAdderMethod = classAdderMethod;
45 myGetterMethod = getterMethod;
46 myIndexedAdderMethod = indexedAdderMethod;
47 myIndexedClassAdderMethod = indexedClassAdderMethod;
48 myInvertedIndexedClassAdderMethod = invertedIndexedClassAdderMethod;
51 public JavaMethod getClassAdderMethod() {
52 return myClassAdderMethod;
55 public JavaMethod getIndexedClassAdderMethod() {
56 return myIndexedClassAdderMethod;
59 public JavaMethod getInvertedIndexedClassAdderMethod() {
60 return myInvertedIndexedClassAdderMethod;
63 public JavaMethod getAdderMethod() {
64 return myAdderMethod;
67 public DomElement addValue(DomElement element) {
68 return addChild(element, getType(), Integer.MAX_VALUE);
71 private DomElement addChild(final DomElement element, final Type type, final int index) {
72 try {
73 final DomInvocationHandler handler = DomManagerImpl.getDomInvocationHandler(element);
74 assert handler != null;
75 return handler.addChild(getXmlName().createEvaluatedXmlName(handler), type, index);
77 catch (IncorrectOperationException e) {
78 throw new RuntimeException(e);
82 public DomElement addValue(DomElement element, int index) {
83 return addChild(element, getType(), index);
86 public DomElement addValue(DomElement parent, Type type) {
87 return addValue(parent, type, Integer.MAX_VALUE);
90 public final DomElement addValue(DomElement parent, Type type, int index) {
91 return addChild(parent, type, Integer.MAX_VALUE);
94 public JavaMethod getGetterMethod() {
95 return myGetterMethod;
98 public JavaMethod getIndexedAdderMethod() {
99 return myIndexedAdderMethod;
102 @NotNull
103 public List<? extends DomElement> getValues(final DomElement element) {
104 return (List<? extends DomElement>)myGetterMethod.invoke(element, ArrayUtil.EMPTY_OBJECT_ARRAY);
107 public String getCommonPresentableName(DomNameStrategy strategy) {
108 String words = strategy.splitIntoWords(getXmlElementName());
109 return StringUtil.capitalizeWords(words.endsWith(ES) ? words: StringUtil.pluralize(words), true);
112 @Nullable
113 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
114 final T annotation = getGetterMethod().getAnnotation(annotationClass);
115 if (annotation != null) return annotation;
117 final Type elemType = getType();
118 return elemType instanceof AnnotatedElement ? ((AnnotatedElement)elemType).getAnnotation(annotationClass) : null;
121 public boolean equals(final Object o) {
122 if (this == o) return true;
123 if (o == null || getClass() != o.getClass()) return false;
124 if (!super.equals(o)) return false;
126 final CollectionChildDescriptionImpl that = (CollectionChildDescriptionImpl)o;
128 if (myAdderMethod != null ? !myAdderMethod.equals(that.myAdderMethod) : that.myAdderMethod != null) return false;
129 if (myClassAdderMethod != null ? !myClassAdderMethod.equals(that.myClassAdderMethod) : that.myClassAdderMethod != null) return false;
130 if (myGetterMethod != null ? !myGetterMethod.equals(that.myGetterMethod) : that.myGetterMethod != null) return false;
131 if (myIndexedAdderMethod != null ? !myIndexedAdderMethod.equals(that.myIndexedAdderMethod) : that.myIndexedAdderMethod != null) {
132 return false;
134 if (myIndexedClassAdderMethod != null
135 ? !myIndexedClassAdderMethod.equals(that.myIndexedClassAdderMethod)
136 : that.myIndexedClassAdderMethod != null) {
137 return false;
139 if (myInvertedIndexedClassAdderMethod != null
140 ? !myInvertedIndexedClassAdderMethod.equals(that.myInvertedIndexedClassAdderMethod)
141 : that.myInvertedIndexedClassAdderMethod != null) {
142 return false;
145 return true;
148 public int hashCode() {
149 int result = super.hashCode();
150 result = 29 * result + (myGetterMethod != null ? myGetterMethod.hashCode() : 0);
151 result = 29 * result + (myAdderMethod != null ? myAdderMethod.hashCode() : 0);
152 result = 29 * result + (myIndexedAdderMethod != null ? myIndexedAdderMethod.hashCode() : 0);
153 result = 29 * result + (myClassAdderMethod != null ? myClassAdderMethod.hashCode() : 0);
154 result = 29 * result + (myIndexedClassAdderMethod != null ? myIndexedClassAdderMethod.hashCode() : 0);
155 result = 29 * result + (myInvertedIndexedClassAdderMethod != null ? myInvertedIndexedClassAdderMethod.hashCode() : 0);
156 return result;