10 Temmuz 2019 Çarşamba

EventBus Sınıfı

Giriş
Şu satırı dahil ederiz.
import com.google.common.eventbus.EventBus;
Açıklaması şöyle. Aynı JVM içinde haberleşmek içindir. Eğer asenkron olarak çalışmak istersek AsyncEventBus kullanılabilir
It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication
Açıklaması şöyle.

Why can't I do <magic thing> with EventBus?

EventBus is designed to deal with a large class of use cases really, really well. We prefer hitting the nail on the head for most use cases to doing decently on all use cases.
Additionally, making EventBus extensible -- and making it useful and productive to extend, while still allowing ourselves to make additions to the core EventBus API that don't conflict with any of your extensions -- is an extremely difficult problem.
If you really, really need magic thing X, that EventBus can't currently provide, you should file an issue, and then design your own alternative.
EventBus ve Unit Test
EventBus exception'ları yuttuğu için Assertions.assertFalse() gibi bir çağrı eventbus içinde yapılsa daha test başarısız olarak işaretlenmez. Buna dikkat etmek lazım.

Kullanım
1. Bir adet EventBus nesnesi yarat
2. EventBus nesnesinin register() metodunu kullanarak Listener tak. Listener'ların @Susbcribe ile işaretli metodları EventBus tarafından tanınır.
3. EventBus.post() ile event gönder

constructor
Şöyle yaparız.
EventBus bus = new EventBus();
post metodu
post metodu hemen döner ve event kuyruğa atılır. Bir event için tüm dinleyiciler çağrıldıktan sonra kuyruktaki yeni event işletilir.

Örnek
Şöyle yaparız.
bus.post ( new String("event!"));
Dinleyen metodu şöyle yaparız.
@Subscribe
public void consume(String event) {
   System.out.println("Got event: " + event);
}
Örnek
post metodu içinde başka event fırlatmak istersek şöyle yaparız. Elimizde şöyle bir kod olsun.
interface InternalEventCallback {
  void run();
}

class InternalEventCallbackHandler {

  @Subscribe
  public void internalEventHandler(InternalEventCallback r){
    r.run();
  }
}
Bu handler'ı register ederiz. Şöyle yaparız.
eventBus.register(new InternalEventCallbackHandler());
İlk handler içinde önce NestedEvent  gönderilir. Bunun sonucuna bakmadan InternalEventCallback da gönderilir. InternalEventCallback çalışınca NestedEvent işlenmiş olur. Onun sonucuna bakarak InternalEventCallback devam eder veya durur.
@Subscribe
public void parentEventHandler(ParentEvent parentEvent) {
  NestedEvent nestedEvent = createNestedEvent();
  eventBus.post(nestedEvent);
  eventBus.post(new InternalEventCallback() {
    @Override
    public void run() {
      if(nestedEvent.isCancelled()) {
        return;
      }

      Object anotherNestedEvent = createOtherNestedEvent();
      eventBus.post(anotherNestedEvent);
    }
  });
}
register metodu
Açıklaması şöyleEventBus'a register olan dinleyici sınıfların metodlarının Java'daki kalıtım hiyerarşisine göre de çağırılabileceği anlatılıyor. Örneğin dinleyici sınıf parametresi Object olan bir metod tanımlarsa EventBus ile gönderilen tüm mesajları dinleyebilir.
To listen for a common event supertype (such as EventObject or Object)...
...in traditional Java events: not easy.
...with EventBus: events are automatically dispatched to listeners of any supertype, allowing listeners for interface types or "wildcard listeners" for Object.
Otto isimli Guava tabanlı bir EventBus, Guava'daki bu çalışma şeklini değiştirmiş. Yeni bir dinleyici EventBus'a kayıt olunca sınıf hiyerarşisini dolaşmayarak sadece verilen metodu kaydetme yöntemini seçmiş.

Örnek
Şöyle yaparız.
bus.register(foo);
Örnek
Elimizde şöyle bir kod olsun.
public class ComponentRepainter {

  @Subscribe
  public void doSomething(EventObject e) {
    ((Component) e.getSource()).repaint();
  }
}
Elimizde şöyle bir kod olsun.
EventBus eventBus = ... ;
eventBus.register(new ComponentRepainter());
Her türlü event nesnesini ComponentRepainter nesnesinin de duyması için şöyle yaparız
MouseEvent e = ... ;
eventBus.post(e);
Örnek
Elimizde şöyle bir kod olsun.
class MyEvent[T] protected () { /* Your methods here */ }
class MyEventInt extends MyEvent[Int] {}
class MyEventString extends MyEvent[String] {}
Her bir event tipi için @Subscribe metodu yazarsak generic event'ler de gönderebiliriz.

unregister metodu
Bu sınıf WeakReference kullanmıyor dolayısıyla dinleyici sınıfın kod kullanılarak bus'tan çıkarılması gerekiyor.

ImmutableSet Sınıfı - Preserve Insertion Order

Giriş
Açıklaması şöyle
Guava’simmutable collections do not allow null elements (and, for maps, null values nor null keys). While this prevents a few use cases, in general it’s quite useful to know that you cannot have null elements in a collection.

They also preserve insertion order: an ImmutableSet can be iterated upon in the insertion order.
build metodu
ImmutableSet.Builder nesnesi döner.

copyOf metodu
Şöyle yaparız..
ImmutableSet.copyOf(heroes)
toImmutableSet metodu
Collector metodudur
Örnek
Şöyle yaparız.
collections
  .stream()
  .flatMap(collection -> loadKeys(...).stream())
  .collect(ImmutableSet.toImmutableSet());

9 Temmuz 2019 Salı

Collections2 Sınıfı

filter metodu
İmzası şöyle.
Collection<E> filter(Collection<E> collection, Predicate<? super E> predicate);
Açıklaması şöyle. Iterators.filter() metoduna benzer. Girdi olarak Collection alır, çıktı olarak Collection döner.
A few things to note here – first, the output of Collections.filter() is a live view of the original collection – changes to one will be reflected in the other.

It’s also important to understand that now, the result is constrained by the predicate – if we add an element that doesn’t satisfy that Predicate, an IllegalArgumentException will be thrown:
Örnek
Şöyle yaparız.
Collection2.filter(collection, () -> ...)

3 Temmuz 2019 Çarşamba

ImmutableBiMap Sınıfı

Giriş
ImmutableBiMap elemanların ekleme sırasını koruyor. Açıklaması şöyle.
We don't currently have a sorted BiMap type because it's a little ambiguous how it would work: would the entries be sorted by the keys? By the values? Would the forward entries be sorted by the keys, and the inverse entries be sorted by the values?

Currently, the only real available alternative is ImmutableBiMap, which retains the ordering that you put the entries in -- so ImmutableBiMap.copyOf(Maps.newTreeMap(map)) would give you a BiMap sorted by the keys.


BigIntegerMath Sınıfı

binomial metodu
Şöyle yaparız.
BigInteger binomial = BigIntegerMath.binomial(n, k);
factorial metodu
Şöyle yaparız.
BigInteger factorial = BigIntegerMath.factorial(n);

1 Temmuz 2019 Pazartesi

RangeSet Arayüzü

Giriş
Range sadece bir aralığı saklarken RangeSet arayüzünden kalıtan sınıflar ImmutableRangeSet, ve TreeRangeSet birden fazla aralığı saklayabilir.

add metodu
Belirtilen Range'i set'e ekler.

complement metodu
Anlamadım. Örnek ver

contains metodu
Bu arayüzün en önemli metodu contains() metodu. Belirtilen sayının aralıklarda olup olmadığını döner.

intersects metodu
Belirtilen Range'in set'te kısmen veya bütün olarak olup olmadığını döner.

remove metodu
Belirtilen Range'i set'ten siler

subRangeSet metodu
Belirtilen Range'in ile kesişen tüm sayıları döner.

span metodu
Boşluk olmayan aralığı döner.

.