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


  • submit to reddit

Recent Snippets

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( : != null) return false;
        if (i != other.i) return false;
        return true;
import md5
hash ="Hello world").digest()
more info here                
                    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;
                    This attaches a style attribute to the tr tag, but only on every other tag.

<xsl:template match="my_repeating_item">
  <xsl:if test="position() mod 2 != 1">
    <xsl:attribute  name="style">background-color:#dddddd</xsl:attribute>
                    Mark all links except ones inside your domein
a:not([href*="mydomain"]) {
	padding-right: 10px;
	background: transparent url("images/aoutside.gif") center right no-repeat;

Mark all links that start with 'http://'
a[href^="http"] {
padding-right: 10px;
background-image: url(external.gif);
background-position: right !important;
background-repeat: no-repeat;
<cfdirectory action="LIST" directory="d:\www\docs\" name="dirlist">
<cfloop query="dirlist">
	<cfset outSize = dirlist.size / 1000>
	<a href="docs/" target="_blank"></a> 
	(#NumberFormat(outSize)# kB, 
	#lsdateformat(dirlist.dateLastModified, 'dddd d mmmm yyyy')#)