4

I'm new to the Java language and I've tried to write my first relatively complex program. After I wrote a few classes I've realized that I barely use built-in classes (like BigInteger, MessageDigest, ByteBuffer) directly because they don't totally fit 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 have to use try catch when using SHA1, I can put my custom BigNumber class as parameter and I can also put String as parameter to update function.

The following BigNumber class contains all of the functions what 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'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));
    }   
}

My question is that how common in Java language to use these kind of classes instead of the built-in classes? Does it make my code unreadable for other programmers (compared to implementing everything with built-in classes)?

I've read that new C++ programmers desperately trying to write codes they used to write in C therefore the benefits of C++ remains hidden for them. I'm afraid I do something like that in Java: trying to implement everything on my own instead of using the build-in classes directly. Is this happening (for example in the BigNumber class)?

Thank you for your opinions!

brimborium
  • 9,362
  • 9
  • 48
  • 76
user1131015
  • 67
  • 1
  • 4
  • 2
    You have the "Not Invented Here" syndrome. You need to stop reinventing the wheel and use proved and tested classes. You gain nothing by wrapping a custom bigint class over a BigInteger class - you're better off inheriting (extending) it instead. It's not about readability or familiarity (how many people will get to see your code?) but about **robustness** and **correctness**. People tested those way more than you'll test yours. – Shark Oct 31 '12 at 18:30
  • Also, try/catch is nothing to be ashamed of and it's really not something you want to try and avoid. Sometimes 'throws' can be interesting to just "lemme try it i know i won't get an exception i'm still making it" but try/catch is how it should be done. – Shark Oct 31 '12 at 18:33
  • Also.... this is kind of a question for http://programmers.stackexchange.com/ :D – Shark Oct 31 '12 at 18:34
  • I don't think you fully understood my question... – user1131015 Oct 31 '12 at 18:36
  • Your BigNumber class is OK if it suits your needs and gets the job done. This one you posted looks OK to me as well and I wouldn't mind accepting it into the project trunk :) But I would've told you this exact thing along the way. As long as you remember that someone spent more time on making it optimal and robust should be a motivator to go with the builtin solution rather than rolling out your own ones. However, sometimes you either need to, or it's just damn more convenient to; whatever it is - as long as you're not reinventing the wheel - you're OK. And then there's **documentation** :) – Shark Oct 31 '12 at 18:43

5 Answers5

3

I normally write a utility class which will support me to handle logics. Such as

public class CommonUtil{

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

  // add more utility methods

}
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
someone
  • 6,577
  • 7
  • 37
  • 60
2

Wrapping a class makes sense when you add some value by doing so. If you are adding small functionality it can be worth using a Utility class instead of wrapping an existing one.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

I think that if you do not have a very good reason for implementing the same functionality again you should not probably do it. Here are several reasons:

  • Built-in classes are used by a lot of people around the world and therefore there are less bugs than in your code
  • Users that are experienced in Java will be better in 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 documentations and therefore it is much easier to use them
  • You are wasting your time by implementing something that was 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 is already supporting built-in classes. Let them do their job!
  • The last but not the least. Are you sure that you know more about the problem than an author of a built-in class? Only if the answer is yes, consider writing your own implementation. Even implementation of daily used classes, such as collections or time-related classes can be tricky.
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
  • Can you stop pretending like I'm talking about implementing an already existing class? After the 10th post about how bad is to rewrite an already existing class it disturbs me that nobody read the post above. – user1131015 Oct 31 '12 at 19:28
1

You're not gaining anything by making a class that does this stuff for you. If you're going to be doing certain operations a lot, then you might want to create a new class with static methods that do these important things for you.

Let's assume that you want a sorted array at all times. You could make a new class, let's call it SortedArray. You could sort it whenever you add something in, but why would you do that when you can just add in everything and then call the (utility) method Arrays.sort? For common operations, take a look at Java's Arrays class - if you are doing something often, that's something you make a method for, like searching and sorting. In your case, you might make a utility method that turns the BigInteger into a byte array for you. You shouldn't be just making your own, 'better' version that does what you want it. When other people look at your code, when you use standard objects it's much better, instead of having custom objects that don't really do anything.

rm5248
  • 2,590
  • 3
  • 17
  • 16
0

As @Shark commented, there's no point in creating your own solutions, because:

  1. They take time to create
  2. They become not as flexible

However, you can extend classes (it's recommended) or use 3rd party frameworks that might suit you better.

madman_xxx
  • 104
  • 8
  • 1
    *"you can extend classes (it's recommended)"* - well it depends; in general [you should favour composition over inheritance](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance). – assylias Oct 31 '12 at 18:39
  • "Recommended", as opposed to the given example with BigNumber. – madman_xxx Oct 31 '12 at 18:40