com.zeoflow.depot.processor.cache.Cache.kt Maven / Gradle / Ivy
Show all versions of depot-compiler Show documentation
/*
* Copyright (C) 2021 ZeoFlow SRL
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("AddVarianceModifier")
package com.zeoflow.depot.processor.cache
import com.zeoflow.depot.compiler.processing.XElement
import com.zeoflow.depot.compiler.processing.XType
import com.zeoflow.depot.processor.FieldProcessor
import com.zeoflow.depot.vo.EmbeddedField
import com.zeoflow.depot.vo.Entity
import com.zeoflow.depot.vo.Pojo
import com.zeoflow.depot.vo.Warning
import java.util.LinkedHashSet
/**
* A cache key can be used to avoid re-processing elements.
*
* Each context has a cache variable that uses the same backing storage as the Root Context but
* adds current adapters and warning suppression list to the key.
*/
class Cache(
val parent: Cache?,
val converters: LinkedHashSet,
val suppressedWarnings: Set
) {
val entities: Bucket = Bucket(parent?.entities)
val pojos: Bucket = Bucket(parent?.pojos)
inner class Bucket(source: Bucket?) {
private val entries: MutableMap, T> = source?.entries ?: mutableMapOf()
fun get(key: K, calculate: () -> T): T {
val fullKey = FullKey(converters, suppressedWarnings, key)
return entries.getOrPut(
fullKey,
{
calculate()
}
)
}
}
/**
* Key for Entity cache
*/
data class EntityKey(val element: XElement)
/**
* Key for Pojo cache
*/
data class PojoKey(
val element: XElement,
val scope: FieldProcessor.BindingScope,
val parent: EmbeddedField?
)
/**
* Internal key representation with adapters & warnings included.
*
* Converters are kept in a linked set since the order is important for the TypeAdapterStore.
*/
private data class FullKey(
val converters: LinkedHashSet,
val suppressedWarnings: Set,
val key: T
)
}