001 /*
002 * Copyright 2010-2015 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.kotlin.resolve.constants;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
021 import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor;
022 import org.jetbrains.kotlin.types.JetType;
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 assert KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)
036 : "Type should be an array, but was " + type + ": " + value;
037 this.type = type;
038 }
039
040 @NotNull
041 @Override
042 public List<CompileTimeConstant<?>> getValue() {
043 List<CompileTimeConstant<?>> value = super.getValue();
044 assert value != null : "Guaranteed by constructor";
045 return value;
046 }
047
048 @NotNull
049 @Override
050 public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
051 return type;
052 }
053
054 @Override
055 public <R, D> R accept(AnnotationArgumentVisitor<R, D> visitor, D data) {
056 return visitor.visitArrayValue(this, data);
057 }
058
059 @Override
060 public String toString() {
061 return value.toString();
062 }
063
064 @Override
065 public boolean equals(Object o) {
066 if (this == o) return true;
067 if (o == null || getClass() != o.getClass()) return false;
068
069 ArrayValue that = (ArrayValue) o;
070
071 if (value == null) {
072 return that.value == null;
073 }
074
075 int i = 0;
076 for (Object thisObject : value) {
077 if (!thisObject.equals(that.value.get(i))) {
078 return false;
079 }
080 i++;
081 }
082
083 return true;
084 }
085
086 @Override
087 public int hashCode() {
088 int hashCode = 0;
089 if (value == null) return hashCode;
090 for (Object o : value) {
091 hashCode += o.hashCode();
092 }
093 return hashCode;
094 }
095 }
096