update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / dataFlow / value / DfaBoxedValue.java
blob8925f8d34c77cd7a51d3df1c4c6c74f68b1cb9f0
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInspection.dataFlow.value;
18 import com.intellij.psi.PsiVariable;
19 import com.intellij.util.containers.HashMap;
20 import gnu.trove.THashMap;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
25 import java.util.Map;
27 public class DfaBoxedValue extends DfaValue {
28 private final DfaValue myWrappedValue;
30 private DfaBoxedValue(DfaValue valueToWrap, DfaValueFactory factory) {
31 super(factory);
32 myWrappedValue = valueToWrap;
35 @NonNls
36 public String toString() {
37 return "Boxed "+myWrappedValue.toString();
40 public DfaValue getWrappedValue() {
41 return myWrappedValue;
44 public static class Factory {
45 private final Map<Object, DfaBoxedValue> cachedValues = new HashMap<Object, DfaBoxedValue>();
46 private final Map<Object, DfaBoxedValue> cachedNegatedValues = new HashMap<Object, DfaBoxedValue>();
47 private final DfaValueFactory myFactory;
49 public Factory(DfaValueFactory factory) {
50 myFactory = factory;
53 @Nullable
54 public DfaValue createBoxed(DfaValue valueToWrap) {
55 if (valueToWrap instanceof DfaUnboxedValue) return ((DfaUnboxedValue)valueToWrap).getVariable();
56 Object o = valueToWrap instanceof DfaConstValue
57 ? ((DfaConstValue)valueToWrap).getValue()
58 : valueToWrap instanceof DfaVariableValue ? ((DfaVariableValue)valueToWrap).getPsiVariable() : null;
59 if (o == null) return null;
60 Map<Object, DfaBoxedValue> map = valueToWrap instanceof DfaVariableValue && ((DfaVariableValue)valueToWrap).isNegated() ? cachedNegatedValues : cachedValues;
61 DfaBoxedValue boxedValue = map.get(o);
62 if (boxedValue == null) {
63 boxedValue = new DfaBoxedValue(valueToWrap, myFactory);
64 map.put(o, boxedValue);
66 return boxedValue;
69 private final Map<PsiVariable, DfaUnboxedValue> cachedUnboxedValues = new THashMap<PsiVariable, DfaUnboxedValue>();
70 private final Map<PsiVariable, DfaUnboxedValue> cachedNegatedUnboxedValues = new THashMap<PsiVariable, DfaUnboxedValue>();
72 @NotNull
73 public DfaValue createUnboxed(DfaValue value) {
74 if (value instanceof DfaBoxedValue) {
75 return ((DfaBoxedValue)value).getWrappedValue();
77 if (value instanceof DfaConstValue) {
78 if (value == value.myFactory.getConstFactory().getNull()) return DfaUnknownValue.getInstance();
79 return value;
81 DfaValue result;
82 if (value instanceof DfaVariableValue) {
83 PsiVariable var = ((DfaVariableValue)value).getPsiVariable();
84 Map<PsiVariable, DfaUnboxedValue> map = ((DfaVariableValue)value).isNegated() ? cachedNegatedUnboxedValues : cachedUnboxedValues;
85 result = map.get(var);
86 if (result == null) {
87 result = new DfaUnboxedValue((DfaVariableValue)value, myFactory);
88 map.put(var, (DfaUnboxedValue)result);
91 else {
92 result = DfaUnknownValue.getInstance();
94 return result;