|
| 1 | +package javaLibraries.util.arrays; |
| 2 | + |
| 3 | +// ~~~~~~~~~~ Imports ~~~~~~~~~~ |
| 4 | +import javaLibraries.constants.Alignment; |
| 5 | +import javaLibraries.util.strings.StringFormatter; |
| 6 | + |
| 7 | +import java.security.InvalidParameterException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.Function; |
| 11 | +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 12 | + |
| 13 | +public class ArrayToString |
| 14 | +{ |
| 15 | + |
| 16 | + // ----- Label ----- |
| 17 | + // ~~~~~~~~~~ Constants ~~~~~~~~~~ |
| 18 | + |
| 19 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 20 | + // ~~~~~~~~~~ Variables ~~~~~~~~~~ |
| 21 | + |
| 22 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | + |
| 24 | + private ArrayToString() {} |
| 25 | + |
| 26 | + public static <T> ArrayBuilder1D<T> create1D(final T[] array) |
| 27 | + { |
| 28 | + return new ArrayBuilder1D<>(array); |
| 29 | + } |
| 30 | + public static <T> ArrayBuilder2D<T> create2D(final T[][] array) |
| 31 | + { |
| 32 | + return new ArrayBuilder2D<>(array); |
| 33 | + } |
| 34 | + |
| 35 | + public static class ArrayBuilder1D<T> |
| 36 | + { |
| 37 | + |
| 38 | + public enum Style |
| 39 | + { |
| 40 | + |
| 41 | + BLANK(' ', ' ', "", ""), |
| 42 | + TOP('-', ' ', "", ""), |
| 43 | + SIDES(' ', ' ', "[", "]"), |
| 44 | + SURROUNDED('-', '-', "[", "]"); |
| 45 | + |
| 46 | + private char topBorder; |
| 47 | + private char bottomBorder; |
| 48 | + private String leftBorder; |
| 49 | + private String rightBorder; |
| 50 | + Style(final char topBorder, final char bottomBorder, final String leftBorder, final String rightBorder) |
| 51 | + { |
| 52 | + this.topBorder = topBorder; |
| 53 | + this.bottomBorder = bottomBorder; |
| 54 | + this.leftBorder = leftBorder; |
| 55 | + this.rightBorder = rightBorder; |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + private T[] array; |
| 61 | + |
| 62 | + private Alignment alignment; |
| 63 | + private Function<T, String> function; |
| 64 | + |
| 65 | + private char topBorder; |
| 66 | + private char bottomBorder; |
| 67 | + private String leftBorder; |
| 68 | + private String rightBorder; |
| 69 | + |
| 70 | + private ArrayBuilder1D(final T[] array) |
| 71 | + { |
| 72 | + if (array == null) |
| 73 | + { |
| 74 | + throw new InvalidParameterException("array can not be null"); |
| 75 | + } |
| 76 | + this.array = array; |
| 77 | + |
| 78 | + alignment = Alignment.LEFT; |
| 79 | + function = t -> t.toString(); |
| 80 | + topBorder = ' '; |
| 81 | + bottomBorder = ' '; |
| 82 | + leftBorder = ""; |
| 83 | + rightBorder = ""; |
| 84 | + } |
| 85 | + |
| 86 | + public ArrayBuilder1D<T> setAlignment(final Alignment alignment) |
| 87 | + { |
| 88 | + this.alignment = alignment; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public ArrayBuilder1D<T> setBorders(final Style style) |
| 93 | + { |
| 94 | + setTopBorder(style.topBorder); |
| 95 | + setBottomBorder(style.bottomBorder); |
| 96 | + setLeftBorder(style.leftBorder); |
| 97 | + setRightBorder(style.rightBorder); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public ArrayBuilder1D<T> setTopBorder(final char topBorder) |
| 102 | + { |
| 103 | + this.topBorder = topBorder; |
| 104 | + return this; |
| 105 | + } |
| 106 | + public ArrayBuilder1D<T> setBottomBorder(final char bottomBorder) |
| 107 | + { |
| 108 | + this.bottomBorder = bottomBorder; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + public ArrayBuilder1D<T> setLeftBorder(final String leftBorder) |
| 113 | + { |
| 114 | + if (leftBorder == null) |
| 115 | + { |
| 116 | + throw new InvalidParameterException("leftBorder can not be null"); |
| 117 | + } |
| 118 | + this.leftBorder = leftBorder; |
| 119 | + return this; |
| 120 | + } |
| 121 | + public ArrayBuilder1D<T> setRightBorder(final String rightBorder) |
| 122 | + { |
| 123 | + if (rightBorder == null) |
| 124 | + { |
| 125 | + throw new InvalidParameterException("rightBorder can not be null"); |
| 126 | + } |
| 127 | + this.rightBorder = rightBorder; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public ArrayBuilder1D<T> setFunction(final Function<T, String> function) |
| 132 | + { |
| 133 | + if (function == null) |
| 134 | + { |
| 135 | + throw new InvalidParameterException("function can not be null"); |
| 136 | + } |
| 137 | + this.function = function; |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + public String build() |
| 142 | + { |
| 143 | + int maxLength = 0; |
| 144 | + List<String> outputs = new ArrayList<>(); |
| 145 | + |
| 146 | + String temp; |
| 147 | + for (T t : array) |
| 148 | + { |
| 149 | + temp = (t == null ? "null" : function.apply(t)); |
| 150 | + maxLength = Math.max(temp.length(), maxLength); |
| 151 | + outputs.add(temp); |
| 152 | + } |
| 153 | + |
| 154 | + StringBuilder string = new StringBuilder(); |
| 155 | + |
| 156 | + if (topBorder != ' ') |
| 157 | + { |
| 158 | + for (int i = 0; i < leftBorder.length() + maxLength + rightBorder.length(); i++) |
| 159 | + { |
| 160 | + string.append(topBorder); |
| 161 | + } |
| 162 | + string.append('\n'); |
| 163 | + } |
| 164 | + |
| 165 | + for (int i = 0; i < outputs.size(); i++) |
| 166 | + { |
| 167 | + string.append(leftBorder); |
| 168 | + string.append(StringFormatter.create(outputs.get(i), maxLength).setAlignment(alignment).build()); |
| 169 | + string.append(rightBorder); |
| 170 | + if (i + 1 < outputs.size()) |
| 171 | + { |
| 172 | + string.append('\n'); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (bottomBorder != ' ') |
| 177 | + { |
| 178 | + string.append('\n'); |
| 179 | + for (int i = 0; i < leftBorder.length() + maxLength + rightBorder.length(); i++) |
| 180 | + { |
| 181 | + string.append(bottomBorder); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return string.toString(); |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + public static class ArrayBuilder2D<T> |
| 191 | + { |
| 192 | + |
| 193 | + private T[][] array; |
| 194 | + |
| 195 | + private ArrayBuilder2D(final T[][] array) |
| 196 | + { |
| 197 | + this.array = array; |
| 198 | + } |
| 199 | + |
| 200 | + public String build() |
| 201 | + { |
| 202 | + throw new UnsupportedOperationException(); |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments