2014-04-20

For-each loop vs Iterator vs C-style for loop

In Java a for loop that uses an iterator
List<String> strings = new LinkedList<>(Arrays.asList("first", "second"));
for (Iterator iterator = strings.iterator(); iterator.hasNext();) {
    System.out.println(iterator.next());
}
is essentially the same as
List<String> strings = new LinkedList<>(Arrays.asList("first", "second"));
for (String string : strings) {
    System.out.println(string);
}
because the generated byte code for the second code snippet executes the same operations underneath as for the first code snippet. So the for-each loop, introduced with Java 5 is merely syntactic sugar for the for loop that explicitly deals with an iterator.

However this C-style for loop works with an index
List<String> strings = new LinkedList<>(Arrays.asList("first", "second"));
for(int index = 0; index < strings.size(); index++) { 
    System.out.println(strings.get(index));
}
and uses #get(index) instead of iterator.next(). This has vast implications on the execution performance of this loop. Both former loops call iterator.next() which is an O(1) operation, making the both loops to O(n) operations whereas strings.get(index) is an O(n) operation, making the entire loop an O(n²) operation which is substantially slower.

The first and last loop can be both expressed using the for-each' syntactic sugar form.

2014-04-06

Manipulate / change git' commit timestamp

After doing a regular commit, issue this command:
git commit --amend --date="Mon Apr 7 07:33 2014 +0200"

2014-03-25

Maven PermGen space OutOfMemoryError issue

If you get something like
[ERROR] PermGen space -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
...try doing this to resolve this OutOfMemoryError issue:

 export MAVEN_OPTS="-Xmx1g -XX:MaxPermSize=512m"

2014-03-24

@Inject vs. @Resource vs. @Autowired — Java vs. Spring

@Autowired (Spring) & @Inject (Java)
  1. Matches by type
  2. Restricts by qualifiers
  3. Matches by name
@Resource (Java)
  1. Matches by name
  2. Matches by type
  3. Restricted by qualifiers, ignored if match is found by name

Replace SourceTree by CLI Git, branch names in "git log"

A graphical representation of a multi-branch commit history is usually one of the most used features of GUI tools like SourceTree. You can get the same thing in a much simpler way using
git log --graph --all --decorate
So you will see all branches, their merge commits and the branches that have been merged including a visual branch graph.

2014-03-20

Maven: No plugin found for prefix 'jetty' in the current project...

 If you get something like

[ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups [de.hybris.mavenplugins, org.codehaus.cargo, de.hybris.platform, com.sap.research, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/alex/.m2/repository), central (http://repo1.maven.org/maven2), hybris-repository (http://repository.hybris.com/hybris-repository)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
add something like the following to your ~/.m2/settings.xml
<pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
...to resolve this issue.

2014-03-12

Git branch name in your prompt

When you want to get something similar like
idea:~/my/project/hybris/order-service (develop) 
as your Bash prompt on a Mac or perhaps some other Unix OSs — where "(develop)" could be your currently checked out Git branch — add this
PS1='\h:\[\e[0;31m\]\w\[\e[m\]`__git_ps1` ' 
or 
PS1='\h:\[\e[0;31m\]\w\[\e[m\] \e[1;31m\]$(__git_ps1 %s)\e[m ' 
to your ~/.bash_profile

Alternatively you could use
PS1='`__git_ps1` '
just to see the branch name.

2014-03-08

NPE killer in Java8 to prevent NullPointerExceptions

In JDK8 the Optional<T> type monad has been introduced. The purpose of this class is essentially to facilitate the active thinking about the case when null might be assigned to an object causing a NullPointerException when this object will be dereferenced.

Consider this:
public static Optional<Drink> find(String name, List<Drink> drinks) {  
  for(Drink drink : drinks) {
    if(drink.getName().equals(name)) {
    return Optional.of(drink);
    }
  }
  return Optional.empty();
}
List<Drink> drinks = Arrays.asList(new Drink("Beer"), new Drink("Wine"), new Drink("Cocktail"));
Optional<Drink> found = find("Beer", drinks);
if(found.isPresent()) {
  Drink drink = found.get();
  String name = drink.getName();
}

2014-03-06

Delete a remote / local Git branch

git branch -d your_local_branch_to_delete # use the simple name
git push origin :your_remote_branch_to_delete # use the simple name

Mark conflicting files as resolved in Git

After you've manually resolved a Git merge conflict, you have to mark the resulting file as "resolved" (notion from other DVCSs). Just do this to achieve this:

git add your_file_with_manually_resolved_conflicts.txt