Java 扩展/包装内置类有多普遍

标签 java class coding-style built-in

我是 Java 语言的新手,我尝试编写我的第一个相对复杂的程序。在我写了几个类之​​后,我意识到我几乎不直接使用内置类(比如 BigInteger、MessageDigest、ByteBuffer),因为它们不能完全满足我的需要。相反,我编写了自己的类,并在类内部使用内置类作为属性。 示例:

public class SHA1 {
    public static final int SHA_DIGEST_LENGTH = 20;

    private MessageDigest md;

    public SHA1() {
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public void update(byte[] data) {
        md.update(data);
    }

    public void update(BigNumber bn) {
        md.update(bn.asByteArray());
    }

    public void update(String data) {
        md.update(data.getBytes());
    }

    public byte[] digest() {
        return md.digest();
    }
}

通过以下简单类,我在使用 SHA1 时不必使用 try catch,我可以将自定义 BigNumber 类作为参数,我也可以将 String 作为参数来更新函数。

以下 BigNumber 类包含我需要的所有功能以及我需要它们的方式。

public class BigNumber {
    private BigInteger m_bn;

    public BigNumber() {
        m_bn = new BigInteger("0");
    }

    public BigNumber(BigInteger bn) {
        m_bn = bn;
    }

    public BigNumber(String hex) {
        setHexStr(hex);
    }

    //reversed no minsize
    public byte[] asByteArray() {
        return asByteArray(0, true);
    }

    //reversed with minsize
    public byte[] asByteArray(int minSize) {
        return asByteArray(minSize, true);
    }

    public byte[] asByteArray(int minSize, boolean rev) {
        byte[] mag = m_bn.toByteArray();

        //delete sign bit
        //there is always a sign bit! so if bitNum % 8 is zero then
        //the sign bit created a new byte (0th)
        if(getNumBits() % 8 == 0) {
            byte[] tmp = new byte[mag.length-1];
            System.arraycopy(mag, 1, tmp, 0, mag.length-1);
            mag = tmp;
        }

        //extend the byte array if needed
        int byteSize = (minSize >= getNumBytes()) ? minSize : getNumBytes();
        byte[] tmp = new byte[byteSize]; 

        //if tmp's length smaller then byteSize then we keep 0x00-s from left
        System.arraycopy(mag, 0, tmp, byteSize-mag.length, mag.length);

        if(rev) ByteManip.reverse(tmp);

        return tmp;
    }

    public String asHexStr() {
        return ByteManip.byteArrayToHexStr(asByteArray(0, false));
    }

    public void setHexStr(String hex) {
        m_bn = new BigInteger(hex, 16);
    }

    public void setBinary(byte[] data) {
        //reverse = true
        ByteManip.reverse(data);
        //set as hex (binary set has some bug with the sign bit...)
        m_bn = new BigInteger(ByteManip.byteArrayToHexStr(data), 16);
    }

    public void setRand(int byteSize) {
        byte[] tmp = new byte[byteSize];
        new Random().nextBytes(tmp);
        //reversing byte order, but it doesn't really matter since it is a random
        //number
        setBinary(tmp);
    }

    public int getNumBytes() {
        return (m_bn.bitLength() % 8 == 0) ? (m_bn.bitLength() / 8) : (m_bn.bitLength() / 8 + 1);
    }

    public int getNumBits() {
        return m_bn.bitLength();
    }

    public boolean isZero() {
        return m_bn.equals(BigInteger.ZERO);
    }

    //operations
    public BigNumber modExp(BigNumber exp, BigNumber mod) {
        return new BigNumber(m_bn.modPow(exp.m_bn, mod.m_bn));
    }

    public BigNumber mod(BigNumber m) {
        return new BigNumber(m_bn.mod(m.m_bn));
    }

    public BigNumber add(BigNumber bn) {
        return new BigNumber(m_bn.add(bn.m_bn));
    }

    public BigNumber subtract(BigNumber bn) {
        return new BigNumber(m_bn.subtract(bn.m_bn));
    }

    public BigNumber multiply(BigNumber bn) {
        return new BigNumber(m_bn.multiply(bn.m_bn));
    }   
}

我的问题是,在 Java 语言中使用这些类而不是内置类有多普遍?它是否会使其他程序员无法阅读我的代码(与使用内置类实现所有内容相比)?

我读到新的 C++ 程序员拼命尝试编写他们过去用 C 编写的代码,因此 C++ 的好处对他们来说仍然是隐藏的。 恐怕我在 Java 中做了类似的事情:尝试自己实现所有内容,而不是直接使用内置类。 这会发生吗(例如在 BigNumber 类中)?

感谢您的意见!

最佳答案

我通常会编写一个实用程序类来支持我处理逻辑。比如

public class CommonUtil{

  public byte[] asByteArray(int minSize)
  {
    return "something".getBytes();
  }

  // add more utility methods

}

关于Java 扩展/包装内置类有多普遍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165353/

相关文章:

python - Python中如何动态加载类并调用方法?

algorithm - 如果在 IF 语句中有 return 或 throw 语句,可以不使用 ELSE 语句吗?

java - XML 转换为 JSON 时前导零被截断

java - 如何等待 FileChooser 选择?

java - modelAndView返回后执行任务

java - 将 String 转换为另一个类中的数组名称

java - 如何在方面构造函数中获取切入点详细信息

python - 在线程中运行类方法(python)

java - Google Java 样式 : Import names v. s。导入语句?

coding-style - if、while 等之后(以及父级之前)的空格