| 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 | |
| 20 | /** |
| 21 | * Structure to append domain specific notes to {@link Timer} recordings. |
| 22 | * Examples of domain specific notes include number of query terms, size of |
| 23 | * results, size of document, service provider name, transaction type, etc. |
| 24 | * These notes may be attached as key value pairs or a list of items. |
| 25 | * <p> |
| 26 | * TimerNotes is immutable and as such, all note values must be provided during |
| 27 | * construction. |
| 28 | * <p> |
| 29 | * TimerNotes may be persisted for later retrieval using serialization or by |
| 30 | * converting the notes to a single {@link String} value using |
| 31 | * {@link #toSingleValue()}. This value can be used to recreate the TimerNotes |
| 32 | * instance using {@link #parse(String)}. |
| 33 | * |
| 34 | * @author dhagberg |
| 35 | * |
| 36 | */ |
| 37 | public class KeyedTimerNotes implements Serializable, TimerNotes { |
| 38 | |
| 39 | private static final long serialVersionUID = -2180211898163201478L; |
| 40 | |
| 41 | private final String[] keys; |
| 42 | private final Object[] notes; |
| 43 | |
| 44 | /** |
| 45 | * Create a new {@link KeyedTimerNotes} and store the provided notes for for |
| 46 | * future retrieval. If keyed is true, the notes must be in key/value pairs |
| 47 | * (.e.g., key1, value1, key2, value2 ...). |
| 48 | * |
| 49 | * @param keyed |
| 50 | * true if notes are provided as key/value pairs, false if all |
| 51 | * notes are values. |
| 52 | * @param notes |
| 53 | * list of either key/value pairs or values. |
| 54 | */ |
| 55 | public KeyedTimerNotes(Object... notes) { |
| 56 | super(); |
| 57 | if (notes.length % 2 != 0) { |
| 58 | throw new IllegalArgumentException( |
| 59 | "Notes must be in key value pairs if keyed state is set"); |
| 60 | } |
| 61 | this.keys = new String[notes.length / 2]; |
| 62 | this.notes = new Object[notes.length / 2]; |
| 63 | for (int i = 0; i < keys.length; i++) { |
| 64 | this.keys[i] = (String) notes[2 * i]; |
| 65 | this.notes[i] = notes[2 * i + 1]; |
| 66 | |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * (non-Javadoc) |
| 72 | * |
| 73 | * @see net.digitaltsunami.tmeter.TimerNotes#getStringValue(int) |
| 74 | */ |
| 75 | @Override |
| 76 | public String getStringValue(int index) { |
| 77 | return String.valueOf(notes[index]); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * (non-Javadoc) |
| 82 | * |
| 83 | * @see |
| 84 | * net.digitaltsunami.tmeter.TimerNotes#getStringValue(java.lang.String) |
| 85 | */ |
| 86 | @Override |
| 87 | public String getStringValue(String key) { |
| 88 | return String.valueOf(getValue(key)); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * (non-Javadoc) |
| 93 | * |
| 94 | * @see net.digitaltsunami.tmeter.TimerNotes#getValue(int) |
| 95 | */ |
| 96 | @Override |
| 97 | public Object getValue(int index) { |
| 98 | return notes[index]; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * (non-Javadoc) |
| 103 | * |
| 104 | * @see net.digitaltsunami.tmeter.TimerNotes#getValue(java.lang.String) |
| 105 | */ |
| 106 | @Override |
| 107 | public Object getValue(String key) { |
| 108 | for (int i = 0; i < keys.length; i++) { |
| 109 | if (key.equalsIgnoreCase(keys[i])) { |
| 110 | return notes[i]; |
| 111 | } |
| 112 | } |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * (non-Javadoc) |
| 118 | * |
| 119 | * @see net.digitaltsunami.tmeter.TimerNotes#isKeyed() |
| 120 | */ |
| 121 | @Override |
| 122 | public boolean isKeyed() { |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * (non-Javadoc) |
| 128 | * |
| 129 | * @see |
| 130 | * net.digitaltsunami.tmeter.TimerNotes#getIndexForKey(java.lang.String) |
| 131 | */ |
| 132 | @Override |
| 133 | public int getIndexForKey(String key) { |
| 134 | for (int i = 0; i < keys.length; i++) { |
| 135 | if (key.equalsIgnoreCase(keys[i])) { |
| 136 | return i; |
| 137 | } |
| 138 | } |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * (non-Javadoc) |
| 144 | * |
| 145 | * @see net.digitaltsunami.tmeter.TimerNotes#getKeys() |
| 146 | */ |
| 147 | @Override |
| 148 | public String[] getKeys() { |
| 149 | return keys.clone(); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * (non-Javadoc) |
| 154 | * |
| 155 | * @see net.digitaltsunami.tmeter.TimerNotes#getNotes() |
| 156 | */ |
| 157 | @Override |
| 158 | public Object[] getNotes() { |
| 159 | return notes.clone(); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * (non-Javadoc) |
| 164 | * |
| 165 | * @see net.digitaltsunami.tmeter.TimerNotes#getLength() |
| 166 | */ |
| 167 | @Override |
| 168 | public int getLength() { |
| 169 | return notes.length; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * (non-Javadoc) |
| 174 | * |
| 175 | * @see net.digitaltsunami.tmeter.TimerNotes#getFormattedNote(int) |
| 176 | */ |
| 177 | @Override |
| 178 | public String getFormattedNote(int index) { |
| 179 | StringBuilder sb = new StringBuilder(100); |
| 180 | return sb.append(keys[index]).append("=").append(notes[index]).toString(); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * (non-Javadoc) |
| 185 | * |
| 186 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue() |
| 187 | */ |
| 188 | @Override |
| 189 | public String toSingleValue() { |
| 190 | return toSingleValue(TimerNotes.NOTE_DELIMITER, TimerNotes.KEY_VALUE_DELIMITER); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * (non-Javadoc) |
| 195 | * |
| 196 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue(char) |
| 197 | */ |
| 198 | @Override |
| 199 | public String toSingleValue(char noteDelimiter) { |
| 200 | return toSingleValue(noteDelimiter, TimerNotes.KEY_VALUE_DELIMITER); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * (non-Javadoc) |
| 205 | * |
| 206 | * @see net.digitaltsunami.tmeter.TimerNotes#toSingleValue(char, char) |
| 207 | */ |
| 208 | @Override |
| 209 | public String toSingleValue(char noteDelimiter, char keyValueDelimiter) { |
| 210 | StringBuilder sb = new StringBuilder(100); |
| 211 | for (int i = 0; i < notes.length; i++) { |
| 212 | if (i > 0) { |
| 213 | sb.append(noteDelimiter); |
| 214 | } |
| 215 | sb.append(keys[i]).append(keyValueDelimiter).append(notes[i]); |
| 216 | } |
| 217 | return sb.toString(); |
| 218 | } |
| 219 | } |