Day 2

Try to implement Decimal to Hex.


public static void main(String[] args) {
   int n = 4123;

   int power = 1;
   while (power <= (n / 16)) {
       power *= 16;
   }

   while (power > 0) {
       if (n >= power) {
           hexOutput(n, power);
           n = n - power;
       }else {
           hexOutput(n, power);
       }
       power = power / 16;
   }
}

private static void hexOutput(int n, int power) {
   switch (n / power) {
       case 15:
           System.out.println("F");
           break;
       case 14:
           System.out.println("E");
           break;
       case 13:
           System.out.println("D");
           break;
       case 12:
           System.out.println("C");
           break;
       case 11:
           System.out.println("B");
           break;
       case 10:
           System.out.println("A");
           break;
       default:
           System.out.println(n / power);
   }
}
2017/4/25 posted in  100DaysOfCode

Day 1

RxJava

The Observer pattern done right. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. — ractivex.io

Upon this, it's obvious that we should first familiar the Observer pattern, the Iterator pattern and the functional programming. I think I'll cover these three part later.

The Java version of this concept is called RxJava, which hosted on Github RxJava

Main Concept

  • Observables Representing sources of data.
  • Subscribers (Observers) Listening to the Observables.
  • a set of methods for modifying and composing the data

Some advantages of RxJava:
* You can chain async operations, e.g. if a API call depends on the call of another API
* A defined way to handle errors
* It reduces the need fro state variables, which can be the source of errors == Creating sources, subscribing to them and disposing them

Creating sources to observe

Observables are the sources for the data. Usually they start providing data once a subscriber starts listening. They can produce an empty, a single or multiple items. They can terminate with an error or success. Sources may never terminate, e.g., a observable for a button click can potentially produce an infinite stream of events.

Table 1. Table Observable types

Type Description
Flowable Emits 0 or n items and terminates with complete or an error. Supports backpressure, which allows to control how fast a source emits items.
Observable Emits 0 or n items and terminates with complete or an error.
Single Emits either a single item or an error. The reactive version of a method call. you subscribe to a Single and you get either a return value or an error.
Maybe Succeeds with an item, or no item, or errors. The reactive version of an Optional.
Completeable Either completes or returns an error. It never return items. The reactive version of a Runnable.

An example for the usage of backpressure is when you process touch events. You cannot control the user who is doing these touch events, but you can tell the source to emit the events on a slower rate in case you cannot processes them at the rate the user produces them. This has been designed for the support fro reactive streams. Only Flowable supports backpressure.

Observable<Interger> observable = Observable.create(new ObserverableOnSubscribe<Integer>(){
    @Override
    public void subscribe(@NonNull ObservableEmitter<Integer> observableEmitter) throws Exception {
        observableEmitter.onNext(1);
        observableEmitter.onNext(2);
        observableEmitter.onNext(3);
        observableEmitter.onComplete();
    }
});

Observer<Integer> observer = new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("On Subscribe");
            }

            @Override
            public void onNext(Integer value) {
                System.out.println("On Next" + value);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("On Error");
            }

            @Override
            public void onComplete() {
                System.out.println("On Complete");
            }
        };

        observable.subscribe(observer);
2017/4/23 posted in  100DaysOfCode