001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016
017 package lapisx.util;
018
019 /**
020 * String which compares case-insensitively with other strings.
021 * Especially useful as a case-insensitive hashtable key.
022 */
023 public class CaselessString {
024 String string;
025
026 public CaselessString (String string) {
027 this.string = string;
028 }
029
030 public boolean equals (Object obj) {
031 if (obj instanceof String
032 || obj instanceof CaselessString)
033 return string.equalsIgnoreCase (obj.toString ());
034 else
035 return false;
036 }
037
038 public int hashCode() {
039 int hash = 0;
040 int len = string.length ();
041
042 if (len < 16) {
043 // use all characters
044 for (int i = 0; i < len; ++i)
045 hash = (hash * 37) + Character.toUpperCase (string.charAt (i));
046 } else {
047 int skip = len / 8; // sample every 8th char
048 for (int i = 0; i < len; i += skip)
049 hash = (hash * 39) + Character.toUpperCase (string.charAt (i));
050 }
051
052 return hash;
053 }
054
055 public String toString () {
056 return string;
057 }
058 }