All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.configurationcache.serialization.codecs.CollectionCodecs.kt Maven / Gradle / Ivy

/*
 * Copyright 2020 the original author or authors.
 *
 * 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.
 */

package org.gradle.configurationcache.serialization.codecs

import org.gradle.configurationcache.serialization.Codec
import org.gradle.configurationcache.serialization.codec
import org.gradle.configurationcache.serialization.readArray
import org.gradle.configurationcache.serialization.readCollectionInto
import org.gradle.configurationcache.serialization.readMapInto
import org.gradle.configurationcache.serialization.writeArray
import org.gradle.configurationcache.serialization.writeCollection
import org.gradle.configurationcache.serialization.writeMap
import java.util.LinkedList
import java.util.TreeMap
import java.util.TreeSet
import java.util.concurrent.ConcurrentHashMap


internal
val arrayListCodec: Codec> = collectionCodec { ArrayList(it) }


internal
val linkedListCodec: Codec> = collectionCodec { LinkedList() }


/**
 * Decodes HashSet instances as LinkedHashSet to preserve original iteration order.
 */
internal
val hashSetCodec: Codec> = collectionCodec { LinkedHashSet(it) }


internal
val treeSetCodec: Codec> = codec(
    {
        write(it.comparator())
        writeCollection(it)
    },
    {
        @Suppress("unchecked_cast")
        val comparator = read() as Comparator?
        readCollectionInto { TreeSet(comparator) }
    }
)


internal
fun > collectionCodec(factory: (Int) -> T) = codec(
    { writeCollection(it) },
    { readCollectionInto(factory) }
)


internal
val hashMapCodec: Codec> = mapCodec { HashMap(it) }


internal
val linkedHashMapCodec: Codec> = mapCodec { LinkedHashMap(it) }


internal
val concurrentHashMapCodec: Codec> = mapCodec { ConcurrentHashMap(it) }


internal
val treeMapCodec: Codec> = mapCodec { TreeMap() }


internal
fun > mapCodec(factory: (Int) -> T): Codec = codec(
    { writeMap(it) },
    { readMapInto(factory) }
)


internal
val arrayCodec: Codec> = codec(
    {
        writeArray(it) { element ->
            write(element)
        }
    },
    {
        readArray { read() }
    }
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy