DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    Grep a bunch of jars for a certain package or class or expression:

find /path/to/directory -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q query && echo "{}"'

E.g.

find ~/soft/java/maven-repository -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q org.apache && echo "{}"'
                
                    This BigDecimalFormatter tries to parse the text as BigDecimal for the current locale. It uses the BigDecimal(String) constructor to keep the number of decimals from the text.

public class BigDecimalFormatter extends NumberFormatter {
    public Object stringToValue(String text) throws ParseException {
        if("".equals(text.trim())) {
             return null;
         }
         char ds = getDefaultLocaleDecimalSeparator();

         try {
             String val = text;
             if(ds != '.') {
                 val = val.replace(".", "").replace(ds, '.');
             }
             return new BigDecimal(val);
         } catch(NumberFormatException e) {
             return null;
         }
    }

    public String valueToString(Object value) throws ParseException {
        return super.valueToString(value);
    }

    private char getDefaultLocaleDecimalSeparator() {
          DecimalFormatSymbols symbols = new DecimalFormat("0").getDecimalFormatSymbols();
          char ds = symbols.getDecimalSeparator();
          return ds;
      }

}

public class BigDecimalFormatterFactory extends JFormattedTextField.AbstractFormatterFactory {
    public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
        return new BigDecimalFormatter();
    }
}

Use it like this:

bigdecimalfield.setFormatterFactory(new BigDecimalFormatterFactory());
                
                    To create a borderless JButton in swing use the following:

JButton taskButton = new JButton(action);
taskButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
taskButton.setHorizontalAlignment(JButton.LEADING); // optional
taskButton.setBorderPainted(false);
taskButton.setContentAreaFilled(false);

This is useful for "hyperlink" style buttons or "task buttons".
                
                    To change all .htm files in a folder to .html files:

for f in *.htm; do mv $f `basename $f .htm`.html; done;
                
                    Go back to your previous directory:

cd -

Go to a new directory and push the current one onto a stack:

pushd /newdirectory

Pop a directory from the stack and go to it:

popd
                
                    
things = [] #[1,2,3]

if things.each do |thing|
    puts thing
end.empty? then
    puts "no things!"
end
                
                    Example of a Auto Attendant menu.

[MainMenu]
exten => s,1,SetVar(NUMINVALID=0)
exten => s,2,Setvar(NUMTIMEOUTS=0)
exten => s,3,Background(sales_press_one)
exten => s,4,Background(support_press_two))
exten => s,5,ResponseTimeout(5)
exten => s,6,DigitTimeout(2)

exten => t,1,SetVar(NUMTIMEOUTS=$[${NUMTIMEOUTS} + 1])
exten => t,2,Gotoif($[${NUMTRIES} < 2]?s,3)
exten => t,3,Background(vm-goodbye)
exten => t,4,Hangup()

exten => i,1,SetVar(NUMINVALID=$[${NUMINVALID} + 1])
exten => i,2,Gotoif($[${NUMINVALID} < 4]?:10)
exten => i,3,Background(invalid)
exten => i,4,Goto(s,2)
exten => i,10,Playback(vm-goodbye)
exten => i,11,Hangup()
                
                    
sudo port install ruby
sudo port install rb-rubygems
sudo gem install rails --include-dependencies
sudo ln /opt/local/bin/ruby /usr/local/bin/ruby

Originally from http://labs.broqn.com/blog/2005/03/ruby-on-rails-with-darwinports/ (added --include-dependencies)                
                    
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od login
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od update -P darwinports

or, if you didn't get the source from CVS:

sudo port -d selfupdate
                
                    if you do not already have a key pair
$ ssh-keygen -t dsa

send your public key to each host you ssh into
$ cat ~/.ssh/id_dsa.pub | ssh you@host 'cat - >> ~/.ssh/authorized_keys2'

now you can use ssh-agent and ssh-add to facilitate a "single signon" situation.

useful if you're ssh'ing into a bunch of machines all day.

google ssh-agent for distribution-specific details.