This file contains hidden or 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
public class WeakReferenceResurrection { | |
public static void main(String[] args) { | |
var que = new ReferenceQueue<Object>(); | |
var weak = new WeakReference<>(new Object(), que); | |
System.out.println("gc..."); | |
System.gc(); | |
Reference polled; | |
while ((polled = que.poll()) == null) { |
This file contains hidden or 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
import java.lang.ref.PhantomReference; | |
import java.lang.ref.ReferenceQueue; | |
import java.util.Random; | |
public class PhantomReferenceExample { | |
static class CustomObject { | |
final int externalResourceId = new Random().nextInt(); // any resource descriptor, etc | |
} |
This file contains hidden or 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
import java.lang.ref.ReferenceQueue; | |
import java.lang.ref.WeakReference; | |
import java.util.Random; | |
public class WeakReferenceAsPhantom { | |
static class CustomObject { | |
final int externalResourceId = new Random().nextInt(); // any resource descriptor, etc | |
} | |
static class CustomFinalizer extends WeakReference<CustomObject> { |
This file contains hidden or 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
import org.antlr.v4.runtime.ParserRuleContext | |
import org.antlr.v4.runtime.tree.ParseTree | |
import org.antlr.v4.runtime.tree.TerminalNode | |
fun printSyntaxTree(root: ParserRuleContext): String { | |
fun recursive(aRoot: ParseTree, buf: StringBuilder, offset: Int) { | |
buf.append(" ".repeat(offset)) | |
if (aRoot is TerminalNode) { | |
buf.appendln("'${aRoot.text}'") | |
} else { |
This file contains hidden or 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
""" | |
REQUIREMENTS: | |
python 3.6+ (tested) | |
pip install plotly==3.3.0 | |
""" | |
import datetime | |
import itertools | |
import logging | |
import numbers |
This file contains hidden or 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
import time | |
import requests | |
url_base = 'http://localhost:9200' | |
# https://dzone.com/articles/23-useful-elasticsearch-example-queries | |
fill_data = True | |
# fill_data = False |
This file contains hidden or 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
import math | |
def percentile(data, perc: int): | |
return sorted(data)[int(math.ceil(len(data) * perc / 100)) - 1] | |
assert 9.0 == percentile([10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], 90) | |
assert 146 == percentile([142, 232, 290, 120, 274, 123, 146, 113, 272, 119, 124, 277, 207], 50) |
This file contains hidden or 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
{ | |
"took" : 7, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { |
This file contains hidden or 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
public static boolean match(@Nullable Pattern pattern, @Nullable String input) { | |
if (pattern == null || input == null) { | |
return false; | |
} | |
return pattern.matcher(input).matches(); | |
} | |
@Nullable | |
public static String toStr(@Nullable Object value) { | |
return (value != null) ? value.toString() : null; |
This file contains hidden or 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
enum Foo { | |
ONE, TWO; | |
private static class Helper { | |
public static Map<String, Foo> enumByName = new HashMap<>(); | |
static { | |
Arrays.stream(Foo.values()).forEach(it -> enumByName.put(it.name(), it)); | |
assert Foo.Helper.enumByName.get("ONE") == Foo.ONE; // usage | |
} | |
} | |
} |
OlderNewer