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

ai.platon.pulsar.skeleton.common.options.Converters.kt Maven / Gradle / Ivy

The newest version!
package ai.platon.pulsar.skeleton.common.options

import ai.platon.pulsar.common.SParser
import ai.platon.pulsar.common.browser.BrowserType
import ai.platon.pulsar.persist.metadata.FetchMode
import com.beust.jcommander.IStringConverter
import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.math.NumberUtils
import org.apache.commons.lang3.tuple.Pair
import java.awt.Dimension
import java.time.Duration
import java.time.Instant
import java.util.*

/**
 * Created by vincent on 17-4-7.
 * Copyright @ 2013-2023 Platon AI. All rights reserved
 */
class DurationConverter : IStringConverter {
    override fun convert(value: String): Duration {
        return SParser(value).duration
    }
}

class InstantConverter : IStringConverter {
    override fun convert(value: String): Instant {
        return SParser(value).getInstant(Instant.EPOCH)
    }
}

class PairConverter : IStringConverter> {
    override fun convert(value: String): Pair {
        val parts = value.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        return Pair.of(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]))
    }
}

class BrowserTypeConverter : IStringConverter {
    override fun convert(value: String): BrowserType {
        return BrowserType.fromString(value)
    }
}

class FetchModeConverter : IStringConverter {
    override fun convert(value: String): FetchMode {
        return FetchMode.fromString(value)
    }
}

enum class ItemExtractor {
    DEFAULT, BOILERPIPE;

    override fun toString(): String {
        return name.lowercase(Locale.getDefault())
    }

    companion object {
        fun fromString(s: String?): ItemExtractor {
            if (s.isNullOrEmpty()) {
                return DEFAULT
            }

            return try {
                valueOf(s.uppercase(Locale.getDefault()))
            } catch (e: Throwable) {
                DEFAULT
            }

        }
    }
}

class ItemExtractorConverter : IStringConverter {
    override fun convert(value: String): ItemExtractor {
        return ItemExtractor.fromString(value)
    }
}

/**
 * Created by vincent on 17-4-7.
 * Copyright @ 2013-2023 Platon AI. All rights reserved
 */
class WeightedKeywordsConverter : IStringConverter> {
    override fun convert(value: String): Map {
        var value0 = value
        val keywords = HashMap()
        value0 = StringUtils.remove(value0, ' ')
        val parts = value0.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        for (part in parts) {
            var k = part
            var v = "1"

            val pos = part.indexOf('^')
            if (pos >= 1 && pos < part.length - 1) {
                k = part.substring(0, pos)
                v = part.substring(pos + 1)
            }

            keywords[k] = NumberUtils.toDouble(v, 1.0)
        }

        return keywords
    }
}

class IntRangeConverter : IStringConverter {
    override fun convert(value: String): IntRange {
        val (a, b) = value.lowercase(Locale.getDefault()).split("..".toRegex())
        return IntRange(a.toInt(), b.toInt())
    }
}

class DimensionConverter : IStringConverter {
    override fun convert(value: String): Dimension {
        val (a, b) = value.lowercase(Locale.getDefault()).split("x".toRegex())
        return Dimension(a.toInt(), b.toInt())
    }
}

enum class Condition {
    BEST, BETTER, GOOD, WORSE, WORST;

    companion object {
        fun valueOfOrDefault(s: String?): Condition {
            return try {
                valueOf(s?.uppercase(Locale.getDefault()) ?: "GOOD")
            } catch (e: Throwable) {
                GOOD
            }
        }
    }
}

class ConditionConverter : IStringConverter {
    override fun convert(value: String) = Condition.valueOfOrDefault(value)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy