| 1 | /* __copyright_begin__ |
| 2 | Copyright 2011 Dan Hagberg |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | __copyright_end__ */ |
| 16 | package net.digitaltsunami.tmeter; |
| 17 | |
| 18 | import java.io.Serializable; |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
| 22 | * Structure to append domain specific notes to {@link Timer} recordings. |
| 23 | * Examples of domain specific notes include number of query terms, size of |
| 24 | * results, size of document, service provider name, transaction type, etc. |
| 25 | * These notes may be attached as key value pairs or a list of items. |
| 26 | * <p> |
| 27 | * TimerNotes is immutable and as such, all note values must be provided during |
| 28 | * construction. |
| 29 | * <p> |
| 30 | * TimerNotes may be persisted for later retrieval using serialization or by |
| 31 | * converting the notes to a single {@link String} value using |
| 32 | * {@link #toSingleValue()}. This value can be used to recreate the TimerNotes |
| 33 | * instance using {@link #parse(String)}. |
| 34 | * |
| 35 | * @author dhagberg |
| 36 | * |
| 37 | */ |
| 38 | public class TimerNoteList implements Serializable, TimerNotes { |
| 39 | |
| 40 | private static final long serialVersionUID = -2180211898163201478L; |
| 41 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 42 | |
| 43 | private final Object[] notes; |
| 44 | |
| 45 | /** |
| 46 | * Create a new {@link TimerNoteList} and store all provided values as notes. |
| 47 | * |
| 48 | * @param notes |
| 49 | * list of values to be stored as notes. |
| 50 | */ |
| 51 | public TimerNoteList(Object... notes) { |
| 52 | super(); |
| 53 | this.notes = notes.clone(); |
| 54 | } |
| 55 | |
| 56 | /* (non-Javadoc) |
| 57 | * @see net.digitaltsunami.tmeter.TimerNotes#getStringValue(int) |
| 58 | */ |
| 59 | @Override |
| 60 | public String getStringValue(int index) { |
| 61 | return String.valueOf(notes[index]); |
| 62 | } |
| 63 | |
| 64 | /* (non-Javadoc) |
| 65 | * @see net.digitaltsunami.tmeter.TimerNotes#getStringValue(java.lang.String) |
| 66 | */ |
| 67 | @Override |
| 68 | public String getStringValue(String key) { |
| 69 | throw new IllegalStateException("Cannot use keyed access on non-keyed values"); |
| 70 | } |
| 71 | |
| 72 | /* (non-Javadoc) |
| 73 | * @see net.digitaltsunami.tmeter.TimerNotes#getValue(int) |
| 74 | */ |
| 75 | @Override |
| 76 | public Object getValue(int index) { |
| 77 | return notes[index]; |
| 78 | } |
| 79 | |
| 80 | /* (non-Javadoc) |
| 81 | * @see net.digitaltsunami.tmeter.TimerNotes#getValue(java.lang.String) |
| 82 | */ |
| 83 | @Override |
| 84 | public Object getValue(String key) { |
| 85 | throw new IllegalStateException("Cannot use keyed access on non-keyed values"); |
| 86 | } |
| 87 | |
| 88 | /* (non-Javadoc) |
| 89 | * @see net.digitaltsunami.tmeter.TimerNotes#isKeyed() |
| 90 | */ |
| 91 | @Override |
| 92 | public boolean isKeyed() { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /* (non-Javadoc) |
| 97 | * @see net.digitaltsunami.tmeter.TimerNotes#getIndexForKey(java.lang.String) |
| 98 | */ |
| 99 | @Override |
| 100 | public int getIndexForKey(String key) { |
| 101 | throw new IllegalStateException("Cannot use keyed access on non-keyed values"); |
| 102 | } |
| 103 | |
| 104 | /* (non-Javadoc) |
| 105 | * @see net.digitaltsunami.tmeter.TimerNotes#getNotes() |
| 106 | */ |
| 107 | @Override |
| 108 | public Object[] getNotes() { |
| 109 | return notes.clone(); |
| 110 | } |
| 111 | |
| 112 | /* (non-Javadoc) |
| 113 | * @see net.digitaltsunami.tmeter.TimerNotes#getLength() |
| 114 | */ |
| 115 | @Override |
| 116 | public int getLength() { |
| 117 | return notes.length; |
| 118 | } |
| 119 | |
| 120 | /* (non-Javadoc) |
| 121 | * @see net.digitaltsunami.tmeter.TimerNotes#getFormattedNote(int) |
| 122 | */ |
| 123 | @Override |
| 124 | public String getFormattedNote(int index) { |
| 125 | return String.valueOf(notes[index]); |
| 126 | } |
| 127 | |
| 128 | /* (non-Javadoc) |
| 129 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue() |
| 130 | */ |
| 131 | @Override |
| 132 | public String toSingleValue() { |
| 133 | return toSingleValue(TimerNotes.NOTE_DELIMITER); |
| 134 | } |
| 135 | |
| 136 | /* (non-Javadoc) |
| 137 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue(char) |
| 138 | */ |
| 139 | @Override |
| 140 | public String toSingleValue(char noteDelimiter) { |
| 141 | StringBuilder sb = new StringBuilder(100); |
| 142 | for (int i = 0; i < notes.length; i++) { |
| 143 | if (i > 0) { |
| 144 | sb.append(noteDelimiter); |
| 145 | } |
| 146 | sb.append(notes[i]); |
| 147 | } |
| 148 | return sb.toString(); |
| 149 | } |
| 150 | |
| 151 | /* (non-Javadoc) |
| 152 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue(char, char) |
| 153 | */ |
| 154 | @Override |
| 155 | public String toSingleValue(char noteDelimiter, char keyValueDelimiter) { |
| 156 | return toSingleValue(noteDelimiter); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public String[] getKeys() { |
| 161 | return EMPTY_STRING_ARRAY; |
| 162 | } |
| 163 | } |