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 */
026public class StringKeyAnalyzer extends KeyAnalyzer<String> {
027
028    private static final long serialVersionUID = -7032449491269434877L;
029
030    /** A singleton instance of {@link StringKeyAnalyzer}. */
031    public static final StringKeyAnalyzer INSTANCE = new StringKeyAnalyzer();
032
033    /** The number of bits per {@link Character}. */
034    public static final int LENGTH = Character.SIZE;
035
036    /** A bit mask where the first bit is 1 and the others are zero. */
037    private static final int MSB = 0x8000;
038
039    /** Returns a bit mask where the given bit is set. */
040    private static int mask(final int bit) {
041        return MSB >>> bit;
042    }
043
044    @Override
045    public int bitIndex(final String key, final int offsetInBits, final int lengthInBits,
046                        final String other, final int otherOffsetInBits, final int otherLengthInBits) {
047
048        boolean allNull = true;
049
050        if (offsetInBits % LENGTH != 0 || otherOffsetInBits % LENGTH != 0
051                || lengthInBits % LENGTH != 0 || otherLengthInBits % LENGTH != 0) {
052            throw new IllegalArgumentException("The offsets and lengths must be at Character boundaries");
053        }
054
055        final int beginIndex1 = offsetInBits / LENGTH;
056        final int beginIndex2 = otherOffsetInBits / LENGTH;
057
058        final int endIndex1 = beginIndex1 + lengthInBits / LENGTH;
059        final int endIndex2 = beginIndex2 + otherLengthInBits / LENGTH;
060
061        final int length = Math.max(endIndex1, endIndex2);
062
063        // Look at each character, and if they're different
064        // then figure out which bit makes the difference
065        // and return it.
066        char k = 0, f = 0;
067        for (int i = 0; i < length; i++) {
068            final int index1 = beginIndex1 + i;
069            final int index2 = beginIndex2 + i;
070
071            if (index1 >= endIndex1) {
072                k = 0;
073            } else {
074                k = key.charAt(index1);
075            }
076
077            if (other == null || index2 >= endIndex2) {
078                f = 0;
079            } else {
080                f = other.charAt(index2);
081            }
082
083            if (k != f) {
084                final int x = k ^ f;
085                return i * LENGTH + Integer.numberOfLeadingZeros(x) - LENGTH;
086            }
087
088            if (k != 0) {
089                allNull = false;
090            }
091        }
092
093        // All bits are 0
094        if (allNull) {
095            return NULL_BIT_KEY;
096        }
097
098        // Both keys are equal
099        return EQUAL_BIT_KEY;
100    }
101
102    @Override
103    public int bitsPerElement() {
104        return LENGTH;
105    }
106
107    @Override
108    public boolean isBitSet(final String key, final int bitIndex, final int lengthInBits) {
109        if (key == null || bitIndex >= lengthInBits) {
110            return false;
111        }
112
113        final int index = bitIndex / LENGTH;
114        final int bit = bitIndex % LENGTH;
115
116        return (key.charAt(index) & mask(bit)) != 0;
117    }
118
119    @Override
120    public boolean isPrefix(final String prefix, final int offsetInBits,
121                            final int lengthInBits, final String key) {
122        if (offsetInBits % LENGTH != 0 || lengthInBits % LENGTH != 0) {
123            throw new IllegalArgumentException(
124                    "Cannot determine prefix outside of Character boundaries");
125        }
126
127        final String s1 = prefix.substring(offsetInBits / LENGTH, lengthInBits / LENGTH);
128        return key.startsWith(s1);
129    }
130
131    @Override
132    public int lengthInBits(final String key) {
133        return key != null ? key.length() * LENGTH : 0;
134    }
135}