30 Nisan 2015 Perşembe

Hashtable on PL/SQL


Some time ago, It's bubbled up that if there is a way to use hashtables in PL/SQL just like in Java which is very oftenly used. We associate the key-value relationship with arrays but since the keys are numeric we need to use associative arrays in order to produce alpha keys. There is a simple and self explanatory example below:


Java:

Hashtable<String, String> htable = new Hashtable<String, String>();

htable.put("MIDFIELDER", "SNEIJDER");
htable.put("STRIKER", "DROGBA");

System.out.println(htable.get("STRIKER"));


PL/SQL:

DECLARE
  TYPE t_htable IS TABLE OF VARCHAR2(30) INDEX BY VARCHAR2(30);
  htable t_htable;
BEGIN
  htable('MIDFIELDER') := 'SNEIJDER';
  htable('STRIKER') := 'DROGBA';

  dbms_output.put_line(htable('STRIKER'));


END;

2 Aralık 2014 Salı

[Java] multiple || "or" statements


When I used to code, I had to compare a value with multiple other values in order to check if one of the values match with the compared one. It became something like below:

        String equation = "A";
        if (equation.equals("A") || equation.equals("C") || equation.equals("B") || equation.equals("D")) {
            // do something.
        }

Ugly and stinky code. I made something simple and my code became shorter and readable.

    public static <T> boolean isContains(T in, T... conditions) {
        for (T iterationItem : conditions) {
            if ((iterationItem.equals(in)))
                return true;
        }
        return false;
    }


String equation = "A";
if(isContains(equation, "A", "C", "B", "D")){
    // do something.
}

Happy coding.

19 Eylül 2014 Cuma

[JAVA] Abstract factory with constructor parameter

Hello again,

During development a project, I needed a solution and would like to share it with you:

Challenge:
1. I need a abstract factory class.
2. The each classes which I am going to call have a constructor parameter. While I call a class instance from factory class, I needed to set the constructor parameter as well.
3. For this, I prefer to use ENUM as the best practice of factory pattern of Java.

The sample project which satisfy these 3 necessities below. The way of setting parameter during call of newInstance(), we use getDeclaredConstructor().newInstance();

Have fun!

public class Main {

    public static void main(String[] args) {
        MyGenericClass mgc = MyClassFactory.instanceOf(MyClassFactory.MYCLASS, "This is My Class");
        MyGenericClass mgc2 = MyClassFactory.instanceOf(MyClassFactory.MYSECONDCLASS, "This is My Second Class");
    }
}

This is My Class
This is My Second Class


public abstract class MyGenericClass {

    public MyGenericClass(String s) {
        System.out.println(s);
    }
}

public class MyClass extends MyGenericClass {

    public MyClass(String s) {
        super(s);
    }
}


public class MySecondClass extends MyGenericClass{

    public MySecondClass(String s) {
        super(s);
    }
}


public enum MyClassFactory {
    MYCLASS(MyClass.class), MYSECONDCLASS(MySecondClass.class);

    private static final Map<MyClassFactory, Class<MyGenericClass>> lookup = new HashMap<MyClassFactory, Class<MyGenericClass>>();
    static {
        for (MyClassFactory myClass : EnumSet.allOf(MyClassFactory.class))
            lookup.put(myClass, (Class<MyGenericClass>) myClass.getClazz());
    }
    private Class<? extends MyGenericClass> clazz;

    MyClassFactory(Class<? extends MyGenericClass> clazz) {
        this.clazz = clazz;
    }

    private Class<? extends MyGenericClass> getClazz() {
        return clazz;
    }

    public static <T extends MyGenericClass> T instanceOf(MyClassFactory clazz, String message)  {
        if (lookup.containsKey(clazz)) {
            try {
                return (T) lookup.get(clazz).getDeclaredConstructor(String.class).newInstance(message);
            }
            catch (InstantiationException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            catch (IllegalArgumentException e) {
                return null;
            }
            catch (SecurityException e) {
                e.printStackTrace();
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

29 Nisan 2014 Salı

[Enterprise Service Bus] Doğru Tarifi Yapabilmek




Merhabalar,

Son zamanlarda son derece popüler olan bir konudan bahsetmek istiyorum: Enterprise Service Bus.

Her konuşulduğunda baştan tarif edilmesi gereken bir  tanım olduğu için aslında problem ve ihtiyaçları anlama noktasında ESB derken "şunun ve bunun için gerekiyor" şeklinde de açıklama yapmak gerekiyor. Zira ünlü fil hikayesinde olduğu gibi herkes ESB'ye farklı anlamlar yüklüyor.

Bana göre anlaşılması gereken bazı ön tarifler var:

1. ESB'nin başı ve sonunu tarif eden standart bir açıklama mevcut değil, dolayısıyla herkes ve her ürün için farklı özellikler mevcut.
2. Sadece "farklı sistemleri entegre etmemize yardımcı olan bir ürün" şeklinde tariflenebiliyor.
3. Yönlendirme, çevirim, güvenlik vb. gibi farklı özellikler sunan ve birbirinden farklı ürünler mevcut.
4. Bunun için firmanın repütasyonu, destekleyici özellikler, ek plug-in geliştirebilme vb. gibi özelliklere bakmadan önce mutlaka çözülmesi gereken problemin ve ihtiyacın tam listesini ve tarifini yapabilmek gerekiyor.

Enterprise Service Bus ürünlerinde karşılaştırılabilecek bazı temel özellikler şöyle:
- Kullanım kolaylığı
- Bakım ve monitoring
- Destek ihtiyacınıza ve sorularınıza yardımcı olabilecek topluluğun niteliği ve niceliği.
- Kurumsal destek
- İşlevsellik
- Esneklik
- Genişlenebilirlik(Extensibility)
- Standartlar
- Bağlayıcılar (Connector)
- Maliyetler
- Lisans
- Öğrenim süresi ve eğrisi (Learning curve)
- Eğitim imkanları ve maliyeti

Bu yüzden seçim kriterlerinizi framework, ESB veya Suite olarak daraltmanız gerekecek. Zira kullanmayacağınız çapta özellikler elde etmek, kurulum ve entegrasyon sürecinde kurumunuzun hemen tüm entegrasyon sürecine dahil etmek zorunda kalacağınız özellikler eklemek demek olacak. Ve eğer seçiminizi yanlış yapmış iseniz (Herhangi bir kriter için) artık geriye dönüşü çok ta mümkün olamayacak bir yola girmiş olacaksınız.

17 Nisan 2014 Perşembe

[Java] Chain of responsibility pattern (recursive implementation)

Hello,

I found out an interesting and useful implementation of Chain of responsibility pattern and would like to share it.:

On Classical implementation, each concrete handler is responsible of it's own successor object (the ring  which comes from itself) as well. This creates a dependency problem and unnecessarry complexity..




This implementation has a recursive structure and it triggers a chain reaction with calling the first ring of the chain.

public abstract class GroupChain {

       private GroupChain next;

       public void setNext(GroupChain nextG) {
             next = nextG;
       }

       public final Integer start(int value) {
             Integer result = this.execute(value);
             if (next != null) {
                    Integer nextResult = next.start(value);
                    if (nextResult != null)
                           result += (nextResult);
             }

             return result;
       }

       protected abstract Integer execute(int value);

}

public class FirstChain extends GroupChain {

       @Override
       protected Integer execute(int value) {
            
             return value * 2;
       }


}

public class SecondChain extends GroupChain{

       @Override
       protected Integer execute(int value) {
            
             return value * 5;
       }

}

public class ThirdChain extends GroupChain {

       @Override
       protected Integer execute(int value) {

             return value * 2;
       }

}

public class Orchestrator {

       public static void main(String[] args) {
             FirstChain firstChain = new FirstChain();
             SecondChain secondChain = new SecondChain();
             ThirdChain thirdChain = new ThirdChain();

             firstChain.setNext(secondChain);
             secondChain.setNext(thirdChain);

             System.out.println(firstChain.start(3));

       }

}

[ORACLE SQL] Combination Calculation

You replace the string 'ALI' with any type you prefer and use to calculate combination of strings by letters.


WITH got_a_char AS
(SELECT 'ALI' AS txt, substr('ALI', LEVEL, 1) AS a_char, LEVEL AS n
    FROM dual
  CONNECT BY LEVEL <= length('ALI'))
SELECT DISTINCT REPLACE(sys_connect_by_path(a_char, '~~'), '~~') AS perm
  FROM got_a_char
WHERE LEVEL = length(txt)
CONNECT BY nocycle txt = PRIOR txt

       AND n != PRIOR n;

Result:

LIA
ILA
ALI
IAL
LAI
AIL

19 Mart 2013 Salı

[Oracle SQL] Parse XML From CLOB



Oracle SQL ile XML Parsing.

----------------------------------------------------------------------------


WITH ornek_xml AS
(SELECT '<manav><meyve>Elma</meyve></manav>' AS meyve_xml
  FROM   dual)
SELECT xmltype(ox.meyve_xml).extract('//meyve/text()').getstringval() AS extracted_meyve
FROM   ornek_xml ox;

----------------------------------------------------------------------------
Result:
EXTRACTED_MEYVE
Elma