Wednesday, November 5, 2014

“Non-resolvable parent POM: Could not transfer artifact” when trying to refer to a parent pom from a child pom with ${parent.groupid}

Looks like you're trying to both inherit the groupId from the parent, and simultaneously specify the parent using an inherited groupId!
In the child pom, use something like this:
4.0.0


  org.felipe
  tutorial_maven
  1.0-SNAPSHOT
  ../pom.xml


tutorial_maven_jar
Using properties like ${project.groupId} won't work there. If you specify the parent in this way, then you can inherit the groupId and version in the child pom. Hence, you only need to specify the artifactId in the child pom.

Sunday, October 26, 2014

DIFFERENCE BETWEEN HIBERNATE FLUSH AND COMMIT

Flushing the Session simply makes the data that is currently in the session synchronized with what is in the database.
However, just because you have flushed, doesn’t mean the data can’t be rolled back.
Commit does flush the session, but it also ends the unit of work.
To summarize commit does two things,
1. Commit internally does flush
2. Ends the unit of work (makes the changes permanent).
FLUSH:
Flushing the Session simply gets the data that is currently in the session synchronized with what is in the database. However, just because you have flushed, doesn’t mean the data can’t be rolled back.
Hibernate will flush changes automatically for you:
before query executions
when a transaction is committed
Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
COMMIT:
Commit does flush the session, but it also ends the unit of work

Thursday, October 23, 2014

Notepad++ Multi editing

Yes: simply press and hold the Alt key, click and drag to select the lines whose columns you wish to edit, and begin typing.
You can also go to Settings > Preferences..., and in the Editing tab, turn on multi-editing, to enable selection of multiple separate regions or columns of text to edit at once.
It's much more intuitive, as you can see your edits live as you type.

Tuesday, October 21, 2014

Does a method's signature in Java include its return type?

Quoting from Oracle Docs:
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
enter image description here
Since the question was edited to include this example:
public class Foo {
    public int  myMethod(int param) {}
    public char myMethod(int param) {}
}
No, the compiler won't know the difference, as their signature: myMethod(int param) is the same. The second line:
    public char myMethod(int param) {}
will give you can error: method is already defined in class, which further confirms the above statement.

Wednesday, October 15, 2014

Maven does not find JUnit tests to run

By default Maven uses the following naming conventions when looking for tests to run:
  • Test*
  • *Test
  • *TestCase
Your test class doesn't follow these conventions. You should rename it or configure Maven Surefire Plugin to use another pattern for test classes.

Tuesday, October 7, 2014

How to run a JAR file

You need to specify a Main-Class in the jar file manifest.
Sun's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:
Test.java:
public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}
manifest.mf:
Manifest-version: 1.0
Main-Class: Test
Then run:
javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar
Output:
Hello world
http://stackoverflow.com/questions/1238145/how-to-run-a-jar-file

Cannot make Project Lombok work on Eclipse (Helios)

After adding lombok and restarting eclipse or spring tools my project still failed to recognize getters and setters. Red markers everywhere!
The solution: right-click your project, go to Maven and select Update Project
After hours of searching and trying random solution, I find this to be the only solution that worked for me.
enter image description here

Friday, October 3, 2014

How do you rotate a two dimensional array?

O(n^2) time and O(1) space algorithm ( without any workarounds and hanky-panky stuff! )
Rotate by +90:
  1. Transpose
  2. Reverse each row
Rotate by -90:
  1. Transpose
  2. Reverse each column
Rotate by +180:
Method 1: Rotate by +90 twice
Method 2: Reverse each row and then reverse each column
Rotate by -180:
Method 1: Rotate by -90 twice
Method 2: Reverse each column and then reverse each row
Method 3: Reverse by +180 as they are same

Friday, September 26, 2014

java: how for loop work in the case of BigInteger

You use these syntax instead:
BigInteger i = BigInteger.valueOf(100000L);  // long i = 100000L;
i.compareTo(BigInteger.ONE) > 0              // i > 1
i = i.subtract(BigInteger.ONE)               // i = i - 1
So here's an example of putting it together:
    for (BigInteger bi = BigInteger.valueOf(5);
            bi.compareTo(BigInteger.ZERO) > 0;
            bi = bi.subtract(BigInteger.ONE)) {

        System.out.println(bi);
    }
    // prints "5", "4", "3", "2", "1"
Note that using BigInteger as a loop index is highly atypical. long is usually enough for this purpose.

API links


The compareTo idiom

From the documentation:
This method is provided in preference to individual methods for each of the six boolean comparison operators (<==>>=!=<=). The suggested idiom for performing these comparisons is: (x.compareTo(y)0), where  is one of the six comparison operators.
In other words, given BigInteger x, y, these are the comparison idioms:
x.compareTo(y) <  0     // x <  y
x.compareTo(y) <= 0     // x <= y
x.compareTo(y) != 0     // x != y
x.compareTo(y) == 0     // x == y
x.compareTo(y) >  0     // x >  y
x.compareTo(y) >= 0     // x >= y
This is not specific to BigInteger; this is applicable to any Comparable in general.

Note on immutability

BigInteger, like String, is an immutable object. Beginners tend to make the following mistake:
String s = "  hello  ";
s.trim(); // doesn't "work"!!!

BigInteger bi = BigInteger.valueOf(5);
bi.add(BigInteger.ONE); // doesn't "work"!!!
Since they're immutable, these methods don't mutate the objects they're invoked on, but instead return new objects, the results of those operations. Thus, the correct usage is something like:
s = s.trim();
bi = bi.add(BigInteger.ONE);
http://stackoverflow.com/questions/3024186/java-how-for-loop-work-in-the-case-of-biginteger