org.ttzero.excel.entity.e3.Option Maven / Gradle / Ivy
/*
* Copyright (c) 2019-2020, [email protected] All Rights Reserved.
*
* Licensed 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.
*/
package org.ttzero.excel.entity.e3;
/**
* @author guanquan.wang at 2019-01-29 17:39
*/
public class Option {
public static final int OFF = 0, ON = 1;
/**
* 16-bit characters
*/
public static final Option UTF16 = Option.of(1);
/**
* 8-bit characters
*/
public static final Option ASCII = Option.of(0);
private final int value;
public static Option of(int value) {
return new Option(value);
}
private Option(int value) {
this.value = value;
}
/**
* The bit value
*
* @param index the value index
* @return bit value
*/
public int get(int index) {
rangeCheck(index);
return value >>> index & 1;
}
/**
* The index value is equals one.
*
* @param index the value index
* @return true if the bit value equals one
*/
public boolean isOn(int index) {
return get(index) == ON;
}
/**
* The index value is equals zero.
*
* @param index the value index
* @return true if the bit value equals zero
*/
public boolean isOff(int index) {
return get(index) == OFF;
}
/**
* The range value
*
* @param from bit index
* @param size size of bit
* @return the range value
*/
public int range(int from, int size) {
rangeCheck(from);
int to = from + size - 1;
rangeCheck(to);
return (value >> from) & ~(~0 << size);
}
/**
* Check range
*
* @param index the bit index
*/
private void rangeCheck(int index) {
if (index > 31) {
throw new RuntimeException("Index out of bound, index " + index + ", size " + 31);
}
}
@Override
public String toString() {
return Integer.toBinaryString(value);
}
}