|
| 1 | +package com.intellij.lang.jsgraphql.schema.library.impl |
| 2 | + |
| 3 | +import com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity |
| 4 | +import com.intellij.platform.workspace.storage.ConnectionId |
| 5 | +import com.intellij.platform.workspace.storage.EntitySource |
| 6 | +import com.intellij.platform.workspace.storage.EntityType |
| 7 | +import com.intellij.platform.workspace.storage.GeneratedCodeApiVersion |
| 8 | +import com.intellij.platform.workspace.storage.GeneratedCodeImplVersion |
| 9 | +import com.intellij.platform.workspace.storage.MutableEntityStorage |
| 10 | +import com.intellij.platform.workspace.storage.WorkspaceEntity |
| 11 | +import com.intellij.platform.workspace.storage.WorkspaceEntityInternalApi |
| 12 | +import com.intellij.platform.workspace.storage.impl.ModifiableWorkspaceEntityBase |
| 13 | +import com.intellij.platform.workspace.storage.impl.WorkspaceEntityBase |
| 14 | +import com.intellij.platform.workspace.storage.impl.WorkspaceEntityData |
| 15 | +import com.intellij.platform.workspace.storage.impl.containers.MutableWorkspaceSet |
| 16 | +import com.intellij.platform.workspace.storage.impl.containers.toMutableWorkspaceSet |
| 17 | +import com.intellij.platform.workspace.storage.instrumentation.EntityStorageInstrumentation |
| 18 | +import com.intellij.platform.workspace.storage.instrumentation.EntityStorageInstrumentationApi |
| 19 | +import com.intellij.platform.workspace.storage.metadata.model.EntityMetadata |
| 20 | +import com.intellij.platform.workspace.storage.url.VirtualFileUrl |
| 21 | +import org.jetbrains.annotations.Nls |
| 22 | +import org.jetbrains.annotations.NonNls |
| 23 | + |
| 24 | +@GeneratedCodeApiVersion(3) |
| 25 | +@GeneratedCodeImplVersion(6) |
| 26 | +@OptIn(WorkspaceEntityInternalApi::class) |
| 27 | +internal class GraphQLLibraryEntityImpl(private val dataSource: GraphQLLibraryEntityData) : GraphQLLibraryEntity, WorkspaceEntityBase( |
| 28 | + dataSource) { |
| 29 | + |
| 30 | + private companion object { |
| 31 | + |
| 32 | + |
| 33 | + private val connections = listOf<ConnectionId>( |
| 34 | + ) |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + override val identifier: String |
| 39 | + get() { |
| 40 | + readField("identifier") |
| 41 | + return dataSource.identifier |
| 42 | + } |
| 43 | + |
| 44 | + override val displayName: String |
| 45 | + get() { |
| 46 | + readField("displayName") |
| 47 | + return dataSource.displayName |
| 48 | + } |
| 49 | + |
| 50 | + override val description: String? |
| 51 | + get() { |
| 52 | + readField("description") |
| 53 | + return dataSource.description |
| 54 | + } |
| 55 | + |
| 56 | + override val roots: Set<VirtualFileUrl> |
| 57 | + get() { |
| 58 | + readField("roots") |
| 59 | + return dataSource.roots |
| 60 | + } |
| 61 | + |
| 62 | + override val entitySource: EntitySource |
| 63 | + get() { |
| 64 | + readField("entitySource") |
| 65 | + return dataSource.entitySource |
| 66 | + } |
| 67 | + |
| 68 | + override fun connectionIdList(): List<ConnectionId> { |
| 69 | + return connections |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + internal class Builder(result: GraphQLLibraryEntityData?) : ModifiableWorkspaceEntityBase<GraphQLLibraryEntity, GraphQLLibraryEntityData>( |
| 74 | + result), GraphQLLibraryEntity.Builder { |
| 75 | + internal constructor() : this(GraphQLLibraryEntityData()) |
| 76 | + |
| 77 | + override fun applyToBuilder(builder: MutableEntityStorage) { |
| 78 | + if (this.diff != null) { |
| 79 | + if (existsInBuilder(builder)) { |
| 80 | + this.diff = builder |
| 81 | + return |
| 82 | + } |
| 83 | + else { |
| 84 | + error("Entity GraphQLLibraryEntity is already created in a different builder") |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + this.diff = builder |
| 89 | + addToBuilder() |
| 90 | + this.id = getEntityData().createEntityId() |
| 91 | + // After adding entity data to the builder, we need to unbind it and move the control over entity data to builder |
| 92 | + // Builder may switch to snapshot at any moment and lock entity data to modification |
| 93 | + this.currentEntityData = null |
| 94 | + |
| 95 | + index(this, "roots", this.roots) |
| 96 | + // Process linked entities that are connected without a builder |
| 97 | + processLinkedEntities(builder) |
| 98 | + checkInitialization() // TODO uncomment and check failed tests |
| 99 | + } |
| 100 | + |
| 101 | + private fun checkInitialization() { |
| 102 | + val _diff = diff |
| 103 | + if (!getEntityData().isEntitySourceInitialized()) { |
| 104 | + error("Field WorkspaceEntity#entitySource should be initialized") |
| 105 | + } |
| 106 | + if (!getEntityData().isIdentifierInitialized()) { |
| 107 | + error("Field GraphQLLibraryEntity#identifier should be initialized") |
| 108 | + } |
| 109 | + if (!getEntityData().isDisplayNameInitialized()) { |
| 110 | + error("Field GraphQLLibraryEntity#displayName should be initialized") |
| 111 | + } |
| 112 | + if (!getEntityData().isRootsInitialized()) { |
| 113 | + error("Field GraphQLLibraryEntity#roots should be initialized") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + override fun connectionIdList(): List<ConnectionId> { |
| 118 | + return connections |
| 119 | + } |
| 120 | + |
| 121 | + override fun afterModification() { |
| 122 | + val collection_roots = getEntityData().roots |
| 123 | + if (collection_roots is MutableWorkspaceSet<*>) { |
| 124 | + collection_roots.cleanModificationUpdateAction() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Relabeling code, move information from dataSource to this builder |
| 129 | + override fun relabel(dataSource: WorkspaceEntity, parents: Set<WorkspaceEntity>?) { |
| 130 | + dataSource as GraphQLLibraryEntity |
| 131 | + if (this.entitySource != dataSource.entitySource) this.entitySource = dataSource.entitySource |
| 132 | + if (this.identifier != dataSource.identifier) this.identifier = dataSource.identifier |
| 133 | + if (this.displayName != dataSource.displayName) this.displayName = dataSource.displayName |
| 134 | + if (this.description != dataSource?.description) this.description = dataSource.description |
| 135 | + if (this.roots != dataSource.roots) this.roots = dataSource.roots.toMutableSet() |
| 136 | + updateChildToParentReferences(parents) |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + override var entitySource: EntitySource |
| 141 | + get() = getEntityData().entitySource |
| 142 | + set(value) { |
| 143 | + checkModificationAllowed() |
| 144 | + getEntityData(true).entitySource = value |
| 145 | + changedProperty.add("entitySource") |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + override var identifier: String |
| 150 | + get() = getEntityData().identifier |
| 151 | + set(value) { |
| 152 | + checkModificationAllowed() |
| 153 | + getEntityData(true).identifier = value |
| 154 | + changedProperty.add("identifier") |
| 155 | + } |
| 156 | + |
| 157 | + override var displayName: String |
| 158 | + get() = getEntityData().displayName |
| 159 | + set(value) { |
| 160 | + checkModificationAllowed() |
| 161 | + getEntityData(true).displayName = value |
| 162 | + changedProperty.add("displayName") |
| 163 | + } |
| 164 | + |
| 165 | + override var description: String? |
| 166 | + get() = getEntityData().description |
| 167 | + set(value) { |
| 168 | + checkModificationAllowed() |
| 169 | + getEntityData(true).description = value |
| 170 | + changedProperty.add("description") |
| 171 | + } |
| 172 | + |
| 173 | + private val rootsUpdater: (value: Set<VirtualFileUrl>) -> Unit = { value -> |
| 174 | + val _diff = diff |
| 175 | + if (_diff != null) index(this, "roots", value) |
| 176 | + changedProperty.add("roots") |
| 177 | + } |
| 178 | + override var roots: MutableSet<VirtualFileUrl> |
| 179 | + get() { |
| 180 | + val collection_roots = getEntityData().roots |
| 181 | + if (collection_roots !is MutableWorkspaceSet) return collection_roots |
| 182 | + if (diff == null || modifiable.get()) { |
| 183 | + collection_roots.setModificationUpdateAction(rootsUpdater) |
| 184 | + } |
| 185 | + else { |
| 186 | + collection_roots.cleanModificationUpdateAction() |
| 187 | + } |
| 188 | + return collection_roots |
| 189 | + } |
| 190 | + set(value) { |
| 191 | + checkModificationAllowed() |
| 192 | + getEntityData(true).roots = value |
| 193 | + rootsUpdater.invoke(value) |
| 194 | + } |
| 195 | + |
| 196 | + override fun getEntityClass(): Class<GraphQLLibraryEntity> = GraphQLLibraryEntity::class.java |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +@OptIn(WorkspaceEntityInternalApi::class) |
| 201 | +internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEntity>() { |
| 202 | + lateinit var identifier: String |
| 203 | + lateinit var displayName: String |
| 204 | + var description: String? = null |
| 205 | + lateinit var roots: MutableSet<VirtualFileUrl> |
| 206 | + |
| 207 | + internal fun isIdentifierInitialized(): Boolean = ::identifier.isInitialized |
| 208 | + internal fun isDisplayNameInitialized(): Boolean = ::displayName.isInitialized |
| 209 | + internal fun isRootsInitialized(): Boolean = ::roots.isInitialized |
| 210 | + |
| 211 | + override fun wrapAsModifiable(diff: MutableEntityStorage): WorkspaceEntity.Builder<GraphQLLibraryEntity> { |
| 212 | + val modifiable = GraphQLLibraryEntityImpl.Builder(null) |
| 213 | + modifiable.diff = diff |
| 214 | + modifiable.id = createEntityId() |
| 215 | + return modifiable |
| 216 | + } |
| 217 | + |
| 218 | + @OptIn(EntityStorageInstrumentationApi::class) |
| 219 | + override fun createEntity(snapshot: EntityStorageInstrumentation): GraphQLLibraryEntity { |
| 220 | + val entityId = createEntityId() |
| 221 | + return snapshot.initializeEntity(entityId) { |
| 222 | + val entity = GraphQLLibraryEntityImpl(this) |
| 223 | + entity.snapshot = snapshot |
| 224 | + entity.id = entityId |
| 225 | + entity |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + override fun getMetadata(): EntityMetadata { |
| 230 | + return MetadataStorageImpl.getMetadataByTypeFqn("com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity") as EntityMetadata |
| 231 | + } |
| 232 | + |
| 233 | + override fun clone(): GraphQLLibraryEntityData { |
| 234 | + val clonedEntity = super.clone() |
| 235 | + clonedEntity as GraphQLLibraryEntityData |
| 236 | + clonedEntity.roots = clonedEntity.roots.toMutableWorkspaceSet() |
| 237 | + return clonedEntity |
| 238 | + } |
| 239 | + |
| 240 | + override fun getEntityInterface(): Class<out WorkspaceEntity> { |
| 241 | + return GraphQLLibraryEntity::class.java |
| 242 | + } |
| 243 | + |
| 244 | + override fun createDetachedEntity(parents: List<WorkspaceEntity.Builder<*>>): WorkspaceEntity.Builder<*> { |
| 245 | + return GraphQLLibraryEntity(identifier, displayName, roots, entitySource) { |
| 246 | + this.description = this@GraphQLLibraryEntityData.description |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + override fun getRequiredParents(): List<Class<out WorkspaceEntity>> { |
| 251 | + val res = mutableListOf<Class<out WorkspaceEntity>>() |
| 252 | + return res |
| 253 | + } |
| 254 | + |
| 255 | + override fun equals(other: Any?): Boolean { |
| 256 | + if (other == null) return false |
| 257 | + if (this.javaClass != other.javaClass) return false |
| 258 | + |
| 259 | + other as GraphQLLibraryEntityData |
| 260 | + |
| 261 | + if (this.entitySource != other.entitySource) return false |
| 262 | + if (this.identifier != other.identifier) return false |
| 263 | + if (this.displayName != other.displayName) return false |
| 264 | + if (this.description != other.description) return false |
| 265 | + if (this.roots != other.roots) return false |
| 266 | + return true |
| 267 | + } |
| 268 | + |
| 269 | + override fun equalsIgnoringEntitySource(other: Any?): Boolean { |
| 270 | + if (other == null) return false |
| 271 | + if (this.javaClass != other.javaClass) return false |
| 272 | + |
| 273 | + other as GraphQLLibraryEntityData |
| 274 | + |
| 275 | + if (this.identifier != other.identifier) return false |
| 276 | + if (this.displayName != other.displayName) return false |
| 277 | + if (this.description != other.description) return false |
| 278 | + if (this.roots != other.roots) return false |
| 279 | + return true |
| 280 | + } |
| 281 | + |
| 282 | + override fun hashCode(): Int { |
| 283 | + var result = entitySource.hashCode() |
| 284 | + result = 31 * result + identifier.hashCode() |
| 285 | + result = 31 * result + displayName.hashCode() |
| 286 | + result = 31 * result + description.hashCode() |
| 287 | + result = 31 * result + roots.hashCode() |
| 288 | + return result |
| 289 | + } |
| 290 | + |
| 291 | + override fun hashCodeIgnoringEntitySource(): Int { |
| 292 | + var result = javaClass.hashCode() |
| 293 | + result = 31 * result + identifier.hashCode() |
| 294 | + result = 31 * result + displayName.hashCode() |
| 295 | + result = 31 * result + description.hashCode() |
| 296 | + result = 31 * result + roots.hashCode() |
| 297 | + return result |
| 298 | + } |
| 299 | +} |
0 commit comments