001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.trie.analyzer; 018 019import org.apache.commons.collections4.trie.KeyAnalyzer; 020 021/** 022 * An {@link KeyAnalyzer} for {@link String}s. 023 * 024 * @since 4.0 025 * @version $Id: StringKeyAnalyzer.html 972421 2015-11-14 20:00:04Z tn $ 026 */ 027public class StringKeyAnalyzer extends KeyAnalyzer<String> { 028 029 private static final long serialVersionUID = -7032449491269434877L; 030 031 /** A singleton instance of {@link StringKeyAnalyzer}. */ 032 public static final StringKeyAnalyzer INSTANCE = new StringKeyAnalyzer(); 033 034 /** The number of bits per {@link Character}. */ 035 public static final int LENGTH = Character.SIZE; 036 037 /** A bit mask where the first bit is 1 and the others are zero. */ 038 private static final int MSB = 0x8000; 039 040 /** Returns a bit mask where the given bit is set. */ 041 private static int mask(final int bit) { 042 return MSB >>> bit; 043 } 044 045 @Override 046 public int bitsPerElement() { 047 return LENGTH; 048 } 049 050 @Override 051 public int lengthInBits(final String key) { 052 return key != null ? key.length() * LENGTH : 0; 053 } 054 055 @Override 056 public int bitIndex(final String key, final int offsetInBits, final int lengthInBits, 057 final String other, final int otherOffsetInBits, final int otherLengthInBits) { 058 059 boolean allNull = true; 060 061 if (offsetInBits % LENGTH != 0 || otherOffsetInBits % LENGTH != 0 062 || lengthInBits % LENGTH != 0 || otherLengthInBits % LENGTH != 0) { 063 throw new IllegalArgumentException("The offsets and lengths must be at Character boundaries"); 064 } 065 066 final int beginIndex1 = offsetInBits / LENGTH; 067 final int beginIndex2 = otherOffsetInBits / LENGTH; 068 069 final int endIndex1 = beginIndex1 + lengthInBits / LENGTH; 070 final int endIndex2 = beginIndex2 + otherLengthInBits / LENGTH; 071 072 final int length = Math.max(endIndex1, endIndex2); 073 074 // Look at each character, and if they're different 075 // then figure out which bit makes the difference 076 // and return it. 077 char k = 0, f = 0; 078 for(int i = 0; i < length; i++) { 079 final int index1 = beginIndex1 + i; 080 final int index2 = beginIndex2 + i; 081 082 if (index1 >= endIndex1) { 083 k = 0; 084 } else { 085 k = key.charAt(index1); 086 } 087 088 if (other == null || index2 >= endIndex2) { 089 f = 0; 090 } else { 091 f = other.charAt(index2); 092 } 093 094 if (k != f) { 095 final int x = k ^ f; 096 return i * LENGTH + Integer.numberOfLeadingZeros(x) - LENGTH; 097 } 098 099 if (k != 0) { 100 allNull = false; 101 } 102 } 103 104 // All bits are 0 105 if (allNull) { 106 return KeyAnalyzer.NULL_BIT_KEY; 107 } 108 109 // Both keys are equal 110 return KeyAnalyzer.EQUAL_BIT_KEY; 111 } 112 113 @Override 114 public boolean isBitSet(final String key, final int bitIndex, final int lengthInBits) { 115 if (key == null || bitIndex >= lengthInBits) { 116 return false; 117 } 118 119 final int index = bitIndex / LENGTH; 120 final int bit = bitIndex % LENGTH; 121 122 return (key.charAt(index) & mask(bit)) != 0; 123 } 124 125 @Override 126 public boolean isPrefix(final String prefix, final int offsetInBits, 127 final int lengthInBits, final String key) { 128 if (offsetInBits % LENGTH != 0 || lengthInBits % LENGTH != 0) { 129 throw new IllegalArgumentException( 130 "Cannot determine prefix outside of Character boundaries"); 131 } 132 133 final String s1 = prefix.substring(offsetInBits / LENGTH, lengthInBits / LENGTH); 134 return key.startsWith(s1); 135 } 136}