forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersionStateManager.java
220 lines (196 loc) · 7.28 KB
/
VersionStateManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2uct.versioning;
import com.intellij.openapi.project.Project;
import com.magento.idea.magento2uct.packages.SupportedVersion;
import com.magento.idea.magento2uct.settings.UctSettingsService;
import com.magento.idea.magento2uct.util.php.MagentoTypeEscapeUtil;
import com.magento.idea.magento2uct.versioning.indexes.data.ApiCoverageStateIndex;
import com.magento.idea.magento2uct.versioning.indexes.data.DeprecationStateIndex;
import com.magento.idea.magento2uct.versioning.indexes.data.ExistenceStateIndex;
import com.magento.idea.magento2uct.versioning.indexes.data.VersionStateIndex;
import java.util.LinkedList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
public final class VersionStateManager {
private static VersionStateManager instance;
private final DeprecationStateIndex deprecationStateIndex;
private final ExistenceStateIndex existenceStateIndex;
private final ApiCoverageStateIndex apiCoverageStateIndex;
private final Boolean isSetIgnoreFlag;
private SupportedVersion currentVersion;
private SupportedVersion targetVersion;
private final List<SupportedVersion> versionsToLoad;
/**
* Get instance of the version state manager.
*
* @param project Project
*
* @return VersionStateManager
*/
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
public static synchronized VersionStateManager getInstance(
final @NotNull Project project
) { //NOPMD
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
if (instance == null
|| !instance.isValidFor(settingsService.shouldIgnoreCurrentVersion(),
settingsService.getCurrentVersionOrDefault(),
settingsService.getTargetVersion()
)) {
instance = new VersionStateManager(project);
}
return instance;
}
/**
* Check if specified FQN exists in the deprecation index.
*
* @param fqn String
*
* @return boolean
*/
public boolean isDeprecated(final @NotNull String fqn) {
return deprecationStateIndex.has(escapeFqn(fqn));
}
/**
* Get deprecated in version for the specified FQN.
*
* @param fqn String
*
* @return String
*/
public String getDeprecatedInVersion(final @NotNull String fqn) {
return deprecationStateIndex.getVersion(escapeFqn(fqn));
}
/**
* Check if specified FQN is exists in the existence index.
*
* @param fqn String
*
* @return boolean
*/
public boolean isExists(final @NotNull String fqn) {
return existenceStateIndex.has(escapeFqn(fqn));
}
/**
* Get removed in version for the specified FQN.
*
* @param fqn String
*
* @return String
*/
public String getRemovedInVersion(final @NotNull String fqn) {
return existenceStateIndex.getVersion(escapeFqn(fqn));
}
/**
* Check if specified FQN is marked as API.
*
* @param fqn String
*
* @return boolean
*/
public boolean isApi(final @NotNull String fqn) {
return apiCoverageStateIndex.has(escapeFqn(fqn));
}
/**
* Version state manager constructor.
*/
private VersionStateManager(final @NotNull Project project) {
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
isSetIgnoreFlag = settingsService.shouldIgnoreCurrentVersion();
currentVersion = settingsService.getCurrentVersionOrDefault();
targetVersion = settingsService.getTargetVersion();
versionsToLoad = new LinkedList<>();
// Correct settings if stored data isn't valid for current supported versions state.
correctSettings(project);
deprecationStateIndex = new DeprecationStateIndex();
compute(deprecationStateIndex);
existenceStateIndex = new ExistenceStateIndex();
compute(existenceStateIndex);
apiCoverageStateIndex = new ApiCoverageStateIndex(existenceStateIndex.getIndexData());
compute(apiCoverageStateIndex);
}
/**
* Correct settings if corrupted.
*
* @param project Project
*/
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
private synchronized void correctSettings(final @NotNull Project project) {
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
final List<String> allVersions = SupportedVersion.getSupportedVersions();
if (currentVersion == null
|| SupportedVersion.getVersion(currentVersion.getVersion()) == null) {
final SupportedVersion correctCurrentVersion = SupportedVersion.getVersion(
allVersions.get(0)
);
settingsService.setCurrentVersion(correctCurrentVersion);
currentVersion = correctCurrentVersion;
}
if (targetVersion == null
|| SupportedVersion.getVersion(targetVersion.getVersion()) == null) {
final SupportedVersion correctTargetVersion = SupportedVersion.getVersion(
allVersions.get(allVersions.size() - 1)
);
settingsService.setTargetVersion(correctTargetVersion);
targetVersion = correctTargetVersion;
}
}
/**
* Check if current instance is valid for settings.
*
* @param isSetIgnoreFlag boolean
* @param currentVersion SupportedVersion
* @param targetVersion SupportedVersion
*
* @return boolean
*/
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
private synchronized boolean isValidFor(
final Boolean isSetIgnoreFlag,
final SupportedVersion currentVersion,
final SupportedVersion targetVersion
) {
return this.isSetIgnoreFlag.equals(isSetIgnoreFlag)
&& this.currentVersion.equals(currentVersion)
&& this.targetVersion.equals(targetVersion);
}
/**
* Compute index data.
*
* @param index VersionStateIndex
*/
@SuppressWarnings({"PMD.CognitiveComplexity", "PMD.AvoidDeeplyNestedIfStmts"})
private void compute(final VersionStateIndex index) {
if (targetVersion == null) {
return;
}
if (versionsToLoad.isEmpty()) {
for (final SupportedVersion version : SupportedVersion.values()) {
if (version.compareTo(targetVersion) <= 0) {
if (isSetIgnoreFlag != null && isSetIgnoreFlag) {
// If current version is NULL, it is less than minimum supported version.
if (currentVersion == null || version.compareTo(currentVersion) > 0) {
versionsToLoad.add(version);
}
} else {
versionsToLoad.add(version);
}
}
}
}
index.load(versionsToLoad);
}
/**
* Escape FQN for adding Factory and Proxy support.
*
* @param fqn String
*
* @return String
*/
private String escapeFqn(final @NotNull String fqn) {
return MagentoTypeEscapeUtil.escape(fqn);
}
}