Commit 17e71d10 authored by AtjonTV's avatar AtjonTV 🇷🇺

Release 3.1.0

* Added
  * 'smartMerge' extension to MutableMap (With value type converter)
  * 'fullMerge' extension to MutableMap (With value type converter)
  * 'mergeArrays' extension to MutableMap (With value type converter)
  * 'toListWithConvert' extension to List
  * 'toListWithConvert' extension to MutableList
  * 'fromUrl' extension to File
  * 'toFile' extension to URL
  * 'toObjectOfType' extension to URL
  * 'Http' class
  * 'Http.DataTypes' Enum Class
  * 'readText' extension to InputStream
  * 'fromHex' extension to String
  * 'fromHexToLong' extension to String
  * 'toHex' extension to Byte
  * 'binSearch' extension to IntArray (Using Recursion, its 50% faster)

* Changed
  * Moved Library API from Experimental to Stable
Signed-off-by: AtjonTV's avatarAtjonTV <thomas.obernosterer@atvg-studios.com>
parent f4aa6567
Pipeline #2015 passed with stages
in 1 minute and 18 seconds
......@@ -13,6 +13,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
### Security
## 3.1.0 - 12.06.2019
### Added
- `smartMerge` extension to MutableMap (With value type converter)
- `fullMerge` extension to MutableMap (With value type converter)
- `mergeArrays` extension to MutableMap (With value type converter)
- `toListWithConvert` extension to List
- `toListWithConvert` extension to MutableList
- `fromUrl` extension to File
- `toFile` extension to URL
- `toObjectOfType` extension to URL
- `Http` class
- `Http.DataTypes` Enum Class
- `readText` extension to InputStream
- `fromHex` extension to String
- `fromHexToLong` extension to String
- `toHex` extension to Byte
- `binSearch` extension to IntArray (Using Recursion, its 50% faster)
### Changed
- Moved Library API from Experimental to Stable
## 3.0.0 - 08.06.2019
### IMPORTANT NOTICE
......@@ -20,7 +42,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `smartMerge` extension to MutableMap
- `smartMerge` extension to MutableMap (With value type converter)
- `fullMerge` extension to MutableMap
- `mergeArrays` extension to MutableMap
- `openIfExist` parameter for ZipFile.open
......
......@@ -11,8 +11,8 @@ Breaking versions must be in a new row!
| 6 | 0.1.5 |
| 7 | 0.1.6 |
| 8 - 15| 0.2.0 - 1.4.0 |
|16 - 19| 2.0.0 - 2.1.1|
|20 | 3.0.0 |
|16 - 19| 2.0.0 - 2.1.1 |
|20 - 21| 3.0.0 - 3.1.0 |
This means: `0.1.2` **IS** compatible with `0.1.3` but **NOT** compatible with `0.1.0` and `0.1.1`.
......@@ -53,4 +53,3 @@ Table created from APIs marked as Experimental. These may change or be removed a
| Since | Change |
|-------|--------|
| 1.3.0 | Adding in Library to dynamically load Jars (`objects.library.Library`, `types.library.LClass`, `types.library.LFunction`) |
......@@ -7,7 +7,7 @@ plugins {
}
group 'klib'
version '3.0.0'
version '3.1.0'
repositories {
mavenCentral()
......
#!/bin/bash
VERSION="3.0.0"
VERSION="3.1.0"
mvn gpg:sign-and-deploy-file \
-DrepositoryId=atvg-studios \
......
......@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.atvgstudios</groupId>
<artifactId>klib</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
<name>kLib</name>
<description>kLib is a in Kotlin written Library by ATVG-Studios containing a set of useful Functions, Extensions and Types.</description>
<url>https://klib.atvg-studios.com/</url>
......
......@@ -32,4 +32,14 @@ infix fun Byte.mul(b: Byte): Byte {
}
return p.toByte()
}
/**
* Convert Byte into a Hex String
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun Byte.toHex(): String {
return this.toString(16)
}
\ No newline at end of file
package klib.extensions
import klib.annotations.Experimental
import klib.objects.hash.Sha1
import klib.objects.hash.Sha256
import klib.objects.library.Library
......@@ -10,6 +9,7 @@ import klib.types.library.LFunction
import klib.types.zip.ZipFile
import java.io.File
import java.io.FileNotFoundException
import java.net.URL
/**
* Add a file to a ZipFile
......@@ -30,9 +30,9 @@ fun File.addToZipFile(zipFile: ZipFile) = zipFile.addFile(this)
* @throws ClassNotFoundException
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@UseExperimental(Experimental::class)
@Throws(FileNotFoundException::class, ClassNotFoundException::class)
infix fun File.loadAsLibraryWithClass(className: String): LClass {
return Library.loadClassFromJar(this, className)
......@@ -49,9 +49,9 @@ infix fun File.loadAsLibraryWithClass(className: String): LClass {
* @throws NoSuchMethodException
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@UseExperimental(Experimental::class)
@Throws(FileNotFoundException::class, ClassNotFoundException::class, NoSuchMethodException::class)
fun File.loadAsLibraryWithFunction(className: String, functionName: String): LFunction {
return Library.loadFunctionFromJar(this, className, functionName)
......@@ -110,4 +110,14 @@ fun File.reset() {
} else {
this.createNewFile()
}
}
/**
* Download a URL into a File
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun File.fromUrl(url: URL) {
url.toFile(this)
}
\ No newline at end of file
......@@ -28,4 +28,14 @@ fun InputStream.toFile(file: File) {
fun InputStream.toFileInZipFile(file: File, zipFile: ZipFile) {
toFile(file)
zipFile.addFile(file)
}
/**
* Read a input streams content
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun InputStream.readText(): String {
return this.bufferedReader().readText()
}
\ No newline at end of file
package klib.extensions
/**
* Recursion based Binary Search (About 50% faster then binarySearch)
*
* @param element Element to search for
* @return Index when Found, -1 when not
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun IntArray.binSearch(element: Int, start: Int = 0, end: Int = size): Int {
fun binSearch(array: IntArray, element: Int, left: Int, right: Int): Int {
val middle = (left + right) / 2
return when {
left > right -> -1
array[middle] == element -> middle
array[middle] > element -> binSearch(array, element, left, middle - 1)
array[middle] < element -> binSearch(array, element, middle + 1, right)
else -> -1
}
}
return binSearch(this, element, start, end)
}
\ No newline at end of file
......@@ -20,4 +20,22 @@ fun <T : Comparable<T>> List<T>.quicksort(): List<T> {
return quicksort(less) + equal + quicksort(greater)
}
return quicksort(this)
}
/**
* Convert List A to List B with Type E
*
* @param convert Method to convert type T to E
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun <T, E> List<T>.toListWithConvert(convert: (T) -> E): List<E> {
val list: MutableList<E> = ArrayList()
this.forEach {
list.add(convert(it))
}
return list
}
\ No newline at end of file
......@@ -111,4 +111,22 @@ fun <T : Comparable<T>> MutableList<T>.quicksort() {
val res = quicksort(orig)
this.clear()
this.addAll(res)
}
/**
* Convert List A to List B with Type E
*
* @param convert Method to convert type T to E
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun <T, E> MutableList<T>.toListWithConvert(convert: (T) -> E): List<E> {
val list: MutableList<E> = ArrayList()
this.forEach {
list.add(convert(it))
}
return list
}
\ No newline at end of file
......@@ -22,7 +22,7 @@ fun <K, V> MutableMap<K, V>.smartMerge(other: Map<K, V>) {
* @param other The map to merge
* @param convertIt Converter function to put Any to type 2
*
* @since 3.0.0
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun <K, V> MutableMap<K, V>.smartMerge(other: Map<K, Any>, convertIt: (Any) -> V) {
......@@ -52,6 +52,26 @@ fun <K, V> MutableMap<K, V>.mergeArrays(keys: Array<K>, values: Array<V>) {
}
}
/**
* Merge a Key and a Value Array into a MutableMap; Duplicate keys will be ignored
*
* @param keys The Keys
* @param values The Values
* @param convertIt Converter function to put Any to type 2
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun <K, V> MutableMap<K, V>.mergeArrays(keys: Array<K>, values: Array<Any>, convertIt: (Any) -> V) {
if (keys.size == values.size) {
for (i in 0 until keys.size) {
if (keys[i] !in this) {
this[keys[i]] = convertIt(values[i])
}
}
}
}
/**
* Merge a Map into a MutableMap with overwriting existing keys
*
......@@ -64,4 +84,19 @@ fun <K, V> MutableMap<K, V>.fullMerge(other: Map<K, V>) {
other.forEach { (key, value) ->
this[key] = value
}
}
/**
* Merge a Map into a MutableMap with overwriting existing keys
*
* @param other The map to merge
* @param convertIt Converter function to put Any to type 2
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun <K, V> MutableMap<K, V>.fullMerge(other: Map<K, Any>, convertIt: (Any) -> V) {
other.forEach { (key, value) ->
this[key] = convertIt(value)
}
}
\ No newline at end of file
package klib.extensions
import klib.annotations.Experimental
import klib.exceptions.InvalidValueException
import klib.exceptions.RequireValueException
import klib.kLibInf
import klib.objects.base.Base58e
import klib.objects.hash.Sha1
import klib.objects.hash.Sha256
......@@ -11,6 +11,7 @@ import klib.types.library.LClass
import klib.types.library.LFunction
import java.io.File
import java.io.FileNotFoundException
import java.lang.reflect.Type
import java.util.Base64
/**
......@@ -242,9 +243,9 @@ fun String.asFile() = File(this)
* @see LClass
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@UseExperimental(Experimental::class)
@Throws(FileNotFoundException::class, ClassNotFoundException::class)
infix fun String.loadAsLibraryWithClass(className: String): LClass {
return Library.loadClassFromJar(this.asFile(), className)
......@@ -263,10 +264,43 @@ infix fun String.loadAsLibraryWithClass(className: String): LClass {
* @see LFunction
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@UseExperimental(Experimental::class)
@Throws(FileNotFoundException::class, ClassNotFoundException::class, NoSuchMethodException::class)
fun String.loadAsLibraryWithFunction(className: String, functionName: String): LFunction {
return Library.loadFunctionFromJar(this.asFile(), className, functionName)
}
/**
* Parse to Type
*
* @param type The type to parse
* @see klib.interfaces.Json
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun String.toObjectOfType(type: Type): Any? {
return kLibInf.jsonHandler.toObject(this, type)
}
/**
* Convert Hex String to Int
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun String.fromHex(): Int {
return this.toIntOrNull(16) ?: 0
}
/**
* Convert Hex String to Long
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun String.fromHextoLong(): Long {
return this.toLongOrNull(16) ?: 0L
}
\ No newline at end of file
package klib.extensions
import klib.kLibInf
import java.io.File
import java.lang.reflect.Type
import java.net.URL
/**
* Download a URL to File
*
* @param file File to download to
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun URL.toFile(file: File) {
this.openStream().toFile(file)
}
/**
* Download json and parse to Type
*
* @param type The type to parse to
* @see kLibInf.jsonHandler
* @see klib.interfaces.Json
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun URL.toObjectOfType(type: Type): Any? {
return kLibInf.jsonHandler.toObject(this.readText(), type)
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ object kLibInf {
* @since 2.0.0
* @author Thomas Obernosterer
*/
val semver = SemVer(3, 0, 0)
val semver = SemVer(3, 1, 0)
/**
* Company creating kLib
......
......@@ -13,9 +13,9 @@ import java.net.URLClassLoader
* Dynamically load Classes and Methods from Jar files
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@klib.annotations.Experimental
object Library {
/**
* Dynamically load a Class from a Jar File
......
package klib.types.http
import klib.extensions.readText
import klib.kLibInf
import java.io.DataOutputStream
import java.net.HttpURLConnection
import java.net.URL
/**
* Simple HTTP Client
*
* @param url URL for requests
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
class Http(val url: String) {
/**
* Send get Request
*
* @param optionalParams Appended to URL with & if ? already present; else with ?
* @return Request Response
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun get(optionalParams: String = ""): String {
val uri = "$url${if (url.contains("?")) "&" else "?"}$optionalParams"
val connection = URL(uri).openConnection() as HttpURLConnection
connection.doInput = true
connection.requestMethod = "GET"
connection.setRequestProperty("User-Agent", "kLib/${kLibInf.semver}")
return connection.inputStream.readText()
}
/**
* Send post Request
*
* @param data Data to send
* @param datatype Content-Type Header
* @return Request Response
*
* @since 3.1.0
* @author Thomas Obernosterer
*/
fun post(data: String, datatype: DataTypes): String {
val connection = URL(url).openConnection() as HttpURLConnection
connection.doOutput = true
connection.requestMethod = "POST"
connection.setRequestProperty("User-Agent", "kLib/${kLibInf.semver}")
when (datatype) {
DataTypes.JSON ->
connection.setRequestProperty("Content-Type", "application/json")
DataTypes.FORM ->
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
}
DataOutputStream(connection.outputStream).writeBytes(data)
return connection.inputStream.readText()
}
/**
* Supported Datatypes
*/
enum class DataTypes {
/**
* HTML Form data (application/x-www-form-urlencoded)
*/
FORM,
/**
* JSON data (application/json)
*/
JSON
}
}
\ No newline at end of file
......@@ -7,12 +7,10 @@ import java.lang.reflect.Modifier
/**
* Custom Class to handle Dynamically loaded Classes
*
* @param sourceClass A Class<*> that serves as the Host for further use
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
@klib.annotations.Experimental
class LClass(
private val sourceClass: Class<*>
) {
......
......@@ -9,6 +9,7 @@ import java.lang.reflect.Method
* @param sourceMethod A method instance (object) to run
*
* @since 1.3.0 (Experimental)
* @since 3.1.0
* @author Thomas Obernosterer
*/
class LFunction(
......
package devtests
import klib.extensions.binSearch
import kotlin.system.measureTimeMillis
fun main() {
binSearch(15)
binSearch(34)
binSearch(48)
binSearch(24)
binSearch(2)
binSearch(20)
}
fun binSearch(find: Int) {
val sortedInput = intArrayOf(1, 4, 5, 6, 8, 11, 12, 14, 16, 19, 20, 22, 23, 29, 31, 32, 33, 34, 35, 36, 38, 39, 42, 44, 52, 59, 60, 61, 62, 66, 67, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 84, 85, 86, 88, 89, 91, 93, 96)
val searchValue = sortedInput[find]
println("kLib: "+measureTimeMillis {
println(sortedInput.binSearch(searchValue))
}.toString()+" ms")
println("Kotlin: "+measureTimeMillis {
println(sortedInput.binarySearch(searchValue))
}.toString()+" ms")
}
\ No newline at end of file
package devtests
import klib.extensions.toListWithConvert
fun main() {
val listA = mutableListOf("1","3","8")
val listB = listA.toListWithConvert { it.toIntOrNull() ?: 0 }
println(listB)
}
\ No newline at end of file
package devtests
import klib.annotations.Experimental
import klib.extensions.asFile
import klib.types.zip.ZipFile
@UseExperimental(Experimental::class)
fun main() {
val zipFile = ZipFile("/stmp/Documents.zip")
zipFile.unzip("/stmp".asFile())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment