Friday, December 30, 2011

How to 'gracefully' deal with bean initialization failures in Spring 3 application?

If you rethrow the exception the context will stop loading and your application will be effectively dead. Or if you really want the JVM to completely stop call System.exit(1)

http://stackoverflow.com/questions/5421022/how-to-gracefully-deal-with-bean-initialization-failures-in-spring-3-applicati

Continute to load webapp even if one spring bean initialization fails

Continue to load webapp even if one spring bean initialization fails
AFAIK, you can't do this.
I do multiple DNS lookups on start up. I do not want the webapp to fail if one of them fails.
In that case, you need to modify your bean(s) to handle the case where the DNS lookup fails, leaving the bean instance in a state where it is essentially inactive but harmless, or where it can retry the DNS lookup later on.
In short, you have to cope with this yourself.

http://stackoverflow.com/questions/6920672/continute-to-load-webapp-even-if-one-spring-bean-initialization-fails

Thursday, December 29, 2011

Accessing Spring beans from Legacy code

As I have blogged before, we have been trying to move to using the Spring framework in our web applications. We inherited a large body of working code which used a home-grown framework based on a combination of what Martin Fowler describes as the "JSP as Handler" web presentation pattern in his Enterprise Application Architecture book and Webwork action controllers. Our rationale for introducing Spring into this application is that maintenance and enhancement are very difficult and time-consuming with the existing architecture.
However, the conversion is not going to happen overnight, and neither can we mandate that all new development should stop as we move to the new architecture. It is also not possible to build and deploy the new functionality in separate web applications, because we don't have the resources to handle the deployment issues that such an approach would require. So our new Spring enabled code co-exists with the legacy code in the same web application.
One by-product of this setup is that we often end up developing non-GUI components using Spring, which need to be hooked into existing legacy code so we can reuse the legacy request flow. There are three ways we could do this:
  • Build the Spring bean manually in our legacy code using explict constructor and setter calls.
  • Scrap the legacy request flow and rebuild it using Spring.
  • Obtain a reference to the Spring bean that has been configured and built declaratively by the Spring container from the legacy code.
The first approach is feasible, but it is extra code that you don't need to write. Also, the first approach may result in strange null pointer exceptions if you missed something. It is also less efficient to construct them each time than having it be built once by Spring. The second approach is good if your long term goal is to get rid of the existing architecture altogether, but it is even more risky than the first approach because new code will generally contain new bugs. Also, because writing code takes time, you will have to build this extra time into your estimates. In my opinion, the last approach takes the minimum effort, code and poses the least risk, and can be accomplished quite simply, as I show below.
Our approach is to build a Singleton bean which is ApplicationContextAware and instantiate it in the Spring container by defining it in the spring-servlet.xml file in our web application. Because it is ApplicationContextAware, Spring would populate it with a reference to the ApplicationContext. Since we just want references to Spring beans from it, we implement a static getBean() method which would delegate to the ApplicationContext.getBean() method. Here is the code for our Singleton. The bean definition for this bean is just this one line: In order to now get a reference to a bean named "mySpringBean" that has been defined in the Spring context, we will issue from somewhere in our legacy (non Spring-enabled) code: The only caveat is that your DispatcherServlet must be loaded in web.xml before any component that needs to call the SpringApplicationContext.getBean(). This ensures that the ApplicationContext has finished loading and SpringApplicationContext has a populated reference to it. This is the second time I had a need for this kind of bridge. The last time was at my previous employer where we were embedding Jive Forums software within our Spring enabled web application. We used the standard approach there, using the Spring ContextListener to store the ApplicationContext in the ServletContext, and then calling WebApplicationContextUtils.getApplicationContext(ServletContext) to get at the Spring context. I believe the approach I have outlined is better, since it is less code and there does not have to be a web container for this functionality to be available.

http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html

SQL SERVER – Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

There are three ways to retrieve the current datetime in SQL SERVER.
CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP is a nondeterministic function. Views and expressions that reference this column cannot be indexed. CURRENT_TIMESTAMP can be used to print the current date and time every time that the report is produced.
GETDATE()
GETDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed. GETDATE can be used to print the current date and time every time that the report is produced.
{fn Now()}
The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. {fn Now()} can be used to print the current date and time every time that the report is produced.
If you run following script in Query Analyzer. I will give you same results. If you see execution plan there is no performance difference. It is same for all the three select statement.
SELECT CURRENT_TIMESTAMP
GO
SELECT {fn NOW()}
GO
SELECT GETDATE()
GO

Performance:
There is absolutely no difference in using any of them. As they are absolutely same.
My Preference:
I like GETDATE(). Why? Why bother when they are same!!! 

Modify/view static variables while debugging in Eclipse

In the Debug Variables view their is a arrow button in the right of the view. the tooltip of this button is "Menu". When you click this button a drop down menu is shown where you can select
"Java" -> "Show static variables"

http://stackoverflow.com/questions/801193/modify-view-static-variables-while-debugging-in-eclipse

Wednesday, December 28, 2011

How to print classpath

http://snipplr.com/view/16079/ http://www.jguru.com/faq/view.jsp?EID=816577

Tuesday, November 29, 2011

Making a Spring bean Applicationcontext aware

Suppose we have a webapplication with spring,
and we are initializing spring’s WebApplicationContext by configuring ContextLoaderListener in web.xml.
How can we get an instance of spring’s applicationContext object?
Possible solutions:
While configuring spring through web.xml the spring ApplicationContext object is set in the ServletContext.
To get ApplicationContext we say

So we can get spring ApplicationContext only where ServletContext is avaliable i.e. in Servlet,ServletFilter or any implementation class of ServletContextListener.
What if we dont want to write a servletFilter or ServletContextListener .
What if we need to access a spring bean before a servletFilter or servlet is called(for authentication)and we dont want to write a ServletContextListener.
How can we obtain spring’s applicationContext in this case?
One way to achieve this would be to use the ApplicationContextAware interface provided by Spring.
Create a class which implements ApplicationContextAware. The method, “setApplicationContext(…)” gets called during the creation of this bean, providing the reference to the context. Our program should store this for a later interaction with the context.

Initialize the new bean in applicationContext.xml

Now we can call the static method, ApplicationContextProvider.getApplicationContext(), from any class in our application, to get assess to the Spring ApplicationContext.

http://blog.imaginea.com/making-a-spring-bean-applicationcontext-aware/

Monday, November 21, 2011

Increasing VDI file size on VirtualBox

This little tutorial explains how to increase size of existing .vdi virtual hard drive file once the guest OS is already installed on it. I’ve taken a KISS approach describing each end every single step.

http://www.llakomy.com/articles/increasing-vdi-file-size-on-virtualbox-part-1-using-hdclone
http://www.llakomy.com/articles/increasing-vdi-file-size-on-virtualbox-part-2-merging-partitions-with-gparted

what are Agile and Scrum development methodology?

In software development Agile is a set of values and principles formulated by a group of the industry leading figures in 2001.
Scrum is a concrete software development methodology that can be traced back to 1995 and was created by two of the original signatories of Agile Manifesto. Scrum supports Agile values.
The early approach to software project management usually referred to as “waterfall model” implies a sequence of clearly defined steps necessary to complete any project: define, plan, organise, execute and then close.
This represents a very neat, simple and above all convenient abstraction from the project management theorist point of view. When one stage follows another it is possible to define clear inputs and outputs for each stage, isolate and identify techniques and tools that are most useful at every phase. This is a clear example of reductionism in tackling project management complexity: keep splitting the whole into smaller bits until you get to understand each bit in isolation and then hopefully you will master the mechanics of their totality.
But as soon as the waterfall abstraction was presented it started to leak. Firstly it turned out that real-life process can not be clearly cut into stages, often definition is still changed on what it seems to be planning, organisation or even execution stage. So, in theory, the stages were allowed to overlap and theoreticians started drawing all sorts of diagrams there it’s possible to see how the definition stage gradually runs out as execution starts picking up during the organisation stage. Then, again only in theory, it is possible to accurately estimate during the planning stage how long it is going to take to develop a piece of software. Obviously, in actual practise, the estimates and other plans have to be tweaked right through the execution phase which gave birth to a string of complex yet not very meaningful methods as Earned Value Management.
The chronic problems with the waterfall abstraction gave birth to a number of “agile” project management methods (Agile, Scrum, XP Programming etc) that despite many differences at the more detailed level use the same fundamental principles to tackle product development complexity:
  • The project is organised as a number of small iterations through the classic project stages (definition, planning, organisation, execution and closure).
  • Each iteration aims at a relatively small yet semantically complete increment in product functionality or non-functional characteristics.
  • Strong end-user involvement throughout the project.
Since every iteration goes through a separate definition and planning stage the time horizon for various estimation and planning activities is greatly reduced. It helps achieve greater accuracy, hence make it easier to access feasibility, measure value and costs etc.
Small increments help controlling the scope, evaluate utility of the changes and keep users involved since there is always a fresh version of fully functioning product. It is also much easier to organise a number of teams working on a large project simultaneously when increments are kept small, this really helps tackling task dependencies.
The user feedback is key to iterative methods, since it helps the product evolve gradually in a right direction providing greater utility at the end as opposed to the uncertainly of user reaction that is natural to punctuated equilibrium driven by the waterfall “big-bang” approach.

http://stackoverflow.com/questions/677778/what-are-agile-and-scrum-development-methodology

Sunday, November 20, 2011

Error: “\Java\jre6\lib\ext\QTJava.zip was unexpected at this time.”

After installation of Weblogic Server if you get an error: “\Java\jre6\lib\ext\QTJava.zip was unexpected at this time.” This can be solved as under,

Check if QTJava.zip is set in %CLASSPATH% environment variable CLASSPATH=.;C:\Program Files (x86)\QuickTime\QTSystem\QTJava.zip

Remove C:\Program Files (x86)\QuickTime\QTSystem\QTJava.zip from %CLASSPATH% environment variable and restart the computer.

http://ora-soa.blogspot.com/2011/06/error-javajre6libextqtjavazip-was.html

Saturday, November 19, 2011

tablesorter

tablesorter is a jQuery plugin for turning a standard HTML table with THEAD and TBODY tags into a sortable table without page refreshes. tablesorter can successfully parse and sort many types of data including linked data in a cell. It has many useful features including:
  • Multi-column sorting
  • Parsers for sorting text, URIs, integers, currency, floats, IP addresses, dates (ISO, long and short formats), time. Add your own easily
  • Support secondary "hidden" sorting (e.g., maintain alphabetical sort when sorting on other criteria)
  • Extensibility via widget system
  • Cross-browser: IE 6.0+, FF 2+, Safari 2.0+, Opera 9.0+
  • Small code size
http://tablesorter.com/docs/

Tuesday, November 15, 2011

Linux Tutorial Series - Part 01 (Install and Update and Upgrade Ubuntu)

Linux Tutorial Series - Part 01 (Install and Update and Upgrade Ubuntu) 
Linux Tutorial Series Part 02 (Securing Ubuntu 11.04)

Checked vs Unchecked exception


Unchecked exceptions are those that extend RuntimeException class. Compiler will never force you to catch such exception or force you to declare it in the method using throws keyword. All other exception types (that do not extend RuntimeException) are checked and therefore must be declared to be thrown and/or catched.
Checked exceptions are used when you want the caller of your method (i.e the user of your API) to explicitly handle the exceptional case in your API. Checked exceptions are declared when you believe the call will be able to do something meaningful with that exceptional case, like retrying the call, rolling changes back or converting it into some user-readable error message.
If you believe that there is nothing useful the call can do about the exception (especially when it represents a bug, or a wrong usage of your API), then the exception should be unchecked. Also, an API with too many checked exceptions can be annoying to program with (e.g. try using java reflection API=)

http://stackoverflow.com/questions/4639432/checked-vs-unchecked-exception

Sunday, November 13, 2011

Why do we use Interface? Is it only for Standardization?

Purposes of Interfaces

  • create loosely coupled software
  • support design by contract (an implementor must provide the entire interface)
  • allow for pluggable software
  • allow different objects to interact easily
  • hide implementation details of classes from each other
  • facilitate reuse of software
Analogy 1: Much like the US space shuttle, Russian Soyuz spacecraft and Chinese Shenzhou 5 can all dock to the International Space Station, because they implement the same docking interface. (This is the just an example - I don't know if it's true in real life however let's suspend our disbelief for the sake of an example)
Analogy 2: Like you can plug various computer monitors into your home computer. You can plug a wall-size TV into it, an old CRT (the thick kind), a 20" flat screen, or a braille machine for the blind to "see" by touch. There's compatibility among these various/different devices and your computer because they all agree on interface standards.
Details of C# interfaces -- With C#/OOP interfaces you're doing the same kind of thing but in the unseen/virtual world.
You're correct about standardization, but also flexibility, scalability, extensibility, maintainability, reusability, testability and power.
(The more you use software interfaces the more these "buzz words" will be understood. And always consider interfaces in the real world because they have done us equally well.)

http://stackoverflow.com/questions/2026054/why-do-we-use-interface-is-it-only-for-standardization

Saturday, November 12, 2011

Strategy Pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Called ConcreteStrategyAdd's execute()
Called ConcreteStrategySubtract's execute()
Called ConcreteStrategyMultiply's execute()
http://en.wikipedia.org/wiki/Strategy_pattern

Tuesday, November 8, 2011

Agile programming for non-technical people

I like to cook, so I use a soup analogy. Traditional waterfall would be like following a strict recipe, measuring and timing everything exactly, without tasting it until it's completely finished. Agile is like starting with stock and a few basic ingredients, tasting it frequently and adding what your taste-tests show that it "needs."

http://stackoverflow.com/questions/233599/agile-programming-for-non-technical-people

What is “Agile Development”?

I reckon Agile and Iterative provides an excellent overview of the subject.
There are a number of agile processes out there:
An agile process tends to focus on iterations, and client feedback, to allow for the inevitabilty of changing requirements.
A waterfall process tries to define all requirements up front, and tends to be more inflexible to changing requirements.
In fact, the book gives an interesting account into why the fallacy of waterfall was perpetuated for so long.
I definitely recommend having a skim through this book if you can find it.
Also, IMHO what employers tend to be looking for when asking about Agile experience:
It pays to be aware of the following, but I wouldn't go on about these unless the company in question really has bought into agile. These disciplines are not always universally appreciated (which is a shame, as they are just as vital to preventing an agile project from accumulating cruft)
http://stackoverflow.com/questions/240602/what-is-agile-development

Saturday, November 5, 2011

Template Design Pattern

You have a base class with a template method that lists actions you want to execute. Inheriting class is what actually implements methods customizing them as appropriate for the type of robot that you are creating.
Automotive Robot:
Starting....
Getting a carburetor....
Installing the carburetor....
Revving the engine....
Stopping....

Cookie Robot:
Starting....
Getting a flour and sugar....
Baking a cookie....
Crunching a cookie....
Stopping....
Cookie Robot:
Starting....
Getting a flour and sugar....
Baking a cookie....
Stopping....

Monday, October 31, 2011

Queue Code

Efficiency of Queues
As with a stack, items can be inserted and removed from a queue in O(1) time.

Stack Code

The main() method in the StackApp class creates a stack that can hold 10 items, pushes 4 items onto the stack, and then displays all the items by popping them off the stack, and then displays all the items by popping them off the stack until it's empty. Here's the output.,

80 60 40 20

Notice how the order of the data is reversed. Because the last item pushed is the first one popped, the 80 appears first in the output. the the 80 appears first in the output.

Efficiency of Stacks
Items can be both pushed and popped from the stack implemented in the StackX class in constant O(1) time. That is, the time is not dependent on how many items are in the stack and is therefore very quick. No comparisons or moves are necessary. .

Sunday, October 30, 2011

Plain English explanation of Big O


Disclaimer, stolen from Jon Skeet's answer:

Quick note, this is almost certainly confusing Big O notation (which is an upper bound) with Theta notation (which is a two-side bound). In my experience this is actually typical of discussions in non-academic settings. Apologies for any confusion caused.

The simplest definition I can give for Big-O notation is this:

Big-O notation is a relative representation of the complexity of an algorithm.

There are some important and deliberately chosen words in that sentence:

relative: you can only compare apples to apples. You can't compare an algorithm to do arithmetic multiplication to an algorithm that sorts a list of integers. But two algorithms that do arithmetic operations (one multiplication, one addition) will tell you something meaningful;
representation: Big-O (in its simplest form) reduces the comparison between algorithms to a single variable. That variable is chosen based on observations or assumptions. For example, sorting algorithms are typically compared based on comparison operations (comparing two nodes to determine their relative ordering). This assumes that comparison is expensive. But what if comparison is cheap but swapping is expensive? It changes the comparison; and
complexity: if it takes me one second to sort 10,000 elements how long will it take me to sort one million? Complexity in this instance is a relative measure to something else.
Come back and reread the above when you've read the rest.

The best example of Big-O I can think of is doing arithmetic. Take two numbers (123456 and 789012). The basic arithmetic operations we learnt in school were:

addition;
subtraction;
multiplication; and
division.
Each of these is an operation or a problem. A method of solving these is called an algorithm.

Addition is the simplest. You line the numbers up (to the right) and add the digits in a column writing the last number of that addition in the result. The 'tens' part of that number is carried over to the next column.

Let's assume that the addition of these numbers is the most expensive operation in this algorithm. It stands to reason that to add these two numbers together we have to add together 6 digits (and possibly carry a 7th). If we add two 100 digit numbers together we have to do 100 additions. If we add two 10,000 digit numbers we have to do 10,000 additions.

See the pattern? The complexity (being the number of operations) is directly proportional to the number of digits n in the larger number. We call this O(n) or linear complexity.

Subtraction is similar (except you may need to borrow instead of carry).

Multiplication is different. You line the numbers up, take the first digit in the bottom number and multiply it in turn against each digit in the top number and so on through each digit. So to multiply our two 6 digit numbers we must do 36 multiplications. We may need to do as many as 10 or 11 column adds to get the end result too.

If we have two 100-digit numbers we need to do 10,000 multiplications and 200 adds. For two one million digit numbers we need to do one trillion (1012) multiplications and two million adds.

As the algorithm scales with n-squared, this is O(n2) or quadratic complexity. This is a good time to introduce another important concept:

We only care about the most significant portion of complexity.

The astute may have realized that we could express the number of operations as: n2 + 2n. But as you saw from our example with two numbers of a million digits apiece, the second term (2n) becomes insignificant (accounting for 0.00002% of the total operations by that stage).

The Telephone Book

The next best example I can think of is the telephone book, normally called the White Pages or similar but it'll vary from country to country. But I'm talking about the one that lists people by surname and then initials or first name, possibly address and then telephone numbers.

Now if you were instructing a computer to look up the phone number for "John Smith", what would you do? Ignoring the fact that you could guess how far in the S's started (let's assume you can't), what would you do?

A typical implementation might be to open up to the middle, take the 500,000th and compare it to "Smith". If it happens to be "Smith, John", we just got real lucky. Far more likely is that "John Smith" will be before or after that name. If it's after we then divide the last half of the phone book in half and repeat. If it's before then we divide the first half of the phone book in half and repeat. And so on.

This is called a bisection search and is used every day in programming whether you realize it or not.

So if you want to find a name in a phone book of a million names you can actually find any name by doing this at most 21 or so times (I might be off by 1). In comparing search algorithms we decide that this comparison is our 'n'.

For a phone book of 3 names it takes 2 comparisons (at most).
For 7 it takes at most 3.
For 15 it takes 4.
...
For 1,000,000 it takes 21 or so.

That is staggeringly good isn't it?

In Big-O terms this is O(log n) or logarithmic complexity. Now the logarithm in question could be ln (base e), log10, log2 or some other base. It doesn't matter it's still O(log n) just like O(2n2) and O(100n2) are still both O(n2).

It's worthwhile at this point to explain that Big O can be used to determine three cases with an algorithm:

Best Case: In the telephone book search, the best case is that we find the name in one comparison. This is O(1) or constant complexity;
Expected Case: As discussed above this is O(log n); and
Worst Case: This is also O(log n).
Normally we don't care about the best case. We're interested in the expected and worst case. Sometimes one or the other of these will be more important.

Back to the telephone book.

What if you have a phone number and want to find a name? The police have a reverse phone book but such lookups are denied to the general public. Or are they? Technically you can reverse lookup a number in an ordinary phone book. How?

You start at the first name and compare the number. If it's a match, great, if not, you move on to the next. You have to do it this way because the phone book is unordered (by phone number anyway).

So to find a name:

Best Case: O(1);
Expected Case: O(n) (for 500,000); and
Worst Case: O(n) (for 1,000,000).
The Travelling Salesman

This is quite a famous problem in computer science and deserves a mention. In this problem you have N towns. Each of those towns is linked to 1 or more other towns by a road of a certain distance. The Travelling Salesman problem is to find the shortest tour that visits every town.

Sounds simple? Think again.

If you have 3 towns A, B and C with roads between all pairs then you could go:

A -> B -> C
A -> C -> B
B -> C -> A
B -> A -> C
C -> A -> B
C -> B -> A

Well actually there's less than that because some of these are equivalent (A -> B -> C and C -> B -> A are equivalent, for example, because they use the same roads, just in reverse).

In actuality there are 3 possibilities.

Take this to 4 towns and you have (iirc) 12 possibilities. With 5 it's 60. 6 becomes 360.

This is a function of a mathematical operation called a factorial. Basically:

5! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
...
25! = 25 * 24 * ... * 2 * 1 = 15,511,210,043,330,985,984,000,000
...
50! = 50 * 49 * ... * 2 * 1 = 3.04140932... × 10^64
So the Big-O of the Travelling Salesman problem is O(n!) or factorial or combinatorial complexity.

By the time you get to 200 towns there isn't enough time left in the universe to solve the problem with traditional computers.

Something to think about.

Polynomial Time

Another point I wanted to make quick mention of is that any algorithm that has a complexity of O(na) is said to have polynomial complexity or is solvable in polynomial time.

Traditional computers can solve polynomial-time problems. Certain things are used in the world because of this. Public Key Cryptography is a prime example. It is computationally hard to find two prime factors of a very large number. If it wasn't, we couldn't use the public key systems we use.

Anyway, that's it for my (hopefully plain English) explanation of Big O (revised).

http://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o

Big 'O' notation and Java Constant time performance


Big 'O' notation and Java Constant time performance

Collection Constant time performance, in a line is "The same amount of time taken for a certain operation(method) regardless of the number of elements in the collection"

Big 'O' notation

The Big O notation is used to indicate the time taken by algorithms to run :-

For example:-
(N is the number of elements)

O(N):- The time taken is linerarly dependent to the number of elements.

O(log N):- The time taken is logarithmic to the number of elements.

O(1):- The time taken is constant time, regardless of the number of elements.

Please read here to find out about the big O notation:-

http://en.wikipedia.org/wiki/Big_O_notation

In java collections

Sets

1) The HashSet offers constant time performance for basic operations (add, remove, contains and size).

2) The TreeSet provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

3) Like HashSet, the LinkedHashSet provides constant-time performance for the basic operations (add, contains and remove)

Lists

4) The ArrayList offers constant time performance in the size, isEmpty, get, set, iterator, and listIterator operations. The add operation runs in linear time. All of the other operations run in linear time (roughly speaking).

Maps

5) The HashMap provides constant-time performance for the basic operations (get and put).

6) The TreeMap provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

7) Like HashMap, the LinkedHashMap provides constant-time performance for the basic operations (add, contains and remove), assuming the the hash function disperses elements properly among the buckets.

If you need a List you could use the ArrayList.

If you need a Set use the HashSet or for a sorted set use the TreeSet or an insertion ordered set use the LinkedHashSet.

If you need a Map use the HashMap or for a key sorted map use the TreeMap or a key insertion ordered map use the LinkedHashMap.

http://objectissues.blogspot.com/2006/11/big-o-notation-and-java-constant-time.html

Priority Queue Code

The insert() method checks whether there are any items; if not, it inserts one at index 0. Otherwise, it starts at the top of the array and shifts existing items upward until it finds the place where the new item should go. Then it inserts the item and increments nItems. Note that if there’s any chance the priority queue is full, you should check for this possibility with isFull() before using insert(). The front and rear fields aren’t necessary as they were in the Queue class because, as we noted, front is always at nItems-1 and rear is always at 0. The remove() method is simplicity itself: It decrements nItems and returns the item from the top of the array. The peekMin() method is similar, except it doesn’t decre-ment nItems. The isEmpty() and isFull() methods check if nItems is 0 or maxSize, respectively.

Efficiency of Priority Queues
In the priority-queue implementation we show here, insertion runs in O(N) time, while deletion takes O(1) time.

Three ways to find minimum and maximum values in a Java array

http://www.zparacha.com/minimum-maximum-array-value/

Selection Sort Code

Invariant
In the selectionSort.java program. the data items with indices less than or equal to out are always sorted.

Saturday, October 29, 2011

Insertion Sort Code


Invariants in Insertion Sort
At the end of each pass, following the insertion of the item from temp, the data items with smaller indices than ourter are partially sorted.

Can you explain Java pre/post increment/decrement?


If you are only incrementing or decrementing a variable, there's no difference.
i++;
++i;
These are equivalent statements in java.

If, however, you are assigning a value to a variable such as:

array[pos++] = 5;
This means, set the array element at position pos to 5 then increment pos by 1.

array[++pos] = 5;
This means, increment pos by 1 then set the array element at that new pos value to 5.

So if 'pos' was 3, in the first statement we're setting array[3] = 5. In the second statement we're setting array[4] = 5. In either situation, the next line of code will see 'pos' as 4.

This basically means if you are using pre-increment such as ++i this is evaluated before the rest of that code line is. If using post-increment such as i++ this is evaluated after the rest of the code line is.

http://answers.yahoo.com/question/index?qid=20100211211451AACctJj

In Java, what's the difference between public, default, protected, and private?

Modifier    | Class | Package | Subclass | World

public      |  Y    |    Y    |    Y     |   Y

protected   |  Y    |    Y    |    Y     |   N

no modifier |  Y    |    Y    |    N     |   N

private     |  Y    |    N    |    N     |   N
http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private

Tuesday, October 25, 2011

Depth First Search

Definitions
Adjacency
Two vertices are said to be adjacent to one another if they are connected by a single edge.
Paths
A path is a sequence of edges. There can be more than one path between two vertices.
Connected Graphs
A graph is said to be connected if there is at least on path from every vertex to every other vertex.

Representing a Graph in a Program
Vertices
In a very abstract graph program you could simply number the vertices 0 to N-1
Edges
Two methods are commonly used for graphs: the adjacency matrix and the adjacency list.
The Adjacency Matrix
An adjacency matrix is a two-dimensional array in which the elements indicate whether an edge is present between two vertices. If a graph has N vertices, the adjacency matrix is an NxN array.
The Adjacency List
The list in adjacency list refers to a linked list. An adjacency list is an array of lists. Each individual list shows what vertices a given vertex is adjacent to.

Depth-First Search
Rule1
If possible, visit an adjacent unvisited vertex, mark it, and push it on the stack.
Rule2
If you can't follow Rule 1, then, if possible, pop a vertex off the stack.
Rule3
If you can't follow Rule 1 or Rule 2, you're done.

You might say that the depth-first search algorithm likes to get as far away from the starting point as quickly as possible and returns only when it reaches a dead end. If you use the term depth to mean the distance from the starting point, you can see where the name depth-first search comes from.

Analogy
An analogy you might think about in relation to depth-first search is a maze. The maze - perhaps one of the people-size ones made of hedges, popular in England - consists of narrow passages (think of edges) and intersections where passages meet (vertices).

Depth-First Search and Game Simulations
Depth-First searches are often used in simulations of games (and game-like situations in the real world.) In a typical game you can choose one of several possible actions. Each choice leads to further choices, each of which leads to further choices, and so on into an ever-expanding tree-shaped graph of possibilities. a choice point corresponds to a vertex, and the specific choice taken corresponds to an edge, which leads to another choice-point vertex.

Chapter 13 Graphs
http://www.amazon.com/Data-Structures-Algorithms-Java-2nd/dp/0672324539/ref=sr_1_1?s=books&ie=UTF8&qid=1319590014&sr=1-1

Depth First Search Code

Output:
Visits: ABCDE

Monday, October 24, 2011

Binary Tree Code

Building tree with root value 25
=================================
  Inserted 11 to left of node 25
  Inserted 15 to right of node 11
  Inserted 16 to right of node 15
  Inserted 23 to right of node 16
  Inserted 79 to right of node 25

preorder traversing
=================================
Traversed 25
Traversed 11
Traversed 15
Traversed 16
Traversed 23
Traversed 79

postorder traversing
=================================
Traversed 23
Traversed 16
Traversed 15
Traversed 11
Traversed 79
Traversed 25

inorder traversing
=================================
Traversed 11
Traversed 15
Traversed 16
Traversed 23
Traversed 25
Traversed 79
http://www.roseindia.net/java/java-get-example/java-binary-tree-code.shtml

Sunday, October 23, 2011

Simon Sinek: How great leaders inspire action


http://www.ted.com Simon Sinek presents a simple but powerful model for how leaders inspire action, starting with a golden circle and the question "Why?" His examples include Apple, Martin Luther King, and the Wright brothers -- and as a counterpoint Tivo, which (until a recent court victory that tripled its stock price) appeared to be struggling.

Seth Godin: Sliced bread and other marketing delights


http://www.ted.com In a world of too many options and too little time, our obvious choice is to just ignore the ordinary stuff. Marketing guru Seth Godin spells out why, when it comes to getting our attention, bad or bizarre ideas are more successful than boring ones.

Richard St. John: Secrets of success in 8 words, 3 minutes



http://www.ted.com Why do people succeed? Is it because they're smart, or are they just lucky? Analyst Richard St. John condenses years of interviews into an unmissable 3-minute presentation on the real secrets of success.

  1. PASSION
  2. WORK
  3. FOCUS
  4. PERSIST
  5. IDEAS
  6. GOOD
  7. PUSH
  8. SERVE

Sonatype Nexus Maven Repository Manager

https://repository.sonatype.org/index.html#welcome

Sonatype Nexus™ Professional Version
Copyright © 2008-2011 Sonatype, Inc.
All rights reserved. Includes the third-party code listed at http://www.sonatype.com/products/nexus/attributions.
Sonatype and Sonatype Nexus are trademarks of Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation.
M2Eclipse is a trademark of the Eclipse Foundation. All other trademarks are the property of their respective owners.

To see the full license terms for this Software: Click Here
The product EULA is: http://www.sonatype.com/products/nexus/EULA

Linked Lists 9: Adding to an Empty List


Linked Lists 8: Adding to the End


The following code does not work if list is null.

Linked Lists 7: Some Subtleties


Linked Lists 6: Traversing with a while-loop


Linked Lists 5: Simple Operations (without loops)


Converting a Collection to an Array

http://www.exampledepot.com/egs/java.util/coll_GetArrayFromVector.html

Saturday, October 22, 2011

Linked Lists 4: The Empty List (null)


number of data values number of nodes
3 3
2 2
1 1
0 0
null signifies "the empty list"

A recursive definition of a linked list:
* null
* a node whose next refers to a linked list

Why Linked Lists?
* Linked list are BEAUTIFUL.
* Linked lists are more efficient than arrays/ArrayLists in some situations
* Linked lists provide a foundation for understanding other node-based data structures (trees, graphs)

Linked Lists 3: Using Nodes


Node  Node (x) Node
data: "A" data: "B" data: "C"
next: ---> next: ---> next: null

SyntaxHighlighter not showing toolbar


I've been trying to figure this one out myself. I won't claim to be 100% correct here, but from what I can tell, this is the answer:
  1. Toolbar was changed in update from version 2 to version 3.
  2. Toolbar no longer includes the icons and whatnot.
  3. The default toolbar is now the simple '?'.
This pretty much sucks, if it's true. The pop-up toolbar w/ icons is one of the things that made me choose SH over the other options.
This is what I'm guessing comparing the included CSS files in the latest package to the CSS available on sites that have a version with the "proper" toolbar enabled.

Awesome code syntax highlighting made easy

Awesome code syntax highlighting made easy

Linked Lists 2: The Node Class


Node class: What is the "next" of the last node? What does getNext() return for the last node? null

Linked Lists: Introduction


A node consists of data value and reference to the next node.

How to map a composite key with Hibernate?


To map a composite key, you can use the EmbeddedId or the IdClass annotations. I know this question is not strictly about JPA but the rules defined by the specification also applies. So here they are:

2.1.4 Primary Keys and Entity Identity

...
A composite primary key must correspond to either a single persistent field or property or to a set of such fields or properties as described below. A primary key class must be defined to represent a composite primary key. Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns. The EmbeddedId and IdClass annotations are used to denote composite primary keys. See sections 9.1.14 and 9.1.15.
...
The following rules apply for composite primary keys:
  • The primary key class must be public and must have a public no-arg constructor.
  • If property-based access is used, the properties of the primary key class must be public or protected.
  • The primary key class must be serializable.
  • The primary key class must define equals and hashCode methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.
  • A composite primary key must either be represented and mapped as an embeddable class (see Section 9.1.14, “EmbeddedId Annotation”) or must be represented and mapped to multiple fields or properties of the entity class (see Section 9.1.15, “IdClass Annotation”).
  • If the composite primary key class is mapped to multiple fields or properties of the entity class, the names of primary key fields or properties in the primary key class and those of the entity class must correspond and their types must be the same.

With an IdClass

The class for the composite primary key could look like (could be a static inner class):
public class TimePK implements Serializable {
    protected Integer levelStation;
    protected Integer confPathID;

    public TimePK() {}

    public TimePK(Integer levelStation, String confPathID) {
        this.id = levelStation;
        this.name = confPathID;
    }
    // equals, hashCode
}
And the entity:
@Entity
@IdClass(TimePK.class)
class Time implements Serializable {
    @Id
    private Integer levelStation;
    @Id
    private Integer confPathID;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;

    // getters, setters
}
The IdClass annotation maps multiple fields to the table PK.

With EmbeddedId

The class for the composite primary key could look like (could be a static inner class):
@Embeddable
public class TimePK implements Serializable {
    protected Integer levelStation;
    protected Integer confPathID;

    public TimePK() {}

    public TimePK(Integer levelStation, String confPathID) {
        this.id = levelStation;
        this.name = confPathID;
    }
    // equals, hashCode
}
And the entity:
@Entity
class Time implements Serializable {
    @EmbeddedId
    private TimePK timePK;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;

    //...
}
The @EmbeddedId annotation maps a PK class to table PK.

Differences:

  • From the physical model point of view, there are no differences
  • EmbeddedId somehow communicates more clearly that the key is a composite key and IMO makes sense when the combined pk is either a meaningful entity itself or it reused in your code.
  • @IdClass is useful to specify that some combination of fields is unique but these do not have a special meaning.
They also affect the way you write queries (making them more or less verbose):
  • with IdClass
    select t.levelStation from Time t
  • with EmbeddedId
    select t.timePK.levelStation from Time t

References

  • JPA 1.0 specification
    • Section 2.1.4 "Primary Keys and Entity Identity"
    • Section 9.1.14 "EmbeddedId Annotation"
    • Section 9.1.14 "IdClass Annotation
http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate