Monday, February 28, 2022

Comparable and Comparator in Java

Java provider Comparable and Comparator interface for sorting array/collection of objects. By using these interface we can sort primitive and wrapper classes of array or list. 

Here we will see the example of default sort order for primitive types by using java.util.Arrays.sort(). This will sort the primitive by natural order. 

import java.util.Arrays;

public class ComparePrimitive {
public static void main(String[] args) {
int[] intArray = {6, 4, 3, 1, 2, 5};
String[] strArray = {"Java", "Css", "Angular", "Python", "Xml"};
Arrays.sort(intArray);
Arrays.sort(strArray);
System.out.println("Sorted Array of int: " + Arrays.toString(intArray));
System.out.println("Sorted Array of String: " + Arrays.toString(strArray));
}
}

Output: 
Sorted Array of int: [1, 2, 3, 4, 5, 6]
Sorted Array of String: [Angular, Css, Java, Python, Xml].
Let try the Arrays.sort of Object sorting. For example I will create a Employee class with basic attributes and will try to sort the object by using Arrays.sort method??
Here my Employee class, 
public class Employee {
private int id;
private String name;
private double experience;
private long salary;
private int rating;

public Employee(int id, String name, double experience, long salary, int rating) {
this.id = id;
this.name = name;
this.experience = experience;
this.salary = salary;
this.rating = rating;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getExperience() {
return experience;
}

public void setExperience(double experience) {
this.experience = experience;
}

public double getSalary() {
return salary;
}

public void setSalary(long salary) {
this.salary = salary;
}

public int getRating() {
return rating;
}

public void setRating(int rating) {
this.rating = rating;
}
}
Here test class to sort the  employee object using Arrays.sort().
public class EmployeeSort {
public static void main(String[] args) {
Employee[] employees = {
new Employee(5, "Antwaun", 5.5, 10000L, 1),
new Employee(2, "Rick", 32.5, 450000L, 3),
new Employee(1, "Richard", 50, 600000L, 4),
new Employee(3, "Corey", 12.5, 20000L, 2),
new Employee(4, "Chumlee", 10.5, 15000L, 5)
};
Arrays.sort(employees);
System.out.println("Sorting order of Employee by Id: "+Arrays.toString(employees));
}
}
 Lets run this main method, OOPS we are getting error, Runtime exception!!!

Exception in thread "main" java.lang.ClassCastException: com.example.monitoring.compare.Employee     cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
    at java.util.Arrays.sort(Arrays.java:1246)
    at com.example.monitoring.compare.EmployeeSort.main(EmployeeSort.java:14)


Process finished with exit code 1

Compiler says cannot case Employee to Comparable. So we need a Comparable type to sorting object.

Oh then how to cast our Employee class to Comparable java??. Yes here we go.. 

Java provide two interfaces to solve this issue.

  • Comparable 
  • Comparator
By implementing Comparable interface, we can override compareTo(Object o) to sort our custom Employee object. Lets modify our Employee class using Comparable interface.

Here Changes in Employee class, 

public class Employee implements Comparable<Employee>{
private int id;
private String name;
private double experience;
private long salary;
private int rating;

public Employee(int id, String name, double experience, long salary, int rating) {
this.id = id;
this.name = name;
this.experience = experience;
this.salary = salary;
this.rating = rating;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getExperience() {
return experience;
}

public void setExperience(double experience) {
this.experience = experience;
}

public double getSalary() {
return salary;
}

public void setSalary(long salary) {
this.salary = salary;
}

public int getRating() {
return rating;
}

public void setRating(int rating) {
this.rating = rating;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", experience=" + experience +
", salary=" + salary +
", rating=" + rating +
'}';
}

@Override
public int compareTo(Employee o) {
return (this.id-o.id);
}
}

Lets run the EmployeeSort,  

Output

Sorting order of Employee by Id: [Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=7, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=7, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=7, name='Chumlee', experience=10.5, salary=15000, rating=5}]

So now we sorted employee by their id so far so good. What if the scenarios come like of we to sort by employee experience, employee rating , employee salary, employee name or combination of these attributes. Do we need to create N number of classes to solve this??

No need, Java provider Comparable interface, by implementing this interface we can create combination of sorting logic for employee object depends on the scenarios. 

Comparator Interface provider compare(Object o1, Object o2) method, by overriding this method we can sort the objects.

I made some changes in Employee class, changes are below,

public static Comparator<Employee> nameComparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return(o1.getName().compareTo(o2.getName()));
}
};

public static Comparator<Employee> salaryComparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getSalary() - o2.getSalary());
}
};

public static Comparator<Employee> ratingComparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o1.getRating() - o2.getRating();
}
};

public static Comparator<? super Employee> experienceComparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getExperience() - o2.getExperience());
}
};
We need use these comparator in EmployeeSort class, Changes in EmployeeSort class. 
Arrays.sort(employees, Employee.nameComparator);
System.out.println("Sorting order of Employee by Name: "+Arrays.toString(employees));

Arrays.sort(employees, Employee.salaryComparator);
System.out.println("Sorting order of Employee by Salary: "+Arrays.toString(employees));

Arrays.sort(employees, Employee.ratingComparator);
System.out.println("Sorting order of Employee by Rating: "+Arrays.toString(employees));

Arrays.sort(employees, Employee.experienceComparator);
System.out.println("Sorting order of Employee by Experience: "+Arrays.toString(employees));
Output:
Sorting order of Employee by Id: [Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}]
Sorting order of Employee by Name: [Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}]
Sorting order of Employee by Salary: [Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}]
Sorting order of Employee by Rating: [Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}]
Sorting order of Employee by Experience: [Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}]

Here we all used Array of  object lets try the same with list. 

Here I added code in EmployeeSort class. And above output we can see all the order are ascending. For name displaying ascending order is good, but rating, experience  and salary we need descending order.

Comparator provider reversed() to reverse the sorting order by using that we can list them reverse. I have used it for rating, experience  and salary sorting.

Here added list of employee object in list. employee1-5 added in above snippet,

List<Employee> employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
employeeList.add(employee4);
employeeList.add(employee5);


Let we check output one by one
employeeList.sort(Employee.nameComparator);
System.out.println("Sorting order List of Employee by Name: "+employeeList);
Output
Sorting order List of Employee by Name: [Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}]
Let order by salary, here I used reversed() method to get reverse order of salary.
employeeList.sort(Employee.salaryComparator.reversed());
System.out.println("Sorting order List of Employee by Salary: "+employeeList);
Output
Sorting order List of Employee by Salary: [Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}]

Rating order,
employeeList.sort(Employee.ratingComparator.reversed());
System.out.println("Sorting order List of Employee by Rating: "+employeeList);

Output
Sorting order List of Employee by Rating: [Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}]
Experience order,
employeeList.sort(Employee.experienceComparator.reversed());
System.out.println("Sorting order List of Employee by Experience: "+ employeeList);
Output:
Sorting order List of Employee by Experience: [Employee{id=1, name='Richard', experience=50.0, salary=600000, rating=4}, Employee{id=2, name='Rick', experience=32.5, salary=450000, rating=3}, Employee{id=3, name='Corey', experience=12.5, salary=20000, rating=2}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}]

Let see how to work with combination of multiple attribute comparison.  

public static Comparator<Employee> experienceAndSalaryComparator = new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
int flag = (int) (o1.getExperience() -o2.getExperience());
if(flag == 0){
flag = (int) (o1.getSalary() - o2.getSalary());
}
return flag;
}
};
To check this scenario I made some changers in employee input, 

Employee employee1 = new Employee(5, "Antwaun", 5.5, 10000L, 1);
Employee employee2 = new Employee(2, "Rick", 32.5, 2000L, 3);
Employee employee3 = new Employee(1, "Richard", 50, 3000L, 4);
Employee employee4 = new Employee(3, "Corey", 32.5, 20000L, 2);
Employee employee5 = new Employee(4, "Chumlee", 10.5, 15000L, 5);
Lets call new comparator in EmployeeSort class,
employeeList.sort(Employee.experienceAndSalaryComparator.reversed());
System.out.println(employeeList);
Output:
[Employee{id=1, name='Richard', experience=50.0, salary=3000, rating=4}, Employee{id=3, name='Corey', experience=32.5, salary=20000, rating=2}, Employee{id=2, name='Rick', experience=32.5, salary=2000, rating=3}, Employee{id=4, name='Chumlee', experience=10.5, salary=15000, rating=5}, Employee{id=5, name='Antwaun', experience=5.5, salary=10000, rating=1}]

Yes almost we covered all the possible implementation on Comparable and comparator interface in java to sort the Object and primitive types.

Some common points above sorting,

  • Both compare() and compareTo() method returns are below
    • first argument is less than second argument return -1
    • first argument is equals to second argument return 0
    • first argument is greater than second argument return 1.
      • compare o1 first argument o2 second argument
      • compareTo this first argument o second argument
  • Comparable provide single way soring. Comparator provide multiple way of sorting

Thanks all.

Thursday, February 24, 2022

How to Check if an Array Contain Duplicate In Java?

Hello All, 

Here the coding snippet to check duplicate in array. I used char[] to check the duplicate character check in a string. 


private static void findDuplicate(String input){
char[] strChar = input.toCharArray();
boolean haveDuplicate = false;
for(int i = 0 ; i < input.length(); i++){
for(int j = i+1 ; j < input.length(); j++){
if(strChar[i] == strChar[j]){
haveDuplicate = true;
System.out.print(strChar[i]);
}
}
}
if(!haveDuplicate){
System.out.println("No duplicate found in array");
}
}
Let pass input as "Java".
Output : a

Pass input as "Python"
Output : No duplicate found in array

Tuesday, February 22, 2022

SecurityContext and SecurityContextHolder in Spring Security.

Main purpose of SecurityContext to hold the currently authenticated user information as
principal and make sure this principal will be available all the method in same thread.
To get authenticated user details SecurityContextHolder will be used.


from principle? Here my spring boot project structure. From this principal we can get the currently logged in user. How can we get the user detail.



I have added spring-boot-starter-security jar in my pom.xml. This to set a default login screen for my application. 

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
If you add this dependency, spring boot will generate the password for your application on
server startup.

You can see the password in console log: Here generated security is below and default username will be 'user',
Using generated security password: e3660fc9-941c-4012-ba6d-8d1870a177d6. If you restart the server then new password will be generate.

Let we check the principal for default user and password for security context.



Here we are able to get the username of currently logged in user using securitycontext and securitycontextholder. 

We cannot every time copy the password from console. Spring provide the properties to set username and password. Here I used them for my application security. Let we check principal after change this property change.

spring.security.user.name=username
spring.security.user.password=User@123

Let login the application using new username and password, check the securityContext 
userdetails,




Let check with other method call and find the username for securitycontext.
let me call my getUser() to fetch particular user detail from db,

let me call : http://localhost:8084/getUser?userId=101

Here we can see the username from securityContext. Usually we need to have
securityContextHolder to get securityContext, from securityContext we can get authentication
from there we can get principal.

Here code snippet:

Object principalObject = SecurityContextHolder.getContext().
                                    getAuthentication().getPrincipal();

if(principalObject instanceof org.springframework.security.core.userdetails.User)
{
String userName = ((org.springframework.security.core.userdetails.User)principalObject)
                                                                               .getUsername();
System.out.println("logged in user: " + userName);
}

Main purpose of SecurityContext to hold the currently authenticated user information as
principal and make sure this principal will be available all the method in same thread.
To get authenticated user details SecurityContextHolder will be used.


From this principal we can get the currently logged in user. How can we get the user detail
from principle? Here my spring boot project structure.

Spring @Primary annotation

 @Primary annotation used to specify the preference when multiple beans of same type are qualified. In some cases, in your application may have multiple beans have same type and spring is responsible to autowire a bean. By providing @Primary we can specify with bean by default can be used.

Already we have a annotation to solve these kind of issue right?.. The @Qualifier annotation  we have to specify the bean right! . But its little different from this one. Already we discussed about Qualifier


Qualifier used, spring to autowire specific bean over same type. Primary used to preference to the specific bean among multiple bean of same type.


Here example, I have added a controller class on my existing project and override toString(), I will start with changes done for @Primary, if you not read @Qualifier please spend some time to read it

In ChefService, added @Bean and @Qualifier for


public interface Chef {
String doCook();
}

@Component
@Qualifier("Veg")
public class VegChef implements Chef {
@Override
public String doCook() {
return "Vegetarian Food";
}

@Override
public String toString() {
return "VegChef";
}
}

@Component
@Qualifier("Non-Veg")
public class NonVegChef implements Chef {
@Override
public String doCook() {
return "Non-Vegetarian Food";
}

@Override
public String toString() {
return "NonVegChef";
}
}
Here we are mentioning Veg is Primary bean, so Spring autowire VegChef as preference.
@Service
public class ChefService {

@Bean
@Primary
@Qualifier("Veg")
public Chef vegChef(){
return new VegChef();
}

@Bean
@Qualifier("Non-Veg")
public Chef nonVegChef(){
return new NonVegChef();
}

}
For Example, first I mentioned here chef reference with Non-Veg Qualifier 
which mapped with NonVegChef Bean.
@Controller
public class ChefController {


@Autowired
@Qualifier("Non-Veg")
Chef chef;

@GetMapping(value = "/cook")
@ResponseBody
public String cook(){
System.out.println( "Cook : " + chef);
return "Cook : " + chef;
}
}

Set See Output and server startup log to understand how the bean is autowired in spring.
16:15:13.249 [main] DEBUG org.springframework.beans.factory.annotation
.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name
'chefController' to bean named 'nonVegChef'

Output : Cook : NonVegChef.

Now we confirm that how @Qualifier annotation works, let check how @Primary annotation works
in spring,

Let see the changes in ChefController
@Controller
public class ChefController {


@Autowired
Chef chef;

@GetMapping(value = "/cook")
@ResponseBody
public String cook(){
System.out.println( "Cook : " + chef);
return "Cook : " + chef;
}
}
We have removed @Qualifier from chef reference, and see the output and startup log, 
16:18:49.230 [main] DEBUG org.springframework.beans.factory.annotation
.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 
'chefController' to bean named 'vegChef'
Output: Cook : VegChef

Previous topic @Qualifier Annotation

Volatile keyword in java

 Volatile keyword in java used to inform JVM and Thread to read the value of the variable from main method, not from thread stack. Volatile keyword cannot used for method or class declaration. Its only for variable declaration. 

We cannot declare the local variable as volatile, we will get compilation error. "Modifier 'volatile' not allowed here".

Do you think why cannot we declare volatile as local variable?.. 

Yes because all the local variable are line with in a thread stack and it cannot be shared outside the thread, but volatile can be shared across the thread.

When do we choose the volatile?. 

When a need arise the variable value remain same across the application. Better you can use the volatile variables for long and double variables, because they are the 64bit data type, usually it will split like 32+32bit while reading, so when one thread write first 32bit there is a possibility to read the two different thread, so if we make the variable to volatile the changes will be informed the other thread, JVM will take care it.

While reading it do you remember the static keyword also doing the same?? Only one copy will be passed over all the thread??

Static variable can be cached by thread, but volatile cannot be cached by thread. If we use static variable there is a chance, if multiple thread is accessing and updating the value one thread may have cached value, in volatile there is no cache allowed all the thread will be read from main memory.

So now you are clear with volatile and static keyword. 

Do you think about transient ?

transient keyword restrict from serialization, exclude the variable when serializing instance of a class. On deserialzing process default value will be assigned to the variable. (Object null, primitive 0 or false).We will use transient keyword when we convert the domain object in to entity. In domain class we can have n number of variables but in entity class we can have variable for particular Table column, so rest of the domain object not required for while converting from domain to entity, so we can use transient in this case.





Thursday, February 17, 2022

Spring @Qualifier Annotation

 There is a possibility to have more than one bean with same type, in this case your IDE will show the error like 'Could not autowire. There is more than one bean of '' Type'.  This says we need to explicitly specify the name for the Object. This can done by using @Qualifier annotation. Spring provide this annotation to specify the name. 

Here I have Chef Interface, VegChef, NonVegChef component implements Chef and override the doCook() method in both component. And trying to autowire in VegChefService service.



public interface Chef {
String doCook();
}

@Component
public class VegChef implements Chef {
@Override
public String doCook() {
return "Vegetarian Food";
}
}

@Component
public class NonVegChef implements Chef {
@Override
public String doCook() {
return "Non-Vegetarian Food";
}
}

@Service
public class VegChefService {
@Autowired
private Chef chef;
}



 Here we will get the error says "Could not autowire. There is more than one bean of 'Chef' type.

Let see how to resolve the problem by using @Qualifier to indicate which bean want to use. we need add @Qualifier code in Component class like below, and need to add @Qualifier in service class to specify the bean name.

@Qualifier("Veg")
public class VegChef implements Chef {
@Override
public String doCook() {
return "Vegetarian Food";
}

} 


@Service
public class VegChefService {
@Autowired
@Qualifier("Veg")
private Chef chef;
}

As conclusion @Qualifier used to resolve more then one bean issue. 

Next topic @Primary Annotation

Tuesday, February 15, 2022

Error executing query (ValidationError): Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type null of field

 Hello, While working with graphql query/mutation we will get some common exception, due to careless mistake, here we will see kind of issue, I got this exception while calling query resolver.


Query failed to validate : 'query getBookingTypes($email: String!) { getBookingTypes(email: $email) { BookingType } }' 


Error executing query (ValidationError): Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type null of field getBookingTypes @ 'getBookingTypes' 


This call is about to retrieve type of booking  method from QueryResolver which returns list of Object. 

We have checked the return type in both schema file and implementation and other check also looks good.

Below query and enum object,

Query:




enum BookingType{
ADMIN
USER
GUEST
}

Then we found that,  in output data field we have mentioned as BookingType. Here BookingType is ENUM.
So we have changed the query like below, 

After this changes we got output like below,
    ADMIN
USER
GUEST

Issue fixed!.

Wednesday, February 2, 2022

CSS Clock

 Here I created clock using CSS and HTML.


CSS:

body{
    background-color: aquamarine;
}
.clock{
  height: 150px;
  width: 150px;
  border-radius: 150px;
  left: 38%;
  top: 24%;
  position: absolute;
}

.hand{
    position: absolute;
    height: 30vmin;
    left: 49%;
    top: 15%;
    transform-origin: 50% 80%;
    box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.4);

  animation-name: moving;
 
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
.hour{
   
    width: 6px;
    background: whitesmoke;
   
  animation-duration: 43200s;
}
.second{
    width: 4px;
    background: rgb(189, 144, 144);
  animation-duration: 3600s;
 
}
.minute{
   
    background-color: hsl(0, 72%, 47%);
      width: 2px;
     
  animation-duration: 60s;
}

.hour12,
.hour1,
.hour2,
.hour3,
.hour4,
.hour5 {
    height: 10px;
    width: 375px;
    background: transparent;
    border-left: 70px solid #000;
    border-right: 70px solid #000;
    transform: translate(-50%, -50%);
    top: 75%;
    left: 50%;
    position: absolute;
}
.hour12{
  left:110px;
}
.hour3 {
  transform: rotate(90deg) translate(0, 34vmin);
 }

.hour1 {
  transform: rotate(120deg) translate(17vmin, 30vmin);
}

.hour2 {
  transform: rotate(150deg) translate(29vmin, 18vmin);
}

.hour4 {
  transform: rotate(210deg) translate(30vmin, -17vmin);
}

.hour5 {
  transform: rotate(240deg) translate(17vmin, -30vmin);
}


@keyframes moving {
    to {
      transform: rotate(360deg);
    }
}

HTML:

<div class="clock">
    <div class="hour12"></div>
    <div class="hour1"></div>
    <div class="hour2"></div>
    <div class="hour3"></div>
    <div class="hour4"></div>
    <div class="hour5"></div>
</div>
<div class="hand hour"></div>
<div class="hand second"></div>
<div class="hand minute"></div>

CLOCK:






The clock is running one, but I couldn't save them as supporting format. If u want to see the running clock, copy the code and try it in your machine.