November 27, 2011

Fixing ConcurrentModificationException

Here I am discussing the instances where, iterating over a collection throws the ConcurrentModificationException. As mentioned in the API documentation, the reason here is, "it is not generally permissible for one thread to modify a Collection while another thread is iterating over it."

Following is a code segment which can throw this exception.
Iterator itr = purchased_products.iterator();
while (itr.hasNext()) {
  if (itr.next().getProduct_id() == product_id) {
    Purchase p = itr.next();
    purchased_products.remove(p);
  }
}

There are a number of different solutions provided in forums. Almost all of them are provided as tweaks to iteration which is already happening.If you need to keep using the iterator, solution here will not work.

This solution gives up the iterator. This is a simple code which we all have being through, but may have missed out for situation as this. I found this by referring the remove method in java.util.ArrayList.

for (int index = 0; index < size; index++)
  if (elementData[index] == null) {
  fastRemove(index);

As in the above code segment from remove(Object o) method, I too used a for loop to find the index of the item to be removed and then called upon the remove(int index) method to remove the element. This will not result in any such errors and is efficient than using iterator. But it will highly depend on your requirement.

So the revision for the above code segment given earlier would be as,
for (int index = 0; index < purchased_products.size(); index++) {
  if (product_id == purchased_products.get(index).getProduct_id()) {
    purchased_products.remove(index);
    index--;
  }
}

November 23, 2011

SqlFairy

"sqlfairy" is a pretty cool software which helps you in instantly visualizing any huge sql database, when all you have is it's schema.

You just have to do a quick installation with sudo apt-get install sqlfairy and you are all good to go.

Just refer some tutorials are available from :

November 21, 2011

Eclipse block comment problem

Eclipse by default formats the block comments when you save changes of a file. It is additional trouble when you need to uncomment.

To solve this problem,

  • Go to Windows->Preferences->Java->Code Style->Formatter->Edit->Comments 
  • Uncheck Enable block comment formatting
  • Click on Apply

November 20, 2011

Git Reference Sheet

Git Repository

It contains a set of 'commit objects' and 'heads' which are references to those commit objects. Commit objects are a set of files that reflect the state of project at any given time. They also include references to parent commit objects. Head is a reference to a commit object. a head has a name. By default, every repository has a head called "master". A repository can contain many heads but at any given time, one head is selected as the "current head" which is always named as "HEAD".

(In simple terms, HEAD is the current branch. But there are exceptions to this. Another definition is to refer to HEAD as a reference to the last commit in the current checked out branch.)

Delete

delete a remote branch
git push git@github.com:....git :mahee2

delete a branch

git branch -d mahee

Push

Push is done when you want to push the work you have done upstream for sharing purposes.

General command is git push [remote-name] [branch-name]

If you want to push your master branch to origin server, run git push origin master

example : git push git@github.com:charithsoori/Cart-beta.git mahee

The real scene behind above commands are a little explanatory. when you give a command as git push origin mahee it means, to take the local branch named 'mahee' and push it to the remote 'mahee' branch. git push origin mahee:mahee does the same. (Take my local 'mahee' and make it remote 'mahee'). If it is a different branch in remote (e.g. test) just give the command as git push origin mahee:test

if above push doesn't work due to not having a common ancestor and you get the following error :-

To git@github.com:.....git
! [rejected]        mahee -> mahee (non-fast-forward)
error: failed to push some refs to 'git@github.com:charithsoori/Cart-beta.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.


Do force push using
git push --force
or
git push git@github.com:.....git mahee --force (untested)

Actually force is not a very good option and is not encouraged. if a simple git pull and then pushing doesn't work, then you might have to use force.

Fetch

to pull any new work since clone or last fetch

git fetch origin

here "origin" refers to the remote repository when a clone is taken. The fetch will pull all the data from the remote project which isn't currently available. after this fetch, references to all branches are available and it is possible to merge branches or inspect them at will. Fetch only pulls data to local repository. It does not do any merging or modification to current work. Those should be done manually.

Pull

git pull

If a branch is set to track a remote branch, you can use pull to automatically pull and merge a remote branch to the current branch.

When to fetch and when to pull?

http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Merge

git merge local mahee

merges local and mahee branches to local.

also, another method to do this merging is to checkout to local and then call on git merge mahee

Conflicting merge

git checkout branch1
git fetch origin branch2
git merge FETCH_HEAD

Auto-merging ......
CONFLICT (content): Merge conflict in ...........
Removing ..........
Auto-merging .........
CONFLICT (content): ..........
Automatic merge failed; fix conflicts and then commit the result.

when above output is received you can use a mergetool and perform merging in a little bit of a manual way.


git mergetool
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff
Merging:
.........
.........

Normal merge conflict for '....':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (kdiff3):

Normal merge conflict for '.........':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (kdiff3):


References