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 com.google.common.collect.Lists;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
023 import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
024 import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
025 import org.jetbrains.jet.lang.types.JetType;
026 import org.jetbrains.jet.lang.types.TypeConstructor;
027 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
028
029 import java.util.Collection;
030 import java.util.Collections;
031 import java.util.List;
032
033 public class IntegerValueTypeConstructor implements TypeConstructor {
034 private final long value;
035 private final Collection<JetType> supertypes = Lists.newArrayList();
036
037 public IntegerValueTypeConstructor(long value) {
038 // order of types matters
039 // 'getPrimitiveNumberType' returns first of supertypes that is a subtype of expected type
040 // for expected type 'Any' result type 'Int' should be returned
041 this.value = value;
042 checkBoundsAndAddSuperType(value, (long) Integer.MIN_VALUE, (long) Integer.MAX_VALUE, KotlinBuiltIns.getInstance().getIntType());
043 checkBoundsAndAddSuperType(value, (long) Byte.MIN_VALUE, (long) Byte.MAX_VALUE, KotlinBuiltIns.getInstance().getByteType());
044 checkBoundsAndAddSuperType(value, (long) Short.MIN_VALUE, (long) Short.MAX_VALUE, KotlinBuiltIns.getInstance().getShortType());
045 supertypes.add(KotlinBuiltIns.getInstance().getLongType());
046 }
047
048 private void checkBoundsAndAddSuperType(long value, long minValue, long maxValue, JetType kotlinType) {
049 if (value >= minValue && value <= maxValue) {
050 supertypes.add(kotlinType);
051 }
052 }
053
054 @NotNull
055 @Override
056 public Collection<JetType> getSupertypes() {
057 return supertypes;
058 }
059
060 @NotNull
061 @Override
062 public List<TypeParameterDescriptor> getParameters() {
063 return Collections.emptyList();
064 }
065
066 @Override
067 public boolean isFinal() {
068 return false;
069 }
070
071 @Override
072 public boolean isDenotable() {
073 return false;
074 }
075
076 @Nullable
077 @Override
078 public ClassifierDescriptor getDeclarationDescriptor() {
079 return null;
080 }
081
082 @NotNull
083 @Override
084 public Annotations getAnnotations() {
085 return Annotations.EMPTY;
086 }
087
088 public Long getValue() {
089 return value;
090 }
091
092 @Override
093 public String toString() {
094 return "IntegerValueType(" + value + ")";
095 }
096 }