Monday, 4 April 2016

Julia: User defined types (composite types)

You can define custom types using the keyword ‘type’.

type Employee
    firstName::ASCIIString
    lastName::ASCIIString
    id::Int64
end

Above snippet create new type Employee, it stores firstName, lastName and id of an employee. You can access the properties of employee using ‘.’ Notation.

emp1=Employee("Hari Krishna", "Gurram", 1)
Above statement defines employee emp1 with firstName "Hari Krishna", lastName "Gurram" and id 1.
julia> type Employee
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int64
       end

julia> typeof(Employee)
DataType

julia> emp1=Employee("Hari Krishna", "Gurram", 1)
Employee("Hari Krishna","Gurram",1)

julia> emp1.firstName
"Hari Krishna"

julia> emp1.lastName
"Gurram"

julia> emp1.id
1


Custom type is similar to a class without methods in java. Custom types created with the keyword ‘type’ are mutable, i.e, you can change the values of these type.
julia> emp1
Employee("Hari Krishna","Gurram",1)

julia> emp1.firstName="Sudheer"
"Sudheer"

julia> emp1.lastName="Ganji"
"Ganji"

julia> emp1
Employee("Sudheer","Ganji",1)


Julia pass the objects to function by reference, so any changes you made to the object inside a function are visible outside also.

julia> function changeEmployee(emp; firstName=emp.firstName, lastName=emp.lastName, id=emp.id)
           emp.firstName=firstName
           emp.lastName=lastName
           emp.id=id
       end
changeEmployee (generic function with 1 method)

julia> emp1
Employee("Gopi","Ganji",1)

julia> changeEmployee(emp1, lastName="Battu", id=1234)
1234

julia> emp1
Employee("Gopi","Battu",1234)


Immutable types
Some times you want to create immutable types. You can create immutable type using the keyword immutable.

julia> immutable Student
           id::Int64
           name::ASCIIString
       end

julia> stud1=Student(1, "Krishna")
Student(1,"Krishna")

julia> stud1.id
1

julia> stud1.name
"Krishna"


You can’t change the value of immutable type. You will get an error, when you try to change the value of immutable type.

julia> stud1.id=5
ERROR: type Student is immutable

julia> stud1.name="abc"
ERROR: type Student is immutable


Benefits of immutable objects
1.   Immutable objects are inherently thread safe, so you no need to worry about thread safety.
2.   Since immutable objects state can't change, they never get into an inconsistent state.
3.   Since the properties(fields) of immutable objects can't change, you can cache the results of the operations performed on immutable objects and reuse the result.
4.   Immutable objects are best fit to use them as keys in Dictionary.
5.   Improves the performance

Type aliases
We can create a type that is similar to existing type using typealias.

julia> typealias permEmployee Employee
Employee

julia> emp1=permEmployee("Sudheer","Ganji",1)
Employee("Sudheer","Ganji",1)

julia> emp1
Employee("Sudheer","Ganji",1)

julia> typeof(emp1)
Employee


Note
Every concrete type is an instance of DataType.

julia> typeof(Employee)
DataType








Previous                                                 Next                                                 Home

Saturday, 2 April 2016

Julia: Get sub types of given type


subtypes(T): Return a list of immediate subtypes of DataType T.
julia> subtypes(Number)
2-element Array{Any,1}:
 Complex{T<:Real}
 Real            

julia> subtypes(Real)
4-element Array{Any,1}:
 AbstractFloat       
 Integer             
 Irrational{sym}     
 Rational{T<:Integer}

julia> subtypes(Integer)
4-element Array{Any,1}:
 BigInt  
 Bool    
 Signed  
 Unsigned

julia> subtypes(Bool)
0-element Array{Any,1}

julia> subtypes(BigInt)
0-element Array{Any,1}

Note
Any is the super type for all data types in Julia.


Previous                                                 Next                                                 Home

Java: Capture screenshot programmatically

In this post, I am going to show you how to take a screen shot and save it to your computer using Java API. java.awt.Robot class provides createScreenCapture method to capture a screen shot.

Capture full screen
Following statements are used to capture full screen.
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(destination));

‘format’ can be “jpg”, “png”, “bmp”, etc.

Capture portion of the screen
Following statements are used to capture the partial screen.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
Robot robot = new Robot();
BufferedImage screenFullImage = robot.createScreenCapture(rect);
ImageIO.write(screenFullImage, format, new File(destination));


Following is the complete working application.

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

import javax.imageio.ImageIO;

public class Test {
   /**
    * Capture the full screen and save it to the destination.
    * 
    * @param destination
    * @throws AWTException
    * @throws IOException
    */
   public static void captureFullScreen(final String destination,
         final String format) throws AWTException, IOException {
      if (Objects.isNull(destination) || Objects.isNull(format))
         return;
      Robot robot = new Robot();
      Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
            .getScreenSize());
      BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
      ImageIO.write(screenFullImage, format, new File(destination));
   }

   /**
    * Capture rect portion of the screen.
    * 
    * @param rect
    * @param destination
    * @param format
    * @throws AWTException
    * @throws IOException
    */
   public static void capturePortion(final Rectangle rect,
         final String destination, String format) throws AWTException,
         IOException {
      if (Objects.isNull(rect) || Objects.isNull(destination)
            || Objects.isNull(format))
         return;
      Robot robot = new Robot();
      BufferedImage screenFullImage = robot.createScreenCapture(rect);
      ImageIO.write(screenFullImage, format, new File(destination));
   }

   public static void main(String args[]) throws AWTException, IOException {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle rect = new Rectangle(0, 0, screenSize.width / 2,
            screenSize.height / 2);

      captureFullScreen("fullscreen", "png");
      capturePortion(rect, "halfScreen", "jpg");
   }
}


Friday, 1 April 2016

Julia: Get parent type of data type


super(T) return the super type of data type T.

julia> super(Int64)
Signed

julia> super(Signed)
Integer

julia> super(Integer)
Real

julia> super(Real)
Number

julia> super(Number)
Any

julia> super(Any)
Any

Note
Any is the super type for all data types in Julia.



Previous                                                 Next                                                 Home