Copiando coleções em Java

Em java, para fazer com que uma coleção tenha o mesmo conteúdo que outra, podemos fazer o seguinte:

  List<String> a = new LinkedList<String>();
  a.add("1");
  a.add("2");
  List<String> b = new LinkedList<String>(a); //aqui b copiará a

No entanto, os elementos de b, que copiou tudo de a, continuarão referenciando os mesmos elementos de a na memória. Em alguns casos isso pode não ser o comportamento desejado.

Para forçar o Java a duplicar as instâncias da coleção copiada na coleção de destino, adicione após o construtor de b  o seguinte comando:

  Collections.copy(b,a);

Observe que se vc chamar apenas o comando copy sem ter feito a construção corretamente, provavelmente encontrará uma exceção informando que B não suporta A, ou que B não é grande o suficiente para A.

Text replacement in multiple files using sed and find

A common task in programming routine is to change variable names, methods in multiple files. This can be performed by a refactor operation in the most IDEs (eclipse, netbeans). However if you have many text files, such as parameter files, configuration files you can do it in a single command in shell:

$ find . -name "*.c" -exec sed -i "s/oldWord/newWord/g" '{}' \;

Explaining:

  1. find . -name “*.txt”: search all files with extension “.txt”, starting from current directory (.)
  2. -exec sed -i : edit file (option -i) with name received from find
  3. “s/oldWord/newWord/g”: replace all ocurrences “oldWord” by “newWord”
  4. ‘{}’: list of file names retrieved from find
  5. \;: end of exec command (it allow mutiple commands)

See more:

http://how-to.wikia.com/wiki/Howto_use_find_and_sed_to_make_a_replace_in_multiple_files

See ya

Google Wave Notifier

As I didn’t knew any notifier to Google Wave (such as a Gmail Notifier that seems be out of plans of Wave project), I entered in the site sporadically and wore little. A third party releases a Google Wave Notifier to Google Chrome that adds an icon next to the search bar that allows you to check if you received a message and also make a shortcut to the site. Follow a preview:

See ya

Meld: diff and merge files

Diff tools are very useful to identify changes in file versions (CVS, subversion). There are some command line tools such as diff and diff3 that compare two or three files, line per line, returning those that present differences. However, in some cases, a visual tool that highlight the differences could be more effective. Meld aims to handle this issue. It can be used not only to compare files but also to compare folders, to find files that exists in one and doesn’t in other. It can be installed through apt-get tools:

sudo apt-get install meld

Take a look on some previews provided by Meld Project:

See ya

Shutter: A snipping tool for Ubuntu

The standard PrintScreen key get the image of the entire Desktop. If you want just the active window, on Ubuntu, you can use Alt + PrintScreen. However, sometimes you may need only to snip only a specific part of the active window. The intuitive option is to take a PrintScreen and cut it on a image tool such GIMP. By the way, if you need to do it many times you can think on better alternatives, such as Shutter, a useful tool that can be configure to replace the standard PrintScreen key and handle this issue. You can install using apt-get:

sudo apt-get install shutter

The installation does NOT configure it as the default PrintScreen mechanism. To do it, open Shutter tool, click on Edit > Preferences, go to [Behavior] tab and mark [Capture] and [Capture with selection] options. After it, you may need to restart the GDM.

Check out some previews offered on Shutter Blog:

See ya

Follow

Get every new post delivered to your Inbox.