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