0

Are there any existing utilities like Apache Commons StringUtils that make it easy to increment an integer, but output it as a zero padded string?

I can certainly write my own utilizing something like String.format("%05d", counter), but I'm wondering if there is a library that has this already available.

I'm envisioning something I can use like this:

// Create int counter with value of 0 padded to 4 digits
PaddedInt counter = new PaddedInt(0,4);

counter.incr();

// Print "0001"
System.out.println(counter); 

// Print "0002"
System.out.println(counter.incr());

String text = "The counter is now "+counter.decr();

// Print "The counter is now 0001"
System.out.println(text);
Tauren
  • 26,795
  • 42
  • 131
  • 167

3 Answers3

1

I doubt you'll find anything to do this, because padding and incrementing are two basic operations that are unrelated, and trivial to implement. You could have implemented such a class three times in the time you took to write your question. It all boils down to wrapping an int into an object and implementing toString using String.format.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I figured as much. In fact, I implemented a solution only minutes after posting my question. Just thought there might be some library out there that already did something like this. I'm adding my code as another answer below if you want to check it out. – Tauren Nov 19 '11 at 08:52
1

In case anyone is interested, I threw together this a few minutes after posting my question:

import org.apache.commons.lang.StringUtils;

public class Counter {

    private int value;
    private int padding;

    public Counter() {
        this(0, 4);
    }

    public Counter(int value) {
        this(value, 4);
    }

    public Counter(int value, int padding) {
        this.value = value;
        this.padding = padding;
    }

    public Counter incr() {
        this.value++;
        return this;
    }

    public Counter decr() {
        this.value--;
        return this;
    }

    @Override
    public String toString() {
        return StringUtils.leftPad(Integer.toString(this.value), 
                this.padding, "0");
        // OR without StringUtils:
        // return String.format("%0"+this.padding+"d", this.value);
    }
}

The only problem with this is that I must call toString() to get a string out of it, or append it to a string like ""+counter:

@Test
public void testCounter() {
    Counter counter = new Counter();
    assertThat("0000", is(counter.toString()));
    counter.incr();
    assertThat("0001",is(""+counter));
    assertThat("0002",is(counter.incr().toString()));
    assertThat("0001",is(""+counter.decr()));
    assertThat("001",is(not(counter.toString())));
}
Tauren
  • 26,795
  • 42
  • 131
  • 167
0

To be honest, I think you are mixing different concerns. An integer is an integer with all the operations and if you want to output it padded with zeros that is different thing.

You might want to have a look at StringUtils.leftPad as an alternative of String.format.

jeha
  • 10,562
  • 5
  • 50
  • 69