Skip to content

Instantly share code, notes, and snippets.

@wezhang
wezhang / mockito_doreturn.scala
Last active September 14, 2017 08:51
[Spurious ambiguous reference error in Scala]
Mockito.doReturn("fake hi", null.asInstanceOf[Array[Object]]: _* )
when(t.get()).thenReturn("a", Array[Object](): _*)
v.overloadedMethod(arg0, null.asInstanceOf[Array[Object]]: _*)
@wezhang
wezhang / verify_hdfs_path.java
Created September 12, 2017 05:50
[Verify handoop path]
val hadoopConf = spark.sparkContext.hadoopConfiguration
hadoopConf.addResource(new Path("D:\\src\\intellij\\conf\\core-site.xml"))
val path = new Path("sample.csv")
val fs = path.getFileSystem(hadoopConf)
println(fs.open(path).available())
fs.close()
@wezhang
wezhang / reflect.scala
Created September 7, 2017 08:33
[Scala reflection to get private methods or fields]
val dagScheduler = classOf[SparkContext].getDeclaredMethod("dagScheduler").invoke(sc)
val stageIdToStage = classOf[DAGScheduler].getDeclaredField("stageIdToStage").get(dagScheduler).asInstanceOf[HashMap[Int, Stage]]
val stage = stageIdToStage(stageInfo.stageId)
@wezhang
wezhang / iso8601_date_parse.java
Last active September 1, 2017 02:33
[Java ISO 8601 parse] Only jdk function without joda-time
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor accessor = timeFormatter.parse("2015-10-27T16:22:27.605-07:00");
Date date = Date.from(Instant.from(accessor));
System.out.println(date);
@wezhang
wezhang / tryParse.java
Created August 30, 2017 03:20
[Try parse int]
boolean tryParseInt(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@wezhang
wezhang / IJ_background_task_indicator.java
Created August 23, 2017 07:39
[Intellij background task indicator enable] #intellij
Task.Backgroundable checkSshCertBackgroundTask = new Task.Backgroundable(
submitModel.getProject(), "Verify Authentication...", false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
sleep(20 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
@wezhang
wezhang / avoid_null_check.java
Created August 10, 2017 02:51
[Avoid null check in Java chain call] Java 8 has Optional class can avoid null check in chain call.
Optional.of(new Outer())
.map(Outer::getNested)
.map(Nested::getInner)
.map(Inner::getFoo)
.ifPresent(System.out::println);
@wezhang
wezhang / phaser.java
Last active August 10, 2017 02:06
[Phaser for multi threads sync] The class Phaser is a multithreads sync helper class in Java #Phaser #sync
import java.util.concurrent.Phaser;
// Main thread
// Reset the debug process Phaser
debugProcessPhaser = new Phaser(1);
new Thread(() -> {
// new debug process start
debugProcessPhaser.register();
/**
* Retry observable subscription if timeout.
*
* For every retry it will wait delay + delayAmount so we wait more and more every retry.
*
* @param maxRetries number of retries
* @param delay milliseconds of wait between each try
* @param delayAmount delay + delayAmount
*/
class RetryAfterTimeoutWithDelay(val maxRetries: Int, var delay: Long, val delayAmount: Long = 100)
@wezhang
wezhang / enum.java
Created July 27, 2017 08:02
[Java types] samples for basic types
public enum UserStatus {
PENDING,
ACTIVE,
INACTIVE,
DELETED;
}
public enum WhoisRIR {
ARIN("whois.arin.net"),
RIPE("whois.ripe.net"),