| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.io.ObjectStreamException; |
| 36 | | | import java.io.Serializable; |
| 37 | | | import java.nio.CharBuffer; |
| 38 | | | import java.text.FieldPosition; |
| 39 | | | import java.text.Format; |
| 40 | | | import java.text.ParsePosition; |
| 41 | | | import java.util.Arrays; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | (1) | public class FixLengthFormat |
| 53 | | | extends Format |
| 54 | | | { |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | 100 | | public static final Padding LEFT_PADDING = new Padding(); |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | 100 | | public static final Padding RIGHT_PADDING = new Padding(); |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | 100 | | public static final Padding LEFT_CUT_RIGHT_PADDING = new Padding(); |
| 72 | | | |
| 73 | 100 | | private static final Padding[] PADDINGS = {LEFT_PADDING, RIGHT_PADDING, |
| 74 | | | LEFT_CUT_RIGHT_PADDING}; |
| 75 | | | |
| 76 | | | private static final long serialVersionUID = 3256720688978278194L; |
| 77 | | | |
| 78 | | | private static final char DEFAULT_PADDING_CHAR = ' '; |
| 79 | | | |
| 80 | | | private final char[] mAllPadding; |
| 81 | | | |
| 82 | | | private final Padding mType; |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | private static final class Padding |
| 89 | | | implements Comparable, Serializable |
| 90 | | | { |
| 91 | | | private static final long serialVersionUID = 3258410642577831730L; |
| 92 | | | |
| 93 | 100 | | private static int sOrdinal = 0; |
| 94 | | | private final int mOrdinal; |
| 95 | | | |
| 96 | | | private Padding () |
| 97 | 100 | | { |
| 98 | 100 | | mOrdinal = sOrdinal++; |
| 99 | 100 | | } |
| 100 | | | |
| 101 | | | private Object readResolve () |
| 102 | | | throws ObjectStreamException |
| 103 | | | { |
| 104 | 0 | | return PADDINGS[mOrdinal]; |
| 105 | | | } |
| 106 | | | |
| 107 | | | {@inheritDoc} |
| 108 | | | public int compareTo (Object o) |
| 109 | | | { |
| 110 | 0 | (2) | return mOrdinal - ((Padding) o).mOrdinal; |
| 111 | | | } |
| 112 | | | } |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | @param |
| 119 | | | |
| 120 | | | @param {@linkplain } |
| 121 | | | {@linkplain } |
| 122 | | | |
| 123 | | | @throws <code></code> |
| 124 | | | |
| 125 | | | public FixLengthFormat (final int fixedLength, final Padding type) |
| 126 | | | { |
| 127 | 100 | | this(fixedLength, type, DEFAULT_PADDING_CHAR); |
| 128 | 100 | | } |
| 129 | | | |
| 130 | | | |
| 131 | | | <code></code> |
| 132 | | | |
| 133 | | | |
| 134 | | | @param |
| 135 | | | <code></code> |
| 136 | | | @param {@linkplain } |
| 137 | | | {@linkplain } |
| 138 | | | @param |
| 139 | | | |
| 140 | | | @throws <code></code> |
| 141 | | | |
| 142 | | | |
| 143 | | | public FixLengthFormat ( |
| 144 | | | final int fixedLength, |
| 145 | | | final Padding type, |
| 146 | | | final char paddingChar) |
| 147 | | | { |
| 148 | 100 | | super(); |
| 149 | 100 | | if (fixedLength <= 0) |
| 150 | | | { |
| 151 | 0 | | throw new IllegalArgumentException("Fixed length must be >= 0, but is " |
| 152 | | | + fixedLength); |
| 153 | | | } |
| 154 | | | |
| 155 | 100 | | mType = type; |
| 156 | 100 | | mAllPadding = new char[fixedLength]; |
| 157 | 100 | | Arrays.fill(mAllPadding, paddingChar); |
| 158 | 100 | | } |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | <code></code> |
| 163 | | | |
| 164 | | | |
| 165 | | | |
| 166 | | | <code></code> |
| 167 | | | |
| 168 | | | |
| 169 | | | @see |
| 170 | | | |
| 171 | | | @param |
| 172 | | | @param |
| 173 | | | <code></code> |
| 174 | | | |
| 175 | | | @return<code></code> |
| 176 | | | |
| 177 | | | |
| 178 | | | public Object parseObject (String source, ParsePosition pos) |
| 179 | | | { |
| 180 | 100 | | String rc = null; |
| 181 | | | |
| 182 | 100 | | if (pos.getIndex() + mAllPadding.length > source.length()) |
| 183 | | | { |
| 184 | 0 | | pos.setErrorIndex(pos.getIndex()); |
| 185 | | | } |
| 186 | | | else |
| 187 | | | { |
| 188 | 100 | | final CharBuffer paddedString = CharBuffer.wrap(source, |
| 189 | | | pos.getIndex(), pos.getIndex() + mAllPadding.length); |
| 190 | | | |
| 191 | 100 | | if (paddedString.length() != mAllPadding.length) |
| 192 | | | { |
| 193 | 0 | | pos.setErrorIndex(pos.getIndex()); |
| 194 | | | } |
| 195 | | | else |
| 196 | | | { |
| 197 | 100 | | rc = parse(paddedString, pos).toString(); |
| 198 | | | } |
| 199 | | | } |
| 200 | 100 | | return rc; |
| 201 | | | } |
| 202 | | | |
| 203 | | | |
| 204 | | | |
| 205 | | | |
| 206 | | | |
| 207 | | | |
| 208 | | | |
| 209 | | | @param |
| 210 | | | @param |
| 211 | | | @param |
| 212 | | | |
| 213 | | | @return |
| 214 | | | |
| 215 | | | @see |
| 216 | | | |
| 217 | | | public StringBuffer format (Object obj, StringBuffer toAppendTo, |
| 218 | | | FieldPosition pos) |
| 219 | | | { |
| 220 | 100 | | if (obj == null) |
| 221 | | | { |
| 222 | 0 | (3) | throw new NullPointerException( |
| 223 | | | "Supplied object to format must not be null"); |
| 224 | | | } |
| 225 | 100 | | else if (! (obj instanceof String)) |
| 226 | | | { |
| 227 | 0 | | throw new IllegalArgumentException("Supplied object to be formatted " |
| 228 | | | + "must be a String but is " |
| 229 | | | + obj.getClass().getName() + ": " + obj); |
| 230 | | | } |
| 231 | | | else |
| 232 | | | { |
| 233 | 100 | | pos.setBeginIndex(0); |
| 234 | 100 | | pos.setEndIndex(0); |
| 235 | 100 | | final String toBeFormatted = (String) obj; |
| 236 | 100 | | final int len = toBeFormatted.length(); |
| 237 | 100 | | if (len > mAllPadding.length && mType == LEFT_CUT_RIGHT_PADDING) |
| 238 | | | { |
| 239 | 100 | | toAppendTo.append(toBeFormatted.substring( |
| 240 | | | len - mAllPadding.length)); |
| 241 | | | } |
| 242 | 100 | | else if (len > mAllPadding.length) |
| 243 | | | { |
| 244 | 0 | | toAppendTo.append(toBeFormatted.substring(0, mAllPadding.length)); |
| 245 | | | } |
| 246 | 100 | | else if (len < mAllPadding.length) |
| 247 | | | { |
| 248 | 100 | | if (mType == LEFT_PADDING) |
| 249 | | | { |
| 250 | 100 | | toAppendTo.append(mAllPadding, 0, mAllPadding.length - len); |
| 251 | 100 | | toAppendTo.append(toBeFormatted); |
| 252 | | | } |
| 253 | | | else |
| 254 | | | { |
| 255 | 100 | | toAppendTo.append(toBeFormatted); |
| 256 | 100 | | toAppendTo.append(mAllPadding, 0, mAllPadding.length - len); |
| 257 | | | } |
| 258 | | | } |
| 259 | | | else |
| 260 | | | { |
| 261 | 100 | | toAppendTo.append(toBeFormatted); |
| 262 | | | } |
| 263 | | | } |
| 264 | 100 | | return toAppendTo; |
| 265 | | | } |
| 266 | | | |
| 267 | | | private CharSequence parse ( |
| 268 | | | final CharBuffer paddedString, |
| 269 | | | final ParsePosition pos) |
| 270 | | | { |
| 271 | 100 | | CharSequence rc = null; |
| 272 | | | |
| 273 | 100 | | pos.setIndex(pos.getIndex() + mAllPadding.length); |
| 274 | | | |
| 275 | 100 | | final char pad = mAllPadding[0]; |
| 276 | 100 | | if (mType == LEFT_PADDING) |
| 277 | | | { |
| 278 | 100 | | int i = 0; |
| 279 | | | while ((i < mAllPadding.length) |
| 280 | 100 | | && (paddedString.charAt(i) == pad)) |
| 281 | | | { |
| 282 | 100 | | ++i; |
| 283 | | | } |
| 284 | 100 | | rc = paddedString.subSequence(i, mAllPadding.length); |
| 285 | 100 | | } |
| 286 | | | else |
| 287 | | | { |
| 288 | 100 | | int i = mAllPadding.length - 1; |
| 289 | 100 | | while ((i >= 0) && (paddedString.charAt(i) == pad)) |
| 290 | | | { |
| 291 | 100 | | --i; |
| 292 | | | } |
| 293 | 100 | | if (i < 0) |
| 294 | | | { |
| 295 | 0 | | rc = ""; |
| 296 | | | } |
| 297 | | | else |
| 298 | | | { |
| 299 | 100 | | rc = paddedString.subSequence(0, i + 1); |
| 300 | | | } |
| 301 | | | } |
| 302 | 100 | | return rc; |
| 303 | | | } |
| 304 | | | } |