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.
18 * Created by IntelliJ IDEA.
22 * To change template for new class use
23 * Code Style | Class Templates options (Tools | IDE Options).
25 package com
.intellij
.codeInspection
.dataFlow
.value
;
27 import com
.intellij
.openapi
.util
.Comparing
;
28 import com
.intellij
.psi
.PsiKeyword
;
29 import com
.intellij
.psi
.PsiType
;
30 import com
.intellij
.util
.containers
.HashMap
;
31 import org
.jetbrains
.annotations
.NonNls
;
32 import org
.jetbrains
.annotations
.NotNull
;
34 import java
.util
.ArrayList
;
36 public class DfaTypeValue
extends DfaValue
{
37 public static class Factory
{
38 private final DfaTypeValue mySharedInstance
;
39 private final HashMap
<String
,ArrayList
<DfaTypeValue
>> myStringToObject
;
40 private final DfaValueFactory myFactory
;
42 Factory(DfaValueFactory factory
) {
44 mySharedInstance
= new DfaTypeValue(factory
);
45 myStringToObject
= new HashMap
<String
, ArrayList
<DfaTypeValue
>>();
49 public DfaTypeValue
create(PsiType type
, boolean nullable
) {
50 mySharedInstance
.myType
= type
;
51 mySharedInstance
.myCanonicalText
= type
.getCanonicalText();
52 mySharedInstance
.myIsNullable
= nullable
;
53 if (mySharedInstance
.myCanonicalText
== null) {
54 mySharedInstance
.myCanonicalText
= PsiKeyword
.NULL
;
57 String id
= mySharedInstance
.toString();
58 ArrayList
<DfaTypeValue
> conditions
= myStringToObject
.get(id
);
59 if (conditions
== null) {
60 conditions
= new ArrayList
<DfaTypeValue
>();
61 myStringToObject
.put(id
, conditions
);
63 for (DfaTypeValue aType
: conditions
) {
64 if (aType
.hardEquals(mySharedInstance
)) return aType
;
68 DfaTypeValue result
= new DfaTypeValue(type
, nullable
, myFactory
);
69 conditions
.add(result
);
73 public DfaTypeValue
create(PsiType type
) {
74 return create(type
, false);
78 private PsiType myType
;
79 private String myCanonicalText
;
80 private boolean myIsNullable
;
82 private DfaTypeValue(DfaValueFactory factory
) {
86 private DfaTypeValue(PsiType type
, boolean isNullable
, DfaValueFactory factory
) {
89 myIsNullable
= isNullable
;
90 myCanonicalText
= type
.getCanonicalText();
91 if (myCanonicalText
== null) {
92 myCanonicalText
= PsiKeyword
.NULL
;
96 public PsiType
getType() {
100 public boolean isNullable() {
105 public String
toString() {
106 return myCanonicalText
+ ", nullable=" + myIsNullable
;
109 private boolean hardEquals(DfaTypeValue aType
) {
110 return Comparing
.equal(myCanonicalText
, aType
.myCanonicalText
) && myIsNullable
== aType
.myIsNullable
;
113 public boolean isAssignableFrom(DfaTypeValue dfaType
) {
114 return dfaType
!= null && myType
.isAssignableFrom(dfaType
.myType
);
117 public boolean isConvertibleFrom(DfaTypeValue dfaType
) {
118 return dfaType
!= null && myType
.isConvertibleFrom(dfaType
.myType
);