All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.core.structure.CharIndex Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.structure;

import shz.core.constant.ArrayConstant;

import java.io.*;
import java.util.Arrays;

public final class CharIndex implements Serializable {
    private static final long serialVersionUID = -7410641479999715138L;
    public final char[] chars;
    private transient int len;
    private transient int[] indexes;
    private transient int offset;

    private CharIndex(char[] chars) {
        this.chars = chars;
        init();
    }

    private void init() {
        int min = 224, max = -1;
        for (char c : chars) {
            int idx = c - ' ';
            if (idx < 0 || idx > 223) throw new UnsupportedOperationException();
            if (min > idx) min = idx;
            if (max < idx) max = idx;
        }
        len = max - min + 1;
        indexes = new int[len];
        offset = min;
        for (int i = 0; i < chars.length; ++i) indexes[chars[i] - ' ' - min] = i;
    }

    public static final CharIndex C2 = new CharIndex(ArrayConstant.CHAR_ARRAY_2);
    public static final CharIndex C8 = new CharIndex(ArrayConstant.CHAR_ARRAY_8);
    public static final CharIndex C10 = new CharIndex(ArrayConstant.CHAR_ARRAY_10);
    public static final CharIndex C16 = new CharIndex(ArrayConstant.CHAR_ARRAY_16);
    public static final CharIndex C94 = new CharIndex(ArrayConstant.CHAR_ARRAY_94);

    public static final CharIndex UPPERCASE = new CharIndex(ArrayConstant.CHAR_UPPERCASE);
    public static final CharIndex LOWERCASE = new CharIndex(ArrayConstant.CHAR_LOWERCASE);
    public static final CharIndex SYMBOL = new CharIndex(ArrayConstant.CHAR_SYMBOL);

    public static final CharIndex DIGIT_UPPERCASE = new CharIndex(ArrayConstant.CHAR_DIGIT_UPPERCASE);
    public static final CharIndex DIGIT_LOWERCASE = new CharIndex(ArrayConstant.CHAR_DIGIT_LOWERCASE);
    public static final CharIndex DIGIT_ALPHABET = new CharIndex(ArrayConstant.CHAR_DIGIT_ALPHABET);
    public static final CharIndex IP_V4 = new CharIndex(ArrayConstant.CHAR_IP_V4);
    public static final CharIndex IP_V6 = new CharIndex(ArrayConstant.CHAR_IP_V6);
    public static final CharIndex URL = new CharIndex(ArrayConstant.CHAR_URL);

    /**
     * 只支持指定的ASCII码
     */
    public static CharIndex of(char[] chars) {
        if (chars == null || chars.length == 0) throw new NullPointerException();

        if (Arrays.equals(chars, ArrayConstant.CHAR_ARRAY_2)) return C2;
        if (Arrays.equals(chars, ArrayConstant.CHAR_ARRAY_8)) return C8;
        if (Arrays.equals(chars, ArrayConstant.CHAR_ARRAY_10)) return C10;
        if (Arrays.equals(chars, ArrayConstant.CHAR_ARRAY_16)) return C16;
        if (Arrays.equals(chars, ArrayConstant.CHAR_ARRAY_94)) return C94;

        if (Arrays.equals(chars, ArrayConstant.CHAR_UPPERCASE)) return UPPERCASE;
        if (Arrays.equals(chars, ArrayConstant.CHAR_LOWERCASE)) return LOWERCASE;
        if (Arrays.equals(chars, ArrayConstant.CHAR_SYMBOL)) return SYMBOL;

        if (Arrays.equals(chars, ArrayConstant.CHAR_DIGIT_UPPERCASE)) return DIGIT_UPPERCASE;
        if (Arrays.equals(chars, ArrayConstant.CHAR_DIGIT_LOWERCASE)) return DIGIT_LOWERCASE;
        if (Arrays.equals(chars, ArrayConstant.CHAR_DIGIT_ALPHABET)) return DIGIT_ALPHABET;
        if (Arrays.equals(chars, ArrayConstant.CHAR_IP_V4)) return IP_V4;
        if (Arrays.equals(chars, ArrayConstant.CHAR_IP_V6)) return IP_V6;
        if (Arrays.equals(chars, ArrayConstant.CHAR_URL)) return URL;

        return new CharIndex(chars);
    }

    public int idx(char c) {
        int idx = c - ' ' - offset;
        return idx < 0 || idx >= len ? -1 : indexes[idx];
    }

    public boolean contains(char c) {
        int idx = c - ' ' - offset;
        return idx >= 0 && idx < len;
    }

    public boolean containsAll(char[] chars) {
        for (char c : chars) if (!contains(c)) return false;
        return true;
    }

    public boolean nonContainsAll(char[] chars) {
        return !containsAll(chars);
    }

    public boolean containsAny(char[] chars) {
        for (char c : chars) if (contains(c)) return true;
        return false;
    }

    public boolean nonContainsAny(char[] chars) {
        return !containsAny(chars);
    }

    public static int idx(char c, char[] chars) {
        int len = chars.length;
        for (int i = 0; i < len; ++i) if (chars[i] == c) return i;
        return -1;
    }

    public static boolean contains(char c, char[] chars) {
        return idx(c, chars) != -1;
    }

    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        ois.defaultReadObject();
        init();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy