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.io.File | |
fun main(args: Array<String>) { | |
val result = DayOneTask.run() | |
println(result) | |
} | |
object DayOneTask { |
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
// Acquire a reference to the system Location Manager | |
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | |
// Define a listener that responds to location updates | |
LocationListener locationListener = new LocationListener() { | |
public void onLocationChanged(Location location) { | |
// Called when a new location is found by the network location provider. | |
makeUseOfNewLocation(location); | |
} |
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
service.loadDataWithoutAnyParsingAtAll() | |
.flatMapObservable { responseBody -> | |
Observable.create<DataItem> { emitter -> | |
JsonReader(responseBody.charStream()) | |
.use { reader -> | |
while (reader.hasNext()) { | |
if (reader.peek() == JsonToken.BEGIN_OBJECT) { | |
reader.beginObject() | |
if (reader.nextName() == "data") { |
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
class JsonReaderObservable<T>(elementType: Type, gson: Gson, private val reader: JsonReader) : Observable<T>() { | |
private val subject = ReplaySubject.create<T>() | |
init { | |
reader.beginArray() | |
while (reader.hasNext()) { | |
subject.onNext(gson.fromJson(reader, elementType)) | |
} | |
reader.endArray() |
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
data class NewDataHolder( | |
@JsonAdapter(ObservableTypeAdapterFactory::class) | |
@SerializedName("data") | |
val data: Observable<DataItem> | |
) | |
class ObservableTypeAdapterFactory : TypeAdapterFactory { |
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
service.loadDataWithNewTypeConverter() | |
.flatMapObservable { | |
it.data | |
} | |
.count() | |
.subscribe({ result -> | |
println("Size is: $result") | |
}, { error -> error.printStackTrace() }) |
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
data class NewDataHolder( | |
@SerializedName("data") | |
val data: Observable<DataItem> | |
) |
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
interface Api { | |
@GET("thenixan-blogposts/json-streaming-data/master/one-large-file.json") | |
fun loadDataInUsualWay(): Single<DataHolder> | |
} | |
val service = Retrofit.Builder() | |
.baseUrl("https://raw.githubusercontent.com/") | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
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
fun benchmark(f: () -> Unit) { | |
System.gc() | |
val startTime = System.currentTimeMillis() | |
val startMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() | |
f.invoke() | |
System.gc() | |
val finishTime = System.currentTimeMillis() | |
val finishMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() |
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
data class DataHolder( | |
@SerializedName("data") val data: Array<DataItem> | |
) | |
data class DataItem( | |
@SerializedName("about") val about: String, | |
@SerializedName("email") val email: String, | |
@SerializedName("first_name") val firstName: String, | |
@SerializedName("gender") val gender: String, | |
@SerializedName("hash") val hash: String, |
NewerOlder