Skip to content

Instantly share code, notes, and snippets.

View bangarharshit's full-sized avatar

Harshit Bangar bangarharshit

  • Twitter
View GitHub Profile
import com.trello.rxlifecycle2.LifecycleProvider
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
fun Disposable.disposeWith(compositeDisposable: CompositeDisposable) {
compositeDisposable.add(this)
}
fun <E> Disposable.disposeUntil(e: E, lifecycleProvider: LifecycleProvider<E>) {
public static void main(String[] args) throws InterruptedException {
Observable<String> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
@Override public void subscribe(ObservableEmitter<Integer> e) throws Exception {
System.out.println("emit" + Thread.currentThread().getName());
e.onNext(12);
e.onNext(3232);
}
})
.compose(integerStringObservableTransformer)
@bangarharshit
bangarharshit / SealedClasses.java
Last active November 18, 2017 02:04 — forked from rjrjr/java-sealed-class
Poor Man's Sealed Classes (visitor pattern)
/**
* For Java developers all excited by and jealous of Kotlin's sealed classes.
* Do this when you wish you could put parameters on an enum.
*/
public class PoorMan {
private interface Event {
<T> T dispatch(EventHandler<T> handler);
}
interface EventHandler<T> {
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;
import com.example.harshitbangar.ribswithoutdagger.ScreenStackImpl;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
public class CrossfadeTransition implements Transition {
@bangarharshit
bangarharshit / WeakReferenceCaller.java
Created December 22, 2017 11:01
Code to show that multiple calls to Weakreference.get() might return null.
// GC is a background thread and JVM doesn't have a spec for it to be daemon or not.
// https://stackoverflow.com/questions/5553783/java-garbage-collection-thread-priority
public class WeakReferenceCaller {
public static void main(String[] args) throws InterruptedException {
MyObject myObject = new MyObject();
Thread thread = new Thread(new WeakReferenceRunnable(myObject));
thread.start();
myObject = null;
System.out.println(myObject);
@bangarharshit
bangarharshit / EmailDummy.java
Created February 3, 2018 04:21
The current generated code (only the POJO part and removed adapter for brevity):
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class EmailDummy {
@Nonnull
public final String From;
// Given an array with n objects colored red, white or blue,
// sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
// Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
// Note: Using library sort function is not allowed.
// Example :
// Input : [0 1 2 0 1 2]
// Given a sorted linked list, delete all duplicates such that each element appear only once.
// For example,
// Given 1->1->2, return 1->2.
// Given 1->1->2->3->3, return 1->2->3.
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
public class InsertionSort {
public int[] sort(int[] input) {
int[] output = new int[input.length];
for (int i=0; i<input.length; i++) {
int indexToInsert = 0;
for (int j=0; j<=i; j++) {
if (input[i] < output[j]) {
indexToInsert=j;
public class Arbit {
public static void main(String[] args) {
Arbit arbit = new Arbit();
Container container = new Container();
container.a = 7;
container.b = 6;
arbit.change(container);
int d = 5;
arbit.changeInt(d);
System.out.println(container);