| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015, 2016 EfficiOS Inc. and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | *******************************************************************************/ |
| 9 | |
| 10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module; |
| 11 | |
| 12 | import java.text.FieldPosition; |
| 13 | import java.text.SimpleDateFormat; |
| 14 | import java.util.Date; |
| 15 | |
| 16 | import org.eclipse.jdt.annotation.Nullable; |
| 17 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; |
| 18 | |
| 19 | /** |
| 20 | * Formatter for time stamps |
| 21 | */ |
| 22 | public class LamiTimeStampFormat extends SimpleDateFormat { |
| 23 | |
| 24 | private static final long serialVersionUID = 4285447886537779762L; |
| 25 | |
| 26 | private final TmfTimestampFormat fFormat; |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | // Constructors |
| 30 | // ------------------------------------------------------------------------ |
| 31 | |
| 32 | /** |
| 33 | * The default constructor (uses the default time format) |
| 34 | */ |
| 35 | public LamiTimeStampFormat() { |
| 36 | fFormat = TmfTimestampFormat.getDefaulTimeFormat(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * The normal constructor |
| 41 | * |
| 42 | * @param pattern the format pattern |
| 43 | */ |
| 44 | public LamiTimeStampFormat(String pattern) { |
| 45 | fFormat = new TmfTimestampFormat(pattern); |
| 46 | } |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Operations |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | @Override |
| 53 | public StringBuffer format(@Nullable Date date, @Nullable StringBuffer toAppendTo, |
| 54 | @Nullable FieldPosition fieldPosition) { |
| 55 | if (date != null && toAppendTo != null) { |
| 56 | long time = date.getTime(); |
| 57 | toAppendTo.append(fFormat.format(time)); |
| 58 | return toAppendTo; |
| 59 | } |
| 60 | return new StringBuffer(); |
| 61 | } |
| 62 | } |