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

                    The following will fork bomb your shell 

:(){ :|:& };:

You can use ulimit to prevent yourself against this:

ulimit -m 1000000
ulimit -v 1000000
ulimit -u 500

                
                    To react on multiple buttons in webforms use the following if you don't want to depend on the value attribute of the button (think of i18n):
<form>
<input name="foo">
<input type="submit" name="buttonAction.ok" value="submit">
<input type="submit" name="buttonAction.cancel" value="cancel">

And in the action:
class FooAction implements Action {
    private Map buttonAction = new HashMap();
    public Map getButtonAction() { return buttonAction; }

    public String execute() {
        if(buttonAction.containsKey("ok") {
          // ok was pressed
        } else if (buttonAction.containsKey("cancel") {
          // cancel was
        } else {
          // no button was pressed (enter key?)
        }
        ...
    }
}
                
                    Character 0x2026 is the ellipsis "..." (three dots):

Use it in java for Buttons, e.g: "Refresh...":

    private class RefreshAction extends AbstractAction {

        private RefreshAction() {
            super("Refresh\u2026");
        }

        public void actionPerformed(ActionEvent e) {
            ...
        }
    }

Or in HTML:
                
                    
class Foo {
    private String string;
    private Date date;
    private i;
    
    [...]

    public int hashCode() {
        int result;
        result = (string != null ? string.hashCode() : 0);
        result = 29 * result + (date != null ? date.hashCode() : 0);
        result = 31 * result + (i != null ? i : 0);
        [...]
        return result;
    }

Consider using larger prime numbers  if the number of properties is large.                
                    
class Foo {
   private String string;
   private int i;
   [...]

   public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Foo)) return false;

        final Foo other = (Foo) o;

        if (string != null ? !string.equals(other.name) : other.name != null) return false;
        if (i != other.i) return false;
        [...]
        return true;
}
                
                    
import md5
hash = md5.new("Hello world").digest()
more info here http://docs.python.org/lib/module-md5.html                
                    Use the following to automatically use the current class name for the log category (example is for commons-logging, works with log4j, too):

class Foo {
   private final Log log = LogFactory.getLog(getClass());
}

Compare to the following where the class name is refered to explicitely. If the class is renamed or parts of the codes are copied ("reused") the logging will by misleading.

class Foo {
   private static final Log log = LogFactory.getLog(Foo.class);
}


                
                    No more letter counting... :-)
edit: see kirk's faster solution "String_Begins_With2"

function String_Begins_With($needle, $subject) {
 return (substr($subject, 0, strlen($needle))==$needle);
}
                
                    
window.onbeforeunload = function (evt) {
  var message = 'Are you sure you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}
http://www.highdots.com/forums/archive/index.php/t-66363.html                
                    This attaches a style attribute to the tr tag, but only on every other tag.

<xsl:template match="my_repeating_item">
<tr>
  <xsl:if test="position() mod 2 != 1">
    <xsl:attribute  name="style">background-color:#dddddd</xsl:attribute>
  </xsl:if>
  <xsl:apply-templates/>
</tr>
<xsl:template>