15 Nisan 2019 Pazartesi

ConcurrentLinkedHashMap Sınıfı

constructor
En fazla 1000 tane eleman saklamak için şöyle yaparız.
ConcurrentMap<K, V> cache = new ConcurrentLinkedHashMap.Builder<K, V>()
    .maximumWeightedCapacity(1000)
    .build();

9 Nisan 2019 Salı

Files Sınıfı

Giriş
Şu satırı dahil ederiz.
import com.google.comon.io.Files;
asByteSource metodu - File
Şöyle yaparız
try {
  ByteSource byteSource = Files.asByteSource(new File(filePath));
  ...  
} catch (IOException e) {
  ...
}
createParentDirs metodu
Şöyle yaparız.
Files.createParentDirs(file);
createTempDir metodu
Şöyle yaparız.
File myTempDir = Files.createTempDir();
getFileExtension metodu
Şöyle yaparız.
String path = "c:/path/to/file/foo.txt";
String ext = Files.getFileExtension(path);
System.out.println(ext); //prints txt
readLines metodu
Şöyle yaparız.
List<String> strings = Lists.newArrayList();
File file ...;
try {
  strings= Files.readLines(file, Charsets.UTF_8);
}
catch (IOException ioe) {
  ...
  // File does not exist
}
readLines metodu - LineProcessor
Verilen dosyadaki tüm satırları okur ve bir LineProcessor aracılığıyla okunan satırları işleyebilir.

toString metodu
Bu metod ile verilen dosyanın tamamı string olarak okunuyor.

touch metodu
Şöyle yaparız.
Files.touch(file);

4 Nisan 2019 Perşembe

Longs Sınıfı

toByteArray metodu
Big Endian olarak çıktı verir. En soldaki byte (MSB) çıktının ilk byte'ı olur.

Elimizdeki değer şöyle olsun
7|6|5|4|3|2|1|0. 
Çıktı olarak şunu alırız
0|1|2|3|4|5|6|7

Örnek
Şöyle yaparız.
byte[] bytes = Longs.toByteArray(12345L);

1 Nisan 2019 Pazartesi

Hashing Sınıfı

Giriş
Şu satırı dahil ederiz
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
Her metod bir HashFunction nesnesi döndürür.

hmacSha1 metodu
HashFunction nesnesi döndürür.

Örnek
Şöyle yaparız
String valueToDigest = "TestUser";
byte[] key = secret.getBytes();

Hasher hasher = Hashing.hmacSha1(key).newHasher();
hasher.putBytes(valueToDigest.getBytes());


String hashAsString = hasher.hash().toString();
//veya
byte[] hashAsByte = hasher2.hash().asBytes();
murmur3_128 metodu
HashFunction nesnesi döndürür.
Örnek
Şöyle yaparız.
HashFunction hashFunction = Hashing.murmur3_128();
Hasher hasher = hashFunction.newHasher();
for (int i: myArray) {
    hasher.putInt(i);
}
int hashCode = inthasher.hash().asInt();
sha256 metodu
HashFunction nesnesi döndürür.

Örnek
Şöyle yaparız
HashFunction hashFunction = Hashing.sha256();
Örnek
Şöyle yaparız.
password = Hashing
        .sha256()
        .hashString(input, StandardCharsets.UTF_8)
        .toString();
Örnek
Şöyle yaparız.
String data = "hello world";

// Generate Sha 256 hash by using guava library
final String hashed = Hashing.sha256()
  .hashString(data, StandardCharsets.UTF_8)
  .toString();
Çıktı olarak şunu alırız
Guava  : Sha256hash: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9