Wednesday, March 4, 2015

Project Lombok

Lombok  is a plugin that Java developers can use to save time and effort writing repeated code like one we see in data classes. I will describe it with examples and will include a link to video and the project site to download and try.

Consider the following data class Student which has some attributes and access to it Getters/Setters.


Too much code for a data class :-S

Look at Lombok and what it can do :-P


You can also control if you want only getters or setters using the @Getter and @Setter annotation before each attribute.


Now the code looks more pretty and readable as a data class. It has lots of other useful annotations that will save you lots of repeated code.

For example you can look at the @Cleanup annotation and what it does.

References:



Monday, December 1, 2014

FireMeUp Java Job Scheduler - Open Source

In one of the projects at my current employer me and my team were asked to use a Java Job Scheduling library to schedule some jobs doing some business at dynamically configured time. 

We decided to use Quartz as we were using Spring which provides a full integration to Quartz, and I was fascinated with the design and implementation of it.

I thought that I can start a new open source project that implements a Java Job Scheduler for learning purposes with some modifications/simplifications to the current design of Quartz library.

FireMeUp Scheduler is a simple Java scheduler that can be integrated within your application in a very simple way. The library is so simple and can be used in Small and Medium scale projects, and it provides the following functionality.

  •  Jobs that run every interval of time.
  • Jobs that run every day, week, biweekly, monthly, bimonthly, and yearly in a specific time of the day. (e.g., Every day at 12:00 AM).
  • Having more that a scheduler in your application.
  • Enable/Disable scheduler freely throughout your application life time.
  • Configuring more than one trigger and associating them to schedulers.
  • Triggers can be configured in more than one way either a simple trigger that is scheduled to be triggered every interval of time, and crontab trigger that is triggered either daily, weekly, biweekly, monthly, bimonthly, or yearly at specific time HH/MM/SS.
  • Implementing your jobs and associating them to triggers, more than one job to each trigger.
  • Register/Unregister triggers to schedulers.
  • Register/Unregister jobs to triggers.
  • Job can't overlap itself if its in a running state.
  • You can implement your own trigger by extending the AbstractTrigger that contains most of the business.
Future Functions

Here are some ideas I need to add to the current project, and anyone who needs to start his own open source life can start implementing them and be part of the project.
  • Adding state recovery functionality for the configured Schedulers, Triggers, Jobs, I think we can use in-memory (not recovered) and database (recovered) models.
  •  Threading management functionality, manage the number of concurrent running threads.
  •  Configuration management functionality, configuring the application programmatically and using a properties file.
  • Providing a crontab expression parser functionality.
  • Documentation of the whole project.
  • More complex examples and test cases.
Project Links

Wednesday, April 9, 2014

Maximum Triangle Area

Problem MaxTriangle SRM 449 Div-1 250 pts problem.

Here is the problem description:

A triangle with positive area has been positioned on the plane in such a way that all of its vertices are located at integer coordinates. The lengths of two sides of this triangle are equal to sqrt(A) and sqrt(B), where sqrt(X) denotes the squared root of X. Return the maximum area this triangle can have. If there is no such triangle, return -1 instead.

Solution to this problem:

We can use vectors to solve such kinds of problems, the input is the squared of magnitude of two vectors, so we need to get all vectors that can give a magnitude value equals to sqrt(A) and sqrt(B).

Given that magnitude of a two dimensional vector(x, y) can be computed using the following equation:

Magnitude = sqrt(x*x+y*y)

So we need to iterate all valid x and y that can evaluates to this x*x+y*y=c, where c equals the squared magnitude passed.

The following method takes a squared magnitude as a parameter and returns the list of vectors:


Note: For each valid x and y, the following are also valid as per the above equation (x, y), (-x, y), (x, -y), (-x, -y).

Now we can iterate for all pairs of vectors of both squared magnitudes A and B, to get the maximum area of a triangle. To compute the area of a triangle between two two dimensional vectors is done using cross product.

See the following video but this is for three dimensional vectors.

Cross product of two dimension vectors (x1, y1) and (x2, y2) = x1*y2-x2*y1

Read this.

Here is my code for this problem.


Monday, February 24, 2014

Sample ADF CRUD web application

In this post i will show you how to use ADF to create a simple CRUD application in just few minutes.

To know more about ADF "Application development framework" please go to this link.

1. Create an ADF web application.


2. Set application name "Sample Crud" and click finish.


3. Choose "Connect to a Database" and press "Create a Database Connection".


4. Put connection details, and connect to the Oracle Sample HR database, and make sure to test the connection.

Note: Make sure that the hr user is not locked, and if its locked you may need to unlock it.


5. Mark the "Connect to a database" step as Done.


6. Expand the "Build Business Services" step and click "Go to Substeps".


7. Expand "Create Entity Objects and Associations", Press "Create Entity Objects and Associations".



8. Choose the model project.


9. Choose the database connection created in step 3.


10. Press the Query button, and move the Jobs table to the right column.


11. For the updatable views, chose JobsView and move it to the right column, then press finish.


12. Mark the "Build Business Services" step as Done.


13. Open the "adf-config" unbounded task flow.


14. From the "Component Palette" panel drag the view icon to the task flow.


15. Rename it to "jobsView" and double click on it, and then click Ok.


16. Drag a button from "Component Palette" panel into the page, and rename it from the property panel.



17. Do the same for the Edit, Delete, View buttons.



18. From the "Data Control" panel, drag the JobsView1 into the page, and insert it as a read only table.


19. Choose "Single Row" for selection, and click Ok.


20. Back to the "adf-config" unbounded task flow, and drag a new view for the job form.


21. Double click the new page, and drag the JobsView1 into it from the "Data Control" panel as a form.



22. Check the "Include submit button" checkbox, and click Ok.



23. Back to the "adf-config" unbounded task flow, and drag a new view for the job details.


24. Double click the new view, and add the JobsView1 into it as a read only form, and click Ok.


25. Back to the "adf-config" unbounded task flow, In the "Data Control" panel, expand the JobsView1 -> Operations and drag the "CreateInsert" operation into the task flow.



26. Do the same for "Delete" operation.

27. From "Data Control" -> Operations, drag "Commit" and "Rollback" operations into the task flow page.


28. From the "Component Palette" panel click the "Control Flow Case" icon and drag from "jobsView" page to the "viewJobDetails" page, and name it "view".


29. Do the following to complete navigation flow for our business case:

A. "create" flow from "jobsView" to "createInsert" operation.
B. "edit" flow from "jobsView" to "jobForm" page.
C. "delete" flow from "jobsView" to "delete" operation.
D. "CreateInsert" flow from "CreateInsert" operation to "jobForm" page.
E. "Delete" flow from "Delete" operation to "Commit" operation.
F. "save" flow from "jobForm" page to "Commit" operation.
G. "cancel" flow from "jobForm" page to "Rollback" operation.
H. "Commit" flow from "Commit" operation to "jobsView" page.
I. "Rollback" flow from "Rollback" operation to "jobsView" page.
J. "back" flow from "viewJobDetails" page to "jobsView" page.


30. Go to the "jobsView" page, and double click it, and from the property panel, change the action for each button to be as follow:

A. "View" button action = "view"
B. "Create" button action = "create"
C. "Delete" button action = "delete"
D. "Edit" button action = "edit"


31. In the "jobForm" page, add a new button and name it cancel, and the action = cancel, imediate=true.


32. In the "viewJobDetails" page add a new button and name it back, and the action = back.


33. Right click the adf-config unbounded task flow and click run, if prompted to choose the run configuration choose "jobsView" page to be the start page.

34. Screen shots for the application:



Done.

Configuring logging for ADF application

In this post i will show you how to configure ADF logger that logs your messages/errors to a specific log file.  Here are the steps needed to do the configuration.

1. In JDeveloper right click IntegratedWeblogicServer instance, and choose Configure Oracle Diagnostic logging for "IntegratedWeblogicServer".



2. Choose the root logger, and in structure window expand log_handlers, copy log_handler named "old-handler".

3. Paste the copied log_handler and name it XX-custom-handler, from property window change .



4. Choose the path property and change its default value.



5. In the overview tab of logging.xml file, click the add symbol to add a persistent logger. In the logger name select the class file or package for which you want to use this logger. example : xx.oracle.app select a level based on the severity you need.
 




6. Expand the Root logger and choose the newly created logger, and in the handler declarations window press the add icon, and choose the custom handler.



7. Now select your project in the application window, and double click it to change the run configuration java options -> Choose the default run configuration -> Edit -> Java options and add the following option "-Djbo.debugoutput=adflogger".







8. Now you can use the ADFLogger util to create a logger instance that logs to you log file.


Done.

Tuesday, February 4, 2014

How to prepare for an interview - 17

Back to a new set of problems. If you didn't read previous posts start from here. Read previous post here.

Remember you should discuss these solutions with me, and refer if there are mistakes or other good solutions.

Problems

161. Print a binary tree in infix order. Recursive and iterative.


162. Recursively reverse a Linked List.


163. Implement the div operator without using / or %.


164. How to traverse a binary tree in order iteratively.


165. Print out all combinations of k numbers out of 1...N e.g. when k = 2, n = 4 Print out 12, 13, 14, 23, 24, 34.


166. Given a function for a fair coin, write a function for a biased coin that returns heads 1/n times (n is a param).


167. Given a binary tree, print out the elements in order. Without recursion.


168. Explain the difference between a LEFT and RIGHT SQL JOIN.


169. Extract the max 1000 record out of an array.


170. Code a text justification routine (Given a line length insert white space so text is uniformly displayed within the given length).


Done.

Read next post. Share this post to your Facebook, Twitter, LinkedIn.

How to prepare for an interview - 16

Back to a new set of problems. If you didn't read previous posts start from here. Read previous post here.

Remember you should discuss these solutions with me, and refer if there are mistakes or other good solutions.

Problems

151. Given an array of integers, now we want to erase all 0's (can be other value), and we want the result array condensed, meaning no empty cell in the array.


152. Given a list of words with a same size and a big string that contains one of the permutation of all the words combined(say p), find the start index of the string p in the big string.


153. Print out all prime numbers in a given string. abc2134kd31 -> 2, 13, 3, 3.


154. Generate a new array from an array of numbers. Start from the beginning. Put the number of some number first, and then that number, For example, from array 1, 1, 2, 3, 3, 1 You should get 2, 1, 1, 2, 2, 3, 1, 1.


155. Implement a function rotateArray(vector<int> arr, int r) which rotates the array by r places. Eg 1 2 3 4 5 on being rotated by 2 gives 4 5 1 2 3.


156. Implement a function string balanceParanthesis(string s); which given a string s consisting of some parenthesis returns a string s1 in which parenthesis are balanced and differences between s and s1 are minimum. Eg - "(ab(xy)u)2)" -> "(ab(xy)u)2" - ")))(((" -> "".


157. Write a C function to define strcmp(char *s1, char *s2) to return negative if s1 is smaller, positive if s2 is greater and 0 if they are equal.


158. Write a function that computes log2() using sqrt().


159. Design and implement an algorithm that would correct typos: for example, if an extra letter is added, what would you do?


160. Implement a power function to raise a double to an int power, including negative powers.


Done.

Read next post. Share this post to your Facebook, Twitter, LinkedIn.