Last active
August 29, 2015 14:06
-
-
Save chandu-io/dec61fd62268589e1909 to your computer and use it in GitHub Desktop.
java :: Java Spacer Enum :: for outputting fixed length string with paddings of spaces or zeroes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Output from SpacerTest.java | |
| ---------------------------- | |
| [Something ] | |
| [ Something] | |
| [000000000000123] | |
| [123000000000000] | |
| [So] | |
| [So] | |
| [12] | |
| [12] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum Spacer { | |
| ONE(1), | |
| TWO(2), | |
| NINE(9), | |
| FIFTEEN(15), | |
| // Add here - whatever number you like to use | |
| TWENTY(20); | |
| // FIELDS | |
| private final int size; | |
| private final String fmtrLt; | |
| private final String fmtrRt; | |
| private final String strTxt; | |
| private final String intTxt; | |
| // CONSTANTS | |
| private static final char SPACE = ' '; | |
| private static final char ZERO = '0'; | |
| private static final char DOT = '.'; | |
| private static final char DASH = '-'; | |
| private static final char STR_CONVERTER = 's'; | |
| private static final String ARG_INDEX = "%1$"; | |
| private static final String EMPTY = ""; | |
| // CONSTRUCTOR | |
| Spacer(final int n) { | |
| size = n; | |
| fmtrLt = ARG_INDEX + n + DOT + n + STR_CONVERTER; | |
| fmtrRt = ARG_INDEX + DASH + n + DOT + n + STR_CONVERTER; | |
| strTxt = String.format(fmtrLt, EMPTY); | |
| intTxt = String.format(fmtrRt, EMPTY).replace(SPACE, ZERO); | |
| } | |
| public String getStrTxt() { | |
| return strTxt; | |
| } | |
| public String getStrTxt(final String s) { | |
| return getStrTxt(s, false); | |
| } | |
| public String getStrTxt(final String s, final boolean ltPadding) { | |
| return ltPadding ? String.format(fmtrLt, s) : String.format(fmtrRt, s); | |
| } | |
| public String getIntTxt() { | |
| return intTxt; | |
| } | |
| public String getIntTxt(final int i) { | |
| return getIntTxt(i, false); | |
| } | |
| public String getIntTxt(final int i, final boolean rtPadding) { | |
| return (rtPadding ? String.format(fmtrRt, i) : String.format(fmtrLt, i)).replace(SPACE, ZERO); | |
| } | |
| public static Spacer find(final int n) { | |
| Spacer returnSpacer = null; | |
| for (Spacer spacer : values()) { | |
| if (spacer.size == n) { | |
| returnSpacer = spacer; | |
| break; | |
| } | |
| } | |
| return returnSpacer; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SpacerTest { | |
| public static void main(String[] args) { | |
| final String s = "Something"; | |
| final int n = 123; | |
| logn("[%s]", Spacer.FIFTEEN.getStrTxt(s)); | |
| logn("[%s]", Spacer.FIFTEEN.getStrTxt(s, true)); | |
| logn("[%s]", Spacer.FIFTEEN.getIntTxt(n)); | |
| logn("[%s]", Spacer.FIFTEEN.getIntTxt(n, true)); | |
| logn("[%s]", Spacer.TWO.getStrTxt(s)); | |
| logn("[%s]", Spacer.TWO.getStrTxt(s, true)); | |
| logn("[%s]", Spacer.TWO.getIntTxt(n)); | |
| logn("[%s]", Spacer.TWO.getIntTxt(n, true)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment