001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.util.key;
009
010/**
011 * @author Rusi Popov (popovr@mdatools.net)
012 */
013public class Hash {
014
015  /**
016   * Calculates a fast hash based on chars spread among the whole string
017   * @param string
018   * @return a hash for that string
019   */
020  public static int hash(String string) {
021    int result = 0;
022    int pos = 0;
023    int step;
024    int length;
025    
026    if ( string != null 
027         && (length = string.length() ) > 0 ){ 
028         
029       step = 1 + (length >> 3); 
030       for ( int i = 0; i < 8 && i < length; i++ ) {
031         result = result * 7 + string.charAt( pos );
032         pos = (pos + step) % length;
033       }
034    }
035    return result;
036  }
037}