15 Aralık 2019 Pazar

ImmutableSortedMultiset Sınıfı

copyOf metodu
Şöyle yaparız.
List<String> temp = Stream.of("b, c", "a", "  ", "a, c")
            .filter(StringUtils::isNotBlank)
            .map(val -> val.split(","))
            .flatMap(Arrays::stream)
            .map(String::trim)
            .collect(Collectors.toList());

    Multiset<String> multiset = ImmutableSortedMultiset.copyOf(temp);

System.out.println("As list: " + temp);
System.out.println("As multiset: " + multiset);
// Output is:
// As list: [b, c, a, a, c]
// As multiset: [a x 2, b, c x 2]
naturalOrder metodu
Şöyle yaparız.
.collect(Collector.of(
    ImmutableSortedMultiset::<String>naturalOrder,
    ImmutableSortedMultiset.Builder::add,
    (b1, b2) -> {b1.addAll(b2.build()); return b1;},
    ImmutableSortedMultiset.Builder::build)
);
toImmutableSortedMultiset metodu
Şöyle yaparız.
.collect(ImmutableSortedMultiset.toImmutableSortedMultiset(Comparator.naturalOrder()));

RemovalListener Arayüzü

onRemoval metodu
Örnek
Şöyle yaparız.
CacheLoader<Key, DatabaseConnection> loader = new CacheLoader<Key,DatabaseConnection> (){
  public DatabaseConnection load(Key key) throws Exception {
    return openConnection(key);
  }
};
RemovalListener<Key, DatabaseConnection> removalListener = new
  RemovalListener<Key,DatabaseConnection>() {
    public void onRemoval(RemovalNotification<Key, DatabaseConnection> removal) {
      DatabaseConnection conn = removal.getValue();
      conn.close(); // tear down properly
    }
};

return CacheBuilder.newBuilder()
  .expireAfterWrite(2, TimeUnit.MINUTES)
  .removalListener(removalListener)
  .build(loader);
Örnek
Şöyle yaparız
CacheBuilder.newBuilder().maximumSize(250)
  .expireAfterWrite(15, TimeUnit.SECONDS)
  .removalListener(new RemovalListener<String, Path>(){

    @Override
    public void onRemoval(RemovalNotification<String, Path> notification){
      deleteTemporaryFile(notification.getValue());
    }
}).build();


11 Aralık 2019 Çarşamba

Closeables Sınıfı

Giriş
Şu satırı dahil ederiz
import com.google.common.io.Closeables;
close metodu - Closeable + boolean
Örnek ver

closeQuitely metodu
InputStream için kullanılabilir.

3 Aralık 2019 Salı

ImmutableTable Sınıfı

Örnek
Şöyle yaparız.
ImmutableTable<Region, Country, ImmutableList<City>> immutableTable = RECORDS.stream()
        .collect(toImmutableTable(
                r -> r.getRegion(),
                r -> r.getCountry(),
                r -> ImmutableList.of(r.getCity()),
                (l, l2) -> ImmutableList.<City>builder().addAll(l).addAll(l2).build()
        ));