| 1 | package net.digitaltsunami.tmeter; |
| 2 | |
| 3 | /** |
| 4 | * Parser that will convert a string representation of timer notes into an |
| 5 | * instance of {@link TimerNotes}. |
| 6 | * |
| 7 | * @author dhagberg |
| 8 | * |
| 9 | */ |
| 10 | public class TimerNotesParser { |
| 11 | |
| 12 | /** |
| 13 | * Parse a {@link String} representation of a {@link KeyedTimerNotes} |
| 14 | * instance and return a new instance with the values set as appropriate. |
| 15 | * <p> |
| 16 | * Each of the notes must be delimited by {@link #NOTE_DELIMITER}. Notes may |
| 17 | * be keyed or non-keyed. If keyed, each note must be delimited by |
| 18 | * {@link #KEY_VALUE_DELIMITER}. |
| 19 | * |
| 20 | * @param notes |
| 21 | * Single string value containing notes delimited by the |
| 22 | * applicable character values. |
| 23 | * @return an instance of {@link KeyedTimerNotes} populated using the values |
| 24 | * contained within the notes parameter. |
| 25 | */ |
| 26 | public static TimerNotes parse(String notes) { |
| 27 | return parse(notes, TimerNotes.NOTE_DELIMITER, TimerNotes.KEY_VALUE_DELIMITER); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Parse a {@link String} representation of a {@link KeyedTimerNotes} |
| 32 | * instance and return a new instance with the values set as appropriate. |
| 33 | * <p> |
| 34 | * Each of the notes must be delimited by the provided noteDelimiter value. |
| 35 | * Notes may be keyed or non-keyed. If keyed, each note must be delimited by |
| 36 | * {@link #KEY_VALUE_DELIMITER}. |
| 37 | * |
| 38 | * @param notes |
| 39 | * Single string value containing notes delimited by the |
| 40 | * applicable character values. |
| 41 | * @param noteDelimiter |
| 42 | * character value used to delimit note values within provided |
| 43 | * notes string. |
| 44 | * @return an instance of {@link KeyedTimerNotes} populated using the values |
| 45 | * contained within the notes parameter. |
| 46 | */ |
| 47 | public static TimerNotes parse(String notes, char noteDelimiter) { |
| 48 | return parse(notes, noteDelimiter, TimerNotes.KEY_VALUE_DELIMITER); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Parse a {@link String} representation of a {@link KeyedTimerNotes} |
| 54 | * instance and return a new instance with the values set as appropriate. |
| 55 | * <p> |
| 56 | * Each of the notes must be delimited by the provided noteDelimiter value. |
| 57 | * Notes may be keyed or non-keyed. If keyed, each note must be delimited by |
| 58 | * the provided keyValueDelimiter value. |
| 59 | * |
| 60 | * @param notes |
| 61 | * Single string value containing notes delimited by the |
| 62 | * applicable character values. |
| 63 | * @param noteDelimiter |
| 64 | * character value used to delimit note values within provided |
| 65 | * notes string. |
| 66 | * @param keyValueDelimiter |
| 67 | * character value used to delimit key and value within each note |
| 68 | * extracted from the notes parameter. |
| 69 | * @return an instance of {@link KeyedTimerNotes} populated using the values |
| 70 | * contained within the notes parameter. |
| 71 | */ |
| 72 | public static TimerNotes parse(String notes, char noteDelimiter, |
| 73 | char keyValueDelimiter) { |
| 74 | if (notes.length() == 0) { |
| 75 | return new KeyedTimerNotes(); |
| 76 | } |
| 77 | String[] noteVals = notes.split(String.valueOf(noteDelimiter)); |
| 78 | // Test the first note to determine if notes are keyed. If so, |
| 79 | // assumption is all are keyed. |
| 80 | boolean valsKeyed = noteVals[0].indexOf(keyValueDelimiter) > 0; |
| 81 | Object[] noteArgs; |
| 82 | // Keyed notes are provided key1,val1,key2,val2; whereas non-keyed are |
| 83 | // just val1,val2. |
| 84 | if (valsKeyed) { |
| 85 | noteArgs = new Object[noteVals.length * 2]; |
| 86 | int argIdx = 0; |
| 87 | for (String noteVal : noteVals) { |
| 88 | int delimIdx = noteVal.indexOf(keyValueDelimiter); |
| 89 | if (delimIdx > -1) { |
| 90 | noteArgs[argIdx++] = noteVal.substring(0, delimIdx); |
| 91 | noteArgs[argIdx++] = noteVal.substring(delimIdx + 1); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } else { |
| 96 | // Notes already in correct format. |
| 97 | return new TimerNoteList(noteVals); |
| 98 | } |
| 99 | return new KeyedTimerNotes(noteArgs); |
| 100 | } |
| 101 | |
| 102 | } |