Key Value Hash : Hash Code « Development Class « Java






Key Value Hash

      
/*
Copyright 2007 Creare Inc.

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.
*/

/* KeyValueHash.java
 * Reads Creare type 1 UserData byte array, parses into key/value pairs,
 * and stores them in a hashtable.  The format of the UserData byte array is
 * key = value <,|\n|\r> sequences, where
 *   key is an ascii string, which may not include ,=\n
 *   = is the ascii character 'equals'
 *   value is an ascii string, as above
 * No trailing separater is expected after the last key/value pair
 * For example, the key/value pairs units/PSI and rate/slow would be the
 * byte sequence 'units=PSI,rate=slow'
 *
 * Modification History :
 * 09/15/2006 - JPW
 *  Add new constructor that can take a String argument and an array of
 *  user-specified terminator characters.
 * 08/18/2000 - INB
 *  Stole from RBNBPlot to use with the RBNB to JMF handling code. In
 *  this case, it handles Creare type 2 user data, which looks like type
 *  1, but is used for audio/video streams.
 */
//package com.rbnb.utility;

import java.util.Hashtable;

public class KeyValueHash {
   Hashtable hash = new Hashtable();
   
   public KeyValueHash(byte[] userData) {
      
      // JPW 09/15/2006: These aren't used
      //ByteArrayInputStream byteArray = new ByteArrayInputStream(userData);
      //DataInputStream inStream = new DataInputStream(byteArray);
      
      // JPW 09/15/2006: Add new constructor
      this(new String(userData), null);
   }
   
   public KeyValueHash(String inString) {
       this(inString,null);
   }
   
   // JPW 09/15/2006: Add new constructor that takes a String and an optional
   //                 set of terminating characters
   public KeyValueHash(String inString, char[] terminatorCharsI) {
      
      // The default terminating characters
      char[] terminatorChars = {',','\n','\r'};
      if ( (terminatorCharsI != null) && (terminatorCharsI.length > 0) ) {
    terminatorChars = terminatorCharsI;
      }
      
      int lt = -1; // location of the terminator from the previous iteration
      int nt=nextTerminator(inString,lt,terminatorChars);
      while (nt!=-1) {
   int ne=inString.indexOf('=',lt+1);  // next equals sign
   if (ne>lt+1 && ne<nt-1) { //have key,value isolated
      hash.put(inString.substring(lt+1,ne),inString.substring(ne+1,nt));
      //System.out.println(inString.substring(lt+1,ne)+"  "+inString.substring(ne+1,nt));
      }
   lt=nt;
   while ((nt=nextTerminator(inString,lt,terminatorChars))==lt+1) //ignore multiple adjacent separators
      lt=nt;
   }
   } //end KeyValueHash constructor
   
   
   public String get(String key) {
      // JPW 09/15/2006: Add some checks for null
      if (key == null) {
    return null;
      }
      Object value = hash.get(key);
      if (value == null) {
    return null;
      }
      return (String)value;
   }
   
   public Hashtable getHash() {
      return hash;
   }
   
   private int nextTerminator(String s, int n, char[] t) {
      
      // JPW 09/15/2006: The terminator characters are provided as a parameter
      // char[] t = {',','\n','\r'}; //terminator characters
      
      int min=-1;
      boolean foundOne=false;
      
      if (n>=s.length()) return -1; //at end of string
      
      for (int i=0;i<t.length;i++) {
   int j=s.indexOf(t[i],n+1);
   if (j>=0) {
      if (foundOne) {
         if (j<min) min=j;
         }
      else {
         foundOne=true;
         min=j;
         }
      }
   }
      if (foundOne) return min;
      else return s.length();
   }
   
} //end class KeyValueHash

   
    
    
    
    
    
  








Related examples in the same category

1.Computing hash codes
2.A hash-code generator and a collection of static hash-code generation methods.
3.MD5 hash generator
4.Hash 32 String
5.Hash 64 String
6.MD5 hashing: Encodes a string
7.MD5 String
8.Hash Code BuilderHash Code Builder
9.HashCode generationHashCode generation
10.Get hash code for primitive data types
11.Return as hash code for the given object
12.Null Safe Hash Code
13.A very efficient java hash algorithm, based on the BuzHash algoritm
14.Easy implementation of hashCode
15.An implementation of the HMACT64 keyed hashing algorithm
16.Gets the hash code of an object returning zero when the object is null
17.Unify Hash
18.Secure Hash
19.FNV Hash
20.Jenkins Hash
21.Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution.
22.Concurrent Cuckoo hashing using lock striping.
23.encode Hex
24.Fowler/Noll/Vo hash algorhythm
25.Produces 32-bit hash for hash table lookup. (Jenkins Hash Function)
26.Paul Hsieh's Hash Function.
27.An extension of WeakReference that implements a sane equals and hashcode method.
28.Dual Key Hash Map
29.A hash map with int key and int values.
30.null Safe Equals and Hash
31.Generates a hash code for a given source code.
32.AtkinsonHash utility class implements the hash algorithm used by HyperCard's ask password command.
33.Hash Code AssistHash Code Assist
34.This is a very fast, non-cryptographic hash suitable for general hash-based lookup.
35.An advanced hash table supporting configurable garbage collection semantics of keys and values
36.Hash string