Skip to content

Instantly share code, notes, and snippets.

View nbness2's full-sized avatar

Curtis Woodard nbness2

View GitHub Profile
@nbness2
nbness2 / BaseRandom.kt
Last active September 28, 2018 03:08
Kotlin RandomUtil
package KotlinSrc
import java.util.*
import kotlin.reflect.KCallable
import kotlin.reflect.KType
import kotlin.reflect.full.createType
abstract class BaseRandom {
private val getNextByType: HashMap<KType, KCallable<*>> = HashMap()
@nbness2
nbness2 / BaseRandom.java
Last active September 28, 2018 03:09
Java RandomUtil
package JavaSrc;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Random;
import java.lang.reflect.Method;
public abstract class BaseRandom {
private int toInt(boolean bool) { return bool ? 1 : 0; }
@nbness2
nbness2 / BaseRandom.py
Last active September 12, 2018 22:10
Python RandomUtil
import random
class BaseRandom: # Float\double takes way way too long lol just use random.random() [0, 1]
def __init__(self, rand: random.Random=None):
self.__next_by_type = {
"b": self.__nextbyte,
"s": self.__nextshort,
"i": self.__nextint,
"l": self.__nextlong,
@nbness2
nbness2 / BitFields.kt
Last active September 20, 2018 10:57
Bitfields
import java.math.BigInteger
import kotlin.IndexOutOfBoundsException
data class BitFieldInfo(val fieldName: String, val fieldSize: Int, val fieldValue: BigInteger=0.bi) {
init {
if (fieldValue.bitLength() > fieldSize) throw IndexOutOfBoundsException("$fieldValue (${fieldValue.bitLength()} bits) too big to fit in $fieldSize bits")
if (fieldValue < 0.bi) throw NotImplementedError("Signed numbers are not yet implemented for BitFields")
if (fieldSize < 1) throw IndexOutOfBoundsException("Can't have a negative amount of bits ($fieldName, $fieldSize)")
}
constructor(fieldName: String, fieldSize: Int, fieldValue: Byte): this(fieldName, fieldSize, fieldValue.toInt())
val changelog20181122 = TEXTBOX {
+"Changes to Tags and TagBuilders"
+listTag {
+listItem("No more specifying when to use newlines. Newlines are now ALWAYS added BEFORE a string (+\"string\") when using builder syntax. I decided to go with this rather than explicitly specifying automatic newlines because it honestly makes more visual sense.")
+listItem("Because of the above change, NewlineIdentifier class (the gay ass bool wrapper) has been removed")
+listItem("One-liner tags are easier to create. Now you would use something like "+italicText("listItem(\"http://www.google.com\")")+" rather than "+ italicText("listItem { -newlineBefore; -newlineAfter; +\"http://www.google.com/\" }"))
+listItem("The above change was enabled by adding an initialization parameter to FullTagBuilder as well as adding a few .invoke constructors to TypedTag")
+listItem("Fixed a bug with FullTag.toString(String) putting the given string in both the tag AND making it the tagged text. oops
@nbness2
nbness2 / JNSD.java
Last active October 7, 2021 11:34
Java nullable string declaration
String nullableString;
nullableString = "not null";
nullableString = null; // this is fine :)
System.out.println(nullableString); // working as expected!
@nbness2
nbness2 / KNSE.kt
Created October 7, 2021 11:34
Kotlin Nullable String Example
var nullableString: String?
nullableString = "not null"
nullableString = null
println(nullableString) // prints null as expected, wow!
@nbness2
nbness2 / KNNSE.kt
Created October 7, 2021 11:39
Kotlin Non-null String example`
var nullableString: String
nullableString = "not null"
nullableString = null // wait, the compiler no longer likes this. no compile for you!
println(nullableString) // we don't even get a chance to try!
@nbness2
nbness2 / MaybeNull.java
Last active October 7, 2021 12:00
Dubious String Function
import java.util.Random;
public final class MaybeNull {
public static String maybeNullString() {
boolean isStringNull = new Random().nextBoolean();
return isStringNull ? null : "not null";
}
}
@nbness2
nbness2 / maybeNull.kt
Last active October 7, 2021 12:14
Maybe Null String From In Kotlin
fun main() {
val maybeNull: String = MaybeNull.maybeNullString() // allowed because compiler will assume you know what's happening with the `String!`
println(maybeNull)
}