Java String Reverse getReverseShelfKey(String shelfkey)

Here you can find the source of getReverseShelfKey(String shelfkey)

Description

given a shelfkey (a lexicaly sortable call number), return the reverse shelf key - a sortable version of the call number that will give the reverse order (for getting "previous" call numbers in a list)

License

Apache License

Declaration

public static String getReverseShelfKey(String shelfkey) 

Method Source Code

//package com.java2s;
/**//from   w  ww  .j  av a  2  s.  c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.util.*;

public class Main {
    private static Map<Character, Character> alphanumReverseMap = new HashMap<Character, Character>();
    /** this character will sort first */
    public static char SORT_FIRST_CHAR = Character.MIN_VALUE;
    public static StringBuilder reverseDefault = new StringBuilder(75);

    /**
     * given a shelfkey (a lexicaly sortable call number), return the reverse
     * shelf key - a sortable version of the call number that will give the
     * reverse order (for getting "previous" call numbers in a list)
     */
    public static String getReverseShelfKey(String shelfkey) {
        StringBuilder resultBuf = new StringBuilder(reverseDefault);
        if (shelfkey != null && shelfkey.length() > 0)
            resultBuf.replace(0, shelfkey.length(),
                    reverseAlphanum(shelfkey));
        return resultBuf.toString();
    }

    /**
     * return the reverse String value, mapping A --> 9, B --> 8, ... 9 --> A and
     * also non-alphanum to sort properly (before or after alphanum)
     */
    private static String reverseAlphanum(String orig) {

        /*
         * char[] origArray = orig.toCharArray();
         * 
         * char[] reverse = new char[origArray.length]; for (int i = 0; i <
         * origArray.length; i++) { Character ch = origArray[i]; if (ch != null) {
         * if (Character.isLetterOrDigit(ch)) reverse[i] =
         * alphanumReverseMap.get(ch); else reverse[i] = reverseNonAlphanum(ch); } }
         */
        StringBuilder reverse = new StringBuilder();
        for (int ix = 0; ix < orig.length();) {
            int codePoint = Character.toUpperCase(orig.codePointAt(ix));
            char[] chs = Character.toChars(codePoint);

            if (Character.isLetterOrDigit(codePoint)) {
                if (chs.length == 1) {
                    char c = chs[0];
                    if (alphanumReverseMap.containsKey(c))
                        reverse.append(alphanumReverseMap.get(c));
                    else {
                        // not an ASCII letter or digit
                        // ... view it as after Z in regular alphabet, for now
                        reverse.append(SORT_FIRST_CHAR);
                    }
                } else {
                    // multiple 16 bit character unicode letter
                    // ... view it as after Z in regular alphabet, for now
                    reverse.append(SORT_FIRST_CHAR);
                }
            } else
                // not a letter or a digit
                reverse.append(reverseNonAlphanum(chs[0]));

            ix += chs.length;
        }

        return new String(reverse);
    }

    /**
     * for non alpha numeric characters, return a character that will sort first
     * or last, whichever is the opposite of the original character.
     */
    public static char[] reverseNonAlphanum(char ch) {
        // use punctuation before or after alphanum as appropriate
        switch (ch) {
        case '.':
            return Character.toChars('}');
        case '{':
        case '|':
        case '}':
        case '~':
            // N.B.: these are tough to deal with in a variety of contexts.
            // Hopefully diacritics and non-latin won't bite us in the butt.
            // return Character.toChars(Character.MIN_CODE_POINT);
            return Character.toChars(' ');
        default:
            // return Character.toChars(Character.MAX_CODE_POINT);
            return Character.toChars('~');
        }
    }
}

Related

  1. compareVersion(String va, String vb)
  2. reverse(String aPackage)
  3. reverse(String s)
  4. reverse(String text)
  5. reverse(String word)