9 Ocak 2020 Perşembe

CollectionUtils Sınıfı

containsAny metodu
Şöyle yaparız.
CollectionUtils.containsAny(someCollection1, someCollection2)
disjunction metodu
symmetricDifference() anlamına gelir. A ve B'nin kesişimi hariç A + B'yi verir.
Örnek
Şöyle yaparız.
CollectionUtils.disjunction(s1, s2)
Örnek
Elimizde şöyle bir kod olsun
List<String> oldKeys = Arrays.asList("key0","key1","key2","key5");
List<String> newKeys = Arrays.asList("key0","key2","key5", "key6");
Şöyle yaparız. Çıktı olarak [key1, key6] alırız.
List<String> list = new ArrayList<>(CollectionUtils.disjunction(newKeys, oldKeys));
emptyIfNull metodu
Verilen parametre null ise empty collection döner. ListUtils sınıfında da benzer bir metod var.

intersection metodu
Şöyle yaparız. A kesişim B'yi verir.
Collection<Integer> collection1 = Arrays.asList(1, 2, 4, 5, 7, 8);
Collection<Integer> collection2 = Arrays.asList(2, 3, 4, 6, 8);

Collection<Integer> intersection = CollectionUtils.intersection(collection1,collection2);
System.out.println(intersection); // [2, 4, 8]
isEqualCollection metodu
Örnek
Şöyle yaparız
List<Integer> list3 = new ArrayList<>();
list1.add(3);
list1.add(4);
List<Integer> list4 = new ArrayList<>();
list2.add(3);
list2.add(4);
boolean equalCollection = CollectionUtils.isEqualCollection(list3, list4);
System.out.println("Is Equal?:" + equalCollection);
isEmpty metodu
Açıklaması şöyle.
public static boolean isEmpty(Collection<?> coll)

Null-safe check if the specified collection is empty.

Null returns true.
Metodun içi şöyle.
return coll == null || coll.isEmpty();
Örnek
Şöyle yaparız.
List<String> list = ...;

if(CollectionUtils.isEmpty(list)) {  // line3
  ...
}
isNotEmpty metodu
Örnek
Şöyle yaparız.
List<String> stringList = new ArrayList<>();
boolean notEmpty = CollectionUtils.isNotEmpty(stringList);
System.out.println("Is Empty?"+ notEmpty);
select metodu

Şöyle yaparız.
Collection<Cat> smallList = CollectionUtils.select(bigList, new Predicate() {
  public boolean evaluate(Object o) {
    return ...;
    }
});
subtract metodu
A - B 'yi verir. Yani A'nın B ile kesişmeyen kısmıdır. Bu metodun ismi belki difference() olsa daha iyi olurdu.

Örnek
Şöyle yaparız.
Collection<Integer> collection1 = Arrays.asList(1, 2, 4, 5, 7, 8);
Collection<Integer> collection2 = Arrays.asList(2, 3, 4, 6, 8);


Collection<Integer> subtract = CollectionUtils.subtract(collection1, collection2);
System.out.println(subtract); // [1, 5, 7]
union metodu
Örnek
Şöyle yaparız. A + B 'yi verir.
Collection<Integer> collection1 = Arrays.asList(1, 2, 4, 5, 7, 8);
Collection<Integer> collection2 = Arrays.asList(2, 3, 4, 6, 8);

Collection<Integer> union = CollectionUtils.union(collection1, collection2);
System.out.println(union); // [1, 2, 3, 4, 5, 6, 7, 8]
Örnek
Şöyle yaparız
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(4);

Collection<Integer> union = CollectionUtils.union(list1, list2);
System.out.println(union);

Collection<Integer> intersection = CollectionUtils.intersection(list1, list2);
System.out.println(intersection);

Collection<Integer> disjunctionList = CollectionUtils.disjunction(list1, list2);
System.out.println(disjunctionList);

Collection<Integer> subtract = CollectionUtils.subtract(list1, list2);
System.out.println(subtract);



2 Ocak 2020 Perşembe

FileBasedConfigurationBuilder Sınıfı

Giriş
FileBasedConfiguration  nesnesi elde etmek içindir. FileBasedConfiguration XML veya properties dosyasını okumak için kullanılır.

constructor - XMLConfiguration
Örnek
Şöyle yaparız.
String USER_CONFIGURATION_FILE_NAME = "config.xml";

Path configFilePath = Paths.get(System.getProperty("user.home"), ".myapp",
  USER_CONFIGURATION_FILE_NAME);

FileBasedConfigurationBuilder<XMLConfiguration> configBuilder=
  new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
      .configure(new Parameters().xml().setFile(configFilePath.toFile()));
constructor - PropertiesConfiguration
Örnek
Şöyle yaparız.
Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
 new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
   .configure(params.properties().setFileName("my.properties"));
getConfiguration metodu
Örnek
Şöyle yaparız.
Configuration config = configBuilder.getConfiguration();
Örnek
Şöyle yaparız.
FileBasedConfiguration config = builder.getConfiguration();
save metodu
Şöyle yaparız.
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
 new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
  .configure(new Parameters().fileBased().setFile(...));

Configuration config = builder.getConfiguration();

config.setProperty("database.port", 8200);

builder.save();

29 Aralık 2019 Pazar

Lang3 StopWatch Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.commons.lang3.time.StopWatch;
Süreyi milisaniye olarak ölçer.

createStarted metodu
Şöyle yaparız. Çıktı olarak 00:00:02:072 alırız.
StopWatch stopwatch = StopWatch.createStarted()
...
System.out.println(stopwatch);
getTime metodu
Örnek
Şöyle yaparız.
public static void main(String[] args) {
  StopWatch stopwatch = new StopWatch();
  stopwatch.start();
  stopwatch.stop();
  long timeTaken = stopwatch.getTime();
  System.out.println(timeTaken);
}
getTime metodu - TimeUnit
Örnek
Şöyle yaparız
public boolean tryLock(){
  return spinTryLock (10,TimeUnit.SECONDS);
}

private boolean spinTryLock(long timeout,TimeUnit unit) {
  boolean locked = false;
  StopWatch stopWatch = StopWatch.createStarted();
  while (stopWatch.getTime(unit) <= timeout){
    if (someCondition()){
      locked = true;
      break;
    }
  }
  return locked;
}

23 Aralık 2019 Pazartesi

HttpEntity Sınıfı

setEntity metodu
Şöyle yaparız
HttpEntity entity = new ByteArrayEntity(array);
httpPost.setEntity(entity);