Tuesday

Sort multiple variables with Java 8 stream

I'll try to sort my data as following rules using java 8 stream.
Student.Address.CountryName = B or C is previous  >>
    then Student.StudentDetail.StudentScore descending (nulls  appear at the end) >>
       then Student.StudentDetail.RegistrationTime ascending

Example data:

The following code snippet makes sorting: 
 studentList.stream().sorted(
               Comparator.comparing((Student s)-> !Arrays.stream(priorityCountryName).anyMatch(s.address.getCountryName()::equals))
               .thenComparing(Comparator.comparing(s-> s.studentDetail.getStudentScore(), Comparator.nullsLast(Comparator.reverseOrder())))
               .thenComparing((s1) -> s1.studentDetail.getRegistrationTime())
)

Full example:
import java.io.Serializable;

public class Student implements Serializable {
    
    public final Address address;
    public final StudentDetail studentDetail;
    
    public Student(Address address, StudentDetail studentDetail) {
        this.address = address;
        this.studentDetail = studentDetail;
    }
}
import java.io.Serializable;

public class Address implements Serializable {
    
    private String countryName;
    
    public Address() {
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

public class StudentDetail implements Serializable {

    private int studentNumber;
    private BigDecimal studentScore;
    private Timestamp registrationTime;
    
    public StudentDetail(){
    }

    public BigDecimal getStudentScore() {
        return studentScore;
    }

    public void setStudentScore(BigDecimal studentScore) {
        this.studentScore = studentScore;
    }

    public Timestamp getRegistrationTime() {
        return registrationTime;
    }

    public void setRegistrationTime(Timestamp registrationTime) {
        this.registrationTime = registrationTime;
    }

    public int getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(int studentNumber) {
        this.studentNumber = studentNumber;
    }
    
}
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
       String[] priorityCountryName = {"B", "C"};
 
       List<Student> studentList = addStudents();
       logStudentList("********studentList mix order**********", studentList);

       List<Student> orderedStudentList = studentList.stream().sorted(
               Comparator.comparing((Student s)-> !Arrays.stream(priorityCountryName).anyMatch(s.address.getCountryName()::equals))
               .thenComparing(Comparator.comparing(s-> s.studentDetail.getStudentScore(), Comparator.nullsLast(Comparator.reverseOrder())))
               .thenComparing((s1) -> s1.studentDetail.getRegistrationTime())
           ).collect(Collectors.toList());;
 
       logStudentList("********studentList ordered**********", orderedStudentList);
    }
    
    
    private static List<Student> addStudents() {
       List<Student> studentList = new ArrayList<Student>();
 
       Student student8 = new Student(new Address(), new StudentDetail());
       student8.address.setCountryName("D");
       student8.studentDetail.setStudentScore(new BigDecimal(5.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student8.studentDetail.setRegistrationTime(Timestamp.valueOf("2024-12-31 09:30:00"));
       student8.studentDetail.setStudentNumber(8);
       studentList.add(student8);
 
       Student student3 = new Student(new Address(), new StudentDetail());
       student3.address.setCountryName("B");
       student3.studentDetail.setStudentScore(new BigDecimal(0.1).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student3.studentDetail.setRegistrationTime(Timestamp.valueOf("2019-12-31 09:30:00"));
       student3.studentDetail.setStudentNumber(3);
       studentList.add(student3);
 
       Student student5 = new Student(new Address(), new StudentDetail());
       student5.address.setCountryName("B");
       student5.studentDetail.setStudentScore(null);
       student5.studentDetail.setRegistrationTime(Timestamp.valueOf("2018-12-31 09:30:00"));
       student5.studentDetail.setStudentNumber(5);
       studentList.add(student5);
 
       Student student2 = new Student(new Address(), new StudentDetail());
       student2.address.setCountryName("C");
       student2.studentDetail.setStudentScore(new BigDecimal(2.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student2.studentDetail.setRegistrationTime(Timestamp.valueOf("2021-12-31 09:30:00"));
       student2.studentDetail.setStudentNumber(2);
       studentList.add(student2);
 
       Student student7 = new Student(new Address(), new StudentDetail());
       student7.address.setCountryName("D");
       student7.studentDetail.setStudentScore(new BigDecimal(5.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student7.studentDetail.setRegistrationTime(Timestamp.valueOf("2023-12-31 09:30:00"));
       student7.studentDetail.setStudentNumber(7);
       studentList.add(student7);
 
       Student student4 = new Student(new Address(), new StudentDetail());
       student4.address.setCountryName("B");
       student4.studentDetail.setStudentScore(new BigDecimal(0.1).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student4.studentDetail.setRegistrationTime(Timestamp.valueOf("2020-12-31 09:30:00"));
       student4.studentDetail.setStudentNumber(4);
       studentList.add(student4);
 
       Student student9 = new Student(new Address(), new StudentDetail());
       student9.address.setCountryName("D");
       student9.studentDetail.setStudentScore(new BigDecimal(4.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student9.studentDetail.setRegistrationTime(Timestamp.valueOf("2025-12-31 09:30:00"));
       student9.studentDetail.setStudentNumber(9);
       studentList.add(student9);
 
       Student student1 = new Student(new Address(), new StudentDetail());
       student1.address.setCountryName("B");
       student1.studentDetail.setStudentScore(new BigDecimal(6.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student1.studentDetail.setRegistrationTime(Timestamp.valueOf("2022-12-31 09:30:00"));
       student1.studentDetail.setStudentNumber(1);
       studentList.add(student1);
 
       Student student10 = new Student(new Address(), new StudentDetail());
       student10.address.setCountryName("D");
       student10.studentDetail.setStudentScore(null);
       student10.studentDetail.setRegistrationTime(Timestamp.valueOf("2018-11-12 09:30:00"));
       student10.studentDetail.setStudentNumber(10);
       studentList.add(student10);
 
       Student student6 = new Student(new Address(), new StudentDetail());
       student6.address.setCountryName("E");
       student6.studentDetail.setStudentScore(new BigDecimal(5.0).setScale(2, BigDecimal.ROUND_HALF_EVEN));
       student6.studentDetail.setRegistrationTime(Timestamp.valueOf("2023-12-01 09:30:00"));
       student6.studentDetail.setStudentNumber(6);
       studentList.add(student6);
 
       return studentList;
    }
    
    private static void logStudentList(String firstMessage, List<Student> studentList) {
       System.out.println(firstMessage);
 
       for(Student student : studentList) {
          StringBuilder studentBuilder = new StringBuilder();
        
          studentBuilder.append("countryname: ");
          studentBuilder.append(student.address.getCountryName());
          studentBuilder.append(" studentNumber: ");
          studentBuilder.append(student.studentDetail.getStudentNumber());
          studentBuilder.append(" studentScore: ");
          studentBuilder.append(student.studentDetail.getStudentScore());
          studentBuilder.append(" registrationTime: ");
          studentBuilder.append(student.studentDetail.getRegistrationTime());
        
          System.out.println(studentBuilder.toString());
       }
    }

}

//CONSOLE OUTPUT:
********studentList mix order**********
countryname: D studentNumber: 8 studentScore: 5.00 registrationTime: 2024-12-31 09:30:00.0
countryname: B studentNumber: 3 studentScore: 0.10 registrationTime: 2019-12-31 09:30:00.0
countryname: B studentNumber: 5 studentScore: null registrationTime: 2018-12-31 09:30:00.0
countryname: C studentNumber: 2 studentScore: 2.00 registrationTime: 2021-12-31 09:30:00.0
countryname: D studentNumber: 7 studentScore: 5.00 registrationTime: 2023-12-31 09:30:00.0
countryname: B studentNumber: 4 studentScore: 0.10 registrationTime: 2020-12-31 09:30:00.0
countryname: D studentNumber: 9 studentScore: 4.00 registrationTime: 2025-12-31 09:30:00.0
countryname: B studentNumber: 1 studentScore: 6.00 registrationTime: 2022-12-31 09:30:00.0
countryname: D studentNumber: 10 studentScore: null registrationTime: 2018-11-12 09:30:00.0
countryname: E studentNumber: 6 studentScore: 5.00 registrationTime: 2023-12-01 09:30:00.0
********studentList ordered**********
countryname: B studentNumber: 1 studentScore: 6.00 registrationTime: 2022-12-31 09:30:00.0
countryname: C studentNumber: 2 studentScore: 2.00 registrationTime: 2021-12-31 09:30:00.0
countryname: B studentNumber: 3 studentScore: 0.10 registrationTime: 2019-12-31 09:30:00.0
countryname: B studentNumber: 4 studentScore: 0.10 registrationTime: 2020-12-31 09:30:00.0
countryname: B studentNumber: 5 studentScore: null registrationTime: 2018-12-31 09:30:00.0
countryname: E studentNumber: 6 studentScore: 5.00 registrationTime: 2023-12-01 09:30:00.0
countryname: D studentNumber: 7 studentScore: 5.00 registrationTime: 2023-12-31 09:30:00.0
countryname: D studentNumber: 8 studentScore: 5.00 registrationTime: 2024-12-31 09:30:00.0
countryname: D studentNumber: 9 studentScore: 4.00 registrationTime: 2025-12-31 09:30:00.0
countryname: D studentNumber: 10 studentScore: null registrationTime: 2018-11-12 09:30:00.0

Friday

Gradle - Installation

1. Download gradle zip file from  https://gradle.org/releases/








2. Extract files from zip to a location.
3. Go to enviroment variables >> system variables >> new































4. Go to enviroment variables >> system variables >> Edit 'path' >> add '%GRADLE_HOME%/bin'




















5. cmd >> gradle -v

Thursday

How to install and configure Git(2.19.0) and GitHub on Windows

Install git on your desktop:
1. Go  https://git-scm.com/  >> click Downloads >> click for windows.
2. Run the downloaded file.
3. Click next, select below components, then click next.
























































































































































































Create repository on GitHub:
1. Create an account for GitHub. https://github.com/
2. Sign in to GitHub with your user.
3. Click to new repository:































































You can select language from >> 'add .gitignore' section


























































































Then click 'Create repository.'








































































You can find the link for your repository as follow:



















































Configure  git and github:
1. Create a folder named as 'git'. C:\git
2. Then open git bash.
3. Lets pass to out folder directory.
 'cd /C'







































































































'cd git/'



































git config --global user.name "Your github name"
































git config --global user.email "your email adress"
































git clone (your repository adress - i show you up how to copy it)































TestGit folder has been copied to our folder:

























How to upload files to GitHub:




1.Create  a file:































2. Write something to file then save.



























































3.
git add (file names)
























































4.
git commit - m "(committed message)" filename


































5.
git push -u origin master
>> at first time it will ask github user-password.












































6. Go to github then refresh page.
































































Change file and upload again:
1. Change text in the file and save it.














2.
git status










































3.
git commit - m "(committed message)" filename






























4.
git push -u origin master












































5. Go to your github page.

Wednesday

Postgres >> manually alter sequence next value

SELECT setval('my_seq', 100, FALSE);
--my_seq >> your sequence name
-- 100 >> value you want to set
-- false >> means the next value would be 100
--       >> if it would be true then next value will be 100+1 = 101

Monday

How to access JBoss over an another host

For accessing JBOSS or Wildfly from another host outside of localhost:

<!-- in standalone.xml  there is a definition as follows-->
<interface name="public">
    <inet-address value="${jboss.bind.address:127.0.0.1}"/>
</interface>

<!--you can do any of the following -->

<!-- 1. choice: 
     binding for all the IP Addresses 
-->
<interface name="public">
     <inet-address value="${jboss.bind.address:0.0.0.0}"/>
</interface>

<!-- 2. choice: 
     binding for only one IP adress 
-->
<interface name="public">
     <inet-address value="${jboss.bind.address:192.168.x.x}"/>
</interface>

<!-- 3. choice:
     double click on server 
     then click 'Open launch configuration'
     then add -b 0.0.0.0 to the  'Arguments' 'Program arguments' 
-->

Friday

How to check for the existence of undeclared variable in js

You can check for the existence of undeclared variable in javascript without getting ReferenceError:
if (x !== undefined){} // gets error if x undeclared
if (x !== 'undefined'){} // gets error if x undeclared
if (typeof x !== undefined){} // gets error because typeof x returns "undefined"

if (typeof x !== 'undefined'){} //right one

Tuesday

How to learn which vendor / version is using in jsf webapp? (MyFaces or Mojarra)

You can run the following code fragment in your application:
import javax.faces.context.FacesContext;

System.out.println("imp title: " + FacesContext.class.getPackage().getImplementationTitle());
System.out.println("impl vendor " + FacesContext.class.getPackage().getImplementationVendor());
System.out.println("imp version: " + FacesContext.class.getPackage().getImplementationVersion());
Sample output:

imp title: Apache MyFaces JSF-2.2 Core API
impl vendor The Apache Software Foundation
imp version: 2.2.11

Dynamically calling a class - method in java by using reflection

Dynamically call a method on a dynamically given class by using reflection:
import java.lang.reflect.Method;

try {
       String myClassName = "tr.com.xx.xx.xx.Example";
       String myMethodName = "exampleMethod";

       Class<?> myClassRef = Class.forName(myClassName);
       Object instanceOfMyClass = myClassRef.newInstance();
       
       //my object input types in order: param1(int), param2(String), param3(MyDTO), param4(List<MyDTO>)
       Method method = myClassRef.getDeclaredMethod(myMethodName, int.class, String.class, MyDTO.class, List.class);
       MyResult respoonse = (MyResult) method.invoke(instanceOfMyClass, param1, param2, param3, param4);
       System.out.println("response" + response.getX());

} catch(Exception e) {
       //logging
       System.out.println(e.getMessage());