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.calls.smartcasts;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.types.ErrorUtils;
022    import org.jetbrains.kotlin.types.JetType;
023    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
024    
025    public class DataFlowValue {
026        
027        public static final DataFlowValue NULL = new DataFlowValue(new Object(), KotlinBuiltIns.getInstance().getNullableNothingType(), false, Nullability.NULL);
028        public static final DataFlowValue NULLABLE = new DataFlowValue(new Object(), KotlinBuiltIns.getInstance().getNullableAnyType(), false, Nullability.UNKNOWN);
029        public static final DataFlowValue ERROR = new DataFlowValue(new Object(), ErrorUtils.createErrorType("Error type for data flow"), false, Nullability.IMPOSSIBLE);
030    
031        private final boolean stableIdentifier;
032        private final JetType type;
033        private final Object id;
034        private final Nullability immanentNullability;
035    
036        // Use DataFlowValueFactory
037        /*package*/ DataFlowValue(Object id, JetType type, boolean stableIdentifier, Nullability immanentNullability) {
038            this.stableIdentifier = stableIdentifier;
039            this.type = type;
040            this.id = id;
041            this.immanentNullability = immanentNullability;
042        }
043    
044        @Nullable
045        public Object getId() {
046            return id;
047        }
048    
049        @NotNull
050        public Nullability getImmanentNullability() {
051            return immanentNullability;
052        }
053    
054        /**
055         * Stable identifier is a non-literal value that is statically known to be immutable
056         * @return
057         */
058        public boolean isStableIdentifier() {
059            return stableIdentifier;
060        }
061    
062        @NotNull
063        public JetType getType() {
064            return type;
065        }
066    
067        @Override
068        public boolean equals(Object o) {
069            if (this == o) return true;
070            if (o == null || getClass() != o.getClass()) return false;
071    
072            DataFlowValue that = (DataFlowValue) o;
073    
074            if (stableIdentifier != that.stableIdentifier) return false;
075            if (id != null ? !id.equals(that.id) : that.id != null) return false;
076            if (type != null ? !type.equals(that.type) : that.type != null) return false;
077    
078            return true;
079        }
080    
081        @Override
082        public String toString() {
083            return (stableIdentifier ? "stable " : "unstable ") + (id == null ? null : id.toString()) + " " + immanentNullability;
084        }
085    
086        @Override
087        public int hashCode() {
088            int result = (stableIdentifier ? 1 : 0);
089            result = 31 * result + (type != null ? type.hashCode() : 0);
090            result = 31 * result + (id != null ? id.hashCode() : 0);
091            return result;
092        }
093    }