001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.constants;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
021 import org.jetbrains.jet.lang.types.JetType;
022 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
023
024 import java.util.List;
025
026 public class ArrayValue extends CompileTimeConstant<List<CompileTimeConstant<?>>> {
027
028 private final JetType type;
029
030 public ArrayValue(@NotNull List<CompileTimeConstant<?>> value,
031 @NotNull JetType type,
032 boolean canBeUsedInAnnotations,
033 boolean usesVariableAsConstant) {
034 super(value, canBeUsedInAnnotations, false, usesVariableAsConstant);
035 this.type = type;
036 }
037
038 @NotNull
039 @Override
040 public List<CompileTimeConstant<?>> getValue() {
041 List<CompileTimeConstant<?>> value = super.getValue();
042 assert value != null : "Guaranteed by constructor";
043 return value;
044 }
045
046 @NotNull
047 @Override
048 public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
049 return type;
050 }
051
052 @Override
053 public <R, D> R accept(AnnotationArgumentVisitor<R, D> visitor, D data) {
054 return visitor.visitArrayValue(this, data);
055 }
056
057 @Override
058 public String toString() {
059 return value.toString();
060 }
061
062 @Override
063 public boolean equals(Object o) {
064 if (this == o) return true;
065 if (o == null || getClass() != o.getClass()) return false;
066
067 ArrayValue that = (ArrayValue) o;
068
069 if (value == null) {
070 return that.value == null;
071 }
072
073 int i = 0;
074 for (Object thisObject : value) {
075 if (!thisObject.equals(that.value.get(i))) {
076 return false;
077 }
078 i++;
079 }
080
081 return true;
082 }
083
084 @Override
085 public int hashCode() {
086 int hashCode = 0;
087 if (value == null) return hashCode;
088 for (Object o : value) {
089 hashCode += o.hashCode();
090 }
091 return hashCode;
092 }
093 }
094