mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cleanup
This commit is contained in:
@@ -28,9 +28,9 @@ import org.iq80.snappy.SnappyOutputStream
|
||||
import org.jdom.Element
|
||||
import org.jdom.output.Format
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.IOException
|
||||
import java.io.OutputStreamWriter
|
||||
import java.util.Arrays
|
||||
import java.util.TreeMap
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray
|
||||
|
||||
class StateMap private constructor(private val names: Array<String>, private val states: AtomicReferenceArray<Any>) {
|
||||
@@ -47,23 +47,23 @@ class StateMap private constructor(private val names: Array<String>, private val
|
||||
}
|
||||
|
||||
val names = map.keySet().toTypedArray()
|
||||
Arrays.sort(names)
|
||||
if (map !is TreeMap) {
|
||||
Arrays.sort(names)
|
||||
}
|
||||
|
||||
val states = AtomicReferenceArray<Any>(names.size())
|
||||
var i = 0
|
||||
val n = names.size()
|
||||
while (i < n) {
|
||||
for (i in names.indices) {
|
||||
states.set(i, map.get(names[i]))
|
||||
i++
|
||||
}
|
||||
return StateMap(names, states)
|
||||
}
|
||||
|
||||
public fun stateToElement(key: String, state: Any?, newLiveStates: Map<String, Element> = emptyMap<String, Element>()): Element {
|
||||
public fun stateToElement(key: String, state: Any?, newLiveStates: Map<String, Element>? = null): Element {
|
||||
if (state is Element) {
|
||||
return state.clone()
|
||||
}
|
||||
else {
|
||||
return newLiveStates.get(key) ?: unarchiveState(state as ByteArray)
|
||||
return newLiveStates?.get(key) ?: unarchiveState(state as ByteArray)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,13 +73,15 @@ class StateMap private constructor(private val names: Array<String>, private val
|
||||
return null
|
||||
}
|
||||
else if (LOG.isDebugEnabled() && SystemProperties.getBooleanProperty("idea.log.changed.components", false)) {
|
||||
fun stateToString(state: Any) = JDOMUtil.writeParent(state as? Element ?: unarchiveState(state as ByteArray), "\n")
|
||||
|
||||
val before = stateToString(oldState)
|
||||
val after = stateToString(newState)
|
||||
if (before == after) {
|
||||
LOG.debug("Serialization error: serialized are different, but unserialized are equal")
|
||||
}
|
||||
else {
|
||||
LOG.debug(key + " " + StringUtil.repeat("=", 80 - key.length()) + "\nBefore:\n" + before + "\nAfter:\n" + after)
|
||||
LOG.debug("$key ${StringUtil.repeat("=", 80 - key.length())}\nBefore:\n$before\nAfter:\n$after")
|
||||
}
|
||||
}
|
||||
return newBytes
|
||||
@@ -87,42 +89,15 @@ class StateMap private constructor(private val names: Array<String>, private val
|
||||
|
||||
private fun archiveState(state: Element): ByteArray {
|
||||
val byteOut = BufferExposingByteArrayOutputStream()
|
||||
try {
|
||||
val writer = OutputStreamWriter(SnappyOutputStream(byteOut), CharsetToolkit.UTF8_CHARSET)
|
||||
try {
|
||||
val xmlOutputter = JDOMUtil.MyXMLOutputter()
|
||||
xmlOutputter.setFormat(XML_FORMAT)
|
||||
xmlOutputter.output(state, writer)
|
||||
}
|
||||
finally {
|
||||
writer.close()
|
||||
}
|
||||
OutputStreamWriter(SnappyOutputStream(byteOut), CharsetToolkit.UTF8_CHARSET).use {
|
||||
val xmlOutputter = JDOMUtil.MyXMLOutputter()
|
||||
xmlOutputter.setFormat(XML_FORMAT)
|
||||
xmlOutputter.output(state, it)
|
||||
}
|
||||
catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
|
||||
return ArrayUtil.realloc(byteOut.getInternalBuffer(), byteOut.size())
|
||||
}
|
||||
|
||||
private fun unarchiveState(state: ByteArray) = JDOMUtil.load(SnappyInputStream(ByteArrayInputStream(state)))
|
||||
|
||||
fun stateToString(state: Any): String {
|
||||
val element: Element
|
||||
if (state is Element) {
|
||||
element = state
|
||||
}
|
||||
else {
|
||||
try {
|
||||
element = unarchiveState(state as ByteArray)
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
LOG.error(e)
|
||||
return "internal error"
|
||||
}
|
||||
}
|
||||
return JDOMUtil.writeParent(element, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
public fun toMutableMap(): MutableMap<String, Any> {
|
||||
@@ -136,31 +111,20 @@ class StateMap private constructor(private val names: Array<String>, private val
|
||||
/**
|
||||
* Sorted by name.
|
||||
*/
|
||||
fun keys(): Array<String> {
|
||||
return names
|
||||
}
|
||||
fun keys() = names
|
||||
|
||||
public fun get(key: String): Any? {
|
||||
val index = Arrays.binarySearch(names, key)
|
||||
return if (index < 0) null else states.get(index)
|
||||
}
|
||||
|
||||
public fun getElement(key: String, newLiveStates: Map<String, Element>): Element {
|
||||
return stateToElement(key, get(key), newLiveStates)
|
||||
}
|
||||
fun getElement(key: String, newLiveStates: Map<String, Element>) = stateToElement(key, get(key), newLiveStates)
|
||||
|
||||
public fun isEmpty(): Boolean {
|
||||
return names.size() == 0
|
||||
}
|
||||
fun isEmpty() = names.isEmpty()
|
||||
|
||||
public fun getState(key: String): Element? {
|
||||
val state = get(key)
|
||||
return if (state is Element) state else null
|
||||
}
|
||||
fun getState(key: String) = get(key) as? Element
|
||||
|
||||
public fun hasState(key: String): Boolean {
|
||||
return get(key) is Element
|
||||
}
|
||||
fun hasState(key: String) = get(key) is Element
|
||||
|
||||
public fun hasStates(): Boolean {
|
||||
if (isEmpty()) {
|
||||
@@ -194,16 +158,7 @@ class StateMap private constructor(private val names: Array<String>, private val
|
||||
return null
|
||||
}
|
||||
|
||||
val state = states.get(index)
|
||||
if (state !is Element) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (states.compareAndSet(index, state, archiveState(state))) {
|
||||
return state
|
||||
}
|
||||
else {
|
||||
return getStateAndArchive(key)
|
||||
}
|
||||
val state = states.get(index) as? Element ?: return null
|
||||
return if (states.compareAndSet(index, state, archiveState(state))) state else getStateAndArchive(key)
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,6 @@ import com.intellij.util.containers.SmartHashSet
|
||||
import gnu.trove.THashMap
|
||||
import org.jdom.Attribute
|
||||
import org.jdom.Element
|
||||
import org.jdom.JDOMException
|
||||
import java.io.IOException
|
||||
|
||||
abstract class XmlElementStorage protected constructor(protected val fileSpec: String,
|
||||
@@ -60,7 +59,6 @@ abstract class XmlElementStorage protected constructor(protected val fileSpec: S
|
||||
return if (element == null) StateMap.EMPTY else loadState(element)
|
||||
}
|
||||
|
||||
throws(IOException::class, JDOMException::class)
|
||||
private fun loadDataFromProvider() = JDOMUtil.load(provider!!.loadContent(fileSpec, roamingType))
|
||||
|
||||
private fun loadState(element: Element): StateMap {
|
||||
|
||||
Reference in New Issue
Block a user