Wednesday

Install maven on Windows by using "Command Line Prompt"

1.Download maven. http://maven.apache.org/download.cgi (apache-maven-3.3.9-bin.zip)
2. Unzip the file.
3. Open cmd - command line prompt. Set system variable using by below command. (Windows 7 & 8):    
setx M2_HOME "C:\apache-maven-3.3.9"

4. You can see M2_HOME path :
echo %M2_HOME%

5. Update PATH variable. Run the below command in command line prompt:
setx PATH "%PATH%;%M2_HOME%\bin"
echo %PATH%

6.Run the below command:
mvn - version








7. Eclipse - maven integration:
         Help - > install new software:   http://download.eclipse.org/technology/m2e/releases/

Monday

java.lang.IllegalArgumentException: No enum const class java.util.concurrent.TimeUnit.MİLLİSECONDS

BUG:
I got the below error when i started JBOSS server.
java.lang.IllegalArgumentException: No enum const class java.util.concurrent.TimeUnit.MİLLİSECONDS

FIX:
1. Double click to the server.
2. Click "Open launch configuration"
3. Add to the Vm arguments '-Duser.language=en'
4. Then save and start your server.

ResourceBundle example - read a property file based on the locale inside the class folder

package read.properties.file;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourcesBundleExample {

       public static void main(String[] args) {

              ResourceBundle bundle = ResourceBundle.getBundle("config");

              // read config_tr_TR.properties
              Locale.setDefault(new Locale("tr", "TR"));
              bundle = ResourceBundle.getBundle("config");
              System.out.println("Current Locale: " + Locale.getDefault());
              System.out.println("my name is: " + bundle.getString("who_are_you"));
             
              // read config_en_US.properties
              Locale.setDefault(new Locale("en", "US"));
              bundle = ResourceBundle.getBundle("config");
              System.out.println("Current Locale: " + Locale.getDefault());
              System.out.println("my name is: " + bundle.getString("who_are_you"));

       }

}

config_tr_TR.properties
who_are_you = eda_TR


config_en_US.properties
who_are_you = eda_EN
outputs:
Current Locale: tr_TR
my name is: eda_TR
Current Locale: en_US
my name is: eda_EN




Read a .properties file inside the class folder

package read.properties.file;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class App {

       public static void main(String[] args) {
              App app = new App();
              app.readProperties();
       }

       //read a .properties file inside  the class folder
       private void readProperties() {

              Properties prop = new Properties();
              InputStream inputStream = null;
              String filename = "config.properties";

              try {
                     inputStream = getClass().getClassLoader().getResourceAsStream(filename);
                     if (inputStream == null) {
                           System.out.println("Unable to find " + filename);
                           return;
                     }

                     prop.load(inputStream);

                     // get all values
                     Enumeration<?> e = prop.propertyNames();
                     while (e.hasMoreElements()) {
                           String key = (String) e.nextElement();
                           String value = prop.getProperty(key);
                           System.out.println("Key : " + key + ", Value : " + value);
                     }

                     // get only one value you wanted
                     System.out.println("get only one value you wanted: user: " + prop.getProperty("user"));

              } catch (Exception e) {
                     // throw or log the exception

              } finally {
                     if (inputStream != null) {
                           try {
                                  inputStream.close();
                           } catch (IOException e) {
                                  // throw or log the exception
                           }
                     }
              }

       }

}

Config.properties

user=eda
password=less talk more code
outputs:
Key : user, Value : eda
Key : password, Value : less talk more code
get only one value you wanted: user: eda


Friday

java - Enum as instance variables

public class Test {

    public enum MESSAGE {
        SUCCESS("1"),
        ERROR("2");

        private final String msg_id;

        private MESSAGE( final String msg_id ) {
            this.msg_id = msg_id;
        }

        public String getMsgId() {
             return msg_id;
        }
    }

    public static void main(String[] args) {
        System.out.println("MESSAGE.ERROR --> " + MESSAGE.ERROR.getMsgId());
    }

}

Java singleton design pattern example

//Singleton design pattern - example class
//Eager initialization
public class SingletonObject {
    
       //Our static variable holding an instance of an existing singleton class
       private static SingletonObject singletonObject;
     
       static {
         // Initialize "singletonObject" only once
         singletonObject = new SingletonObject();
       }

    
       //The class constructor method is also private. We can not create the class instance outside of this class
       private SingletonObject() {
              //...constructor codes
       }
    
       //The method that returns the single instance of the SingletonObject class
       public static SingletonObject getInstance() {
              return singletonObject;
       }

       //Prevent cloning of class by throwing CloneNotSupportedException
       @Override
       public Object clone() throws CloneNotSupportedException {
              throw new CloneNotSupportedException();
       }
}

//Singleton design pattern - example class
//Lazy initialization
public class SingletonObject {
    
       //Our static variable holding an instance of an existing singleton class
       private static SingletonObject singletonObject = null;
     
       //The class constructor method is also private. We can not create the class instance outside of this class
       private SingletonObject() {
              //...constructor codes
       }
    
       //The method that returns the single instance of the SingletonObject class
       public static SingletonObject getInstance() {
          if(singletonObject == null) {
                 synchronized (SingletonObject.class) {
                        //double-checked locking
                        if(singletonObject == null) {
                              singletonObject = new SingletonObject();
                        }
                 }
                 
          }
              return singletonObject;
       }

       //Prevent cloning of class by throwing CloneNotSupportedException
       @Override
       public Object clone() throws CloneNotSupportedException {
              throw new CloneNotSupportedException();
       }
}



Java builder design pattern example

import java.util.List;

public class Student {

    private String name;
    private int age;               //optional
    private List<String> language; //optinal

    public static class Builder {

        private String name;
        private int age;
        private List<String> language;

        public Builder (String name) {
            this.name = name;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public Builder language(List<String> language) {
            this.language = language;
            return this;
        }

        public Student build() {
            return new Student(this);
        }

    }

    private Student(Builder builder) {
        name = builder.name;
        age = builder.age;
        language = builder.language;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + ", language=" + language +'}';
    }
}

import java.util.Arrays;

public class TestStudent {

    public static void main(String[] args) {

       Student s1 = new Student.Builder("eda").build();
      
        Student s2 = new Student.Builder("eda").age(28).language(Arrays.asList("japanese","english")).build();

        System.out.println("s1--> " + s1);
        System.out.println("s2--> " + s2);
    }

}
Output:
s1--> Student{name='eda', age=0, language=null}
s2--> Student{name='eda', age=28, language=[japanese, english]}


Quoting from Joshua Bloch's Second Edition of Effective Java:
"In summary, the Builder pattern is a good choice when designing classes
whose constructors or static factories would have more than a handful of
parameters, especially if most of those parameters are optional."