Java, how common is the extension / packaging of built-in classes

I am new to Java, and I tried to write my first relatively complex program. After I wrote several classes, I realized that I barely use the built-in classes (e.g. BigInteger, MessageDigest, ByteBuffer) directly, because they do not fully meet my needs. Instead, I write my own class and inside the class I use the built-in class as an attribute. Example:

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(); } } 

With the following simple class, I don't need to use try catch when using SHA1, I can put my own BigNumber class as a parameter, and I can also put a String parameter as a parameter for updating.

The next BigNumber class contains all the functions that I need and exactly how I need them.

 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 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)); } } 

My question is, how common are the Java languages ​​to use these classes instead of the built-in classes? Does this make my code unreadable to other programmers (compared to implementing everything with built-in classes)?

I read that new C ++ programmers are desperate to write the codes they used to write in C, so the benefits of C ++ remain hidden to them. I'm afraid I'm doing something similar in Java: I'm trying to implement everything myself, and not directly use the built-in classes. Does this happen (e.g. in the BigNumber class)?

Thank you for your opinion!

+4
source share
5 answers

Wrapping a class makes sense when you add some value by doing this. If you add a little functionality, you should use the Utility class instead of wrapping an existing one.

+2
source

Usually I write a utility class that will support me for processing logic. For instance,

 public class CommonUtil{ public byte[] asByteArray(int minSize) { return "something".getBytes(); } // add more utility methods } 
+3
source

I think that if you have no good reason to re-implement the same functionality, you probably shouldn't. Here are a few reasons:

  • Built-in classes are used by many people around the world, and therefore there are fewer errors than in your code.
  • Users with Java experience will be better off using standard classes than your classes, and they will need less time to understand your code and write something new in your project.
  • Built-in classes have good documentation, so they are much easier to use.
  • You are wasting time implementing something that has been implemented and tested by Java professionals. It is better to concentrate on your own project.
  • If you are writing a long-term project, you will need to support all your classes. Oracle already supports built-in classes. Let them do their job!
  • Last but not least. Are you sure you know more about the problem than the author of the built-in class? Only if the answer is yes, consider writing your own implementation. Even implementing daily used classes, such as collections or time-related classes, can be complicated.
+1
source

You are not gaining anything by creating a class that does this for you. If you are going to do certain operations a lot, then you may need to create a new class with static methods that will do these important things for you.

Suppose you want a sorted array at all times. You can create a new class, name it SortedArray . You can sort it whenever you add something, but why would you do it when you can just add to everything and then call the Arrays.sort method (utilities)? For general operations, take a look at the Java Arrays class - if you often do something, then something you do for the method, for example, for searching and sorting. In your case, you can make a utility method that turns BigInteger into an array of bytes for you. You should not just create your own β€œbest” version that does what you want. When other people look at your code, when you use standard objects, it is much better, instead of custom objects that actually do nothing .

+1
source

As @Shark noted, it makes no sense to create your own solutions, because:

  • They need time to create
  • They do not become so flexible.

However, you can extend the classes (recommended) or use third-party frameworks that may be better for you.

0
source

Source: https://habr.com/ru/post/1443297/


All Articles