-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Began migration to using Java async driver for better performance and…
… reliability
- Loading branch information
1 parent
94df2b5
commit 96a3b59
Showing
86 changed files
with
587 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.outr.arango | ||
|
||
import java.util.concurrent.ThreadLocalRandom | ||
|
||
/** | ||
* Unique String generator | ||
*/ | ||
object Unique { | ||
lazy val LettersLower = "abcdefghijklmnopqrstuvwxyz" | ||
lazy val LettersUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
lazy val Numbers = "0123456789" | ||
lazy val Readable = "ABCDEFGHJKLMNPQRSTWXYZ23456789" | ||
lazy val Hexadecimal = s"${Numbers}abcdef" | ||
lazy val LettersAndNumbers = s"$LettersLower$Numbers" | ||
lazy val AllLettersAndNumbers = s"$LettersLower$LettersUpper$Numbers" | ||
|
||
/** | ||
* Random number generator used to generate unique values. Defaults to `threadLocalRandom`. | ||
*/ | ||
var random: Int => Int = threadLocalRandom | ||
|
||
/** | ||
* The default length to use for generating unique values. Defaults to 32. | ||
*/ | ||
var defaultLength: Int = 32 | ||
|
||
/** | ||
* The default characters to use for generating unique values. Defaults to AllLettersAndNumbers. | ||
*/ | ||
var defaultCharacters: String = AllLettersAndNumbers | ||
|
||
/** | ||
* Uses java.util.concurrent.ThreadLocalRandom to generate random numbers. | ||
* | ||
* @param max the maximum value to include | ||
* @return random number between 0 and max | ||
*/ | ||
final def threadLocalRandom(max: Int): Int = ThreadLocalRandom.current().nextInt(max) | ||
|
||
/** | ||
* Generates a unique String using the characters supplied at the length defined. | ||
* | ||
* @param length the length of the resulting String. Defaults to Unique.defaultLength. | ||
* @param characters the characters for use in the String. Defaults to Unique.defaultCharacters. | ||
* @return a unique String | ||
*/ | ||
def apply(length: Int = defaultLength, characters: String = defaultCharacters): String = { | ||
val charMax = characters.length | ||
(0 until length).map(i => characters.charAt(random(charMax))).mkString | ||
} | ||
|
||
/** | ||
* Convenience functionality to generate a UUID (https://en.wikipedia.org/wiki/Universally_unique_identifier) | ||
* | ||
* 32 characters of unique hexadecimal values with dashes representing 36 total characters | ||
*/ | ||
def uuid: String = { | ||
val a = apply(8, Hexadecimal) | ||
val b = apply(4, Hexadecimal) | ||
val c = apply(3, Hexadecimal) | ||
val d = apply(1, "89ab") | ||
val e = apply(3, Hexadecimal) | ||
val f = apply(12, Hexadecimal) | ||
s"$a-$b-4$c-$d$e-$f" | ||
} | ||
|
||
/** | ||
* Returns the number of possible values for a specific length and characters. | ||
*/ | ||
def poolSize(length: Int = 32, characters: String = AllLettersAndNumbers): Long = { | ||
math.pow(characters.length, length).toLong | ||
} | ||
} |
Oops, something went wrong.