Network topology and inventory init
[transportpce.git] / inventory / src / main / java / org / opendaylight / transportpce / inventory / utils / StringUtils.java
1 /*
2  * Copyright © 2017 AT&T and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.inventory.utils;
9
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12
13 /**
14  * Utility class which handles strings in inventory project.
15  *
16  */
17 public class StringUtils {
18     public static final String DEFAULT_SQL_DATE = "yyyy-MM-dd HH:mm:ss";
19
20     private StringUtils() {
21         // hiding the constructor
22     }
23
24     /**
25      * Returns the current timestamp formatted with
26      * {@link StringUtils#DEFAULT_SQL_DATE}.
27      *
28      * @see StringUtils#getTimestamp(Date)
29      * @return String the current timestamp formatted
30      */
31     public static String getCurrentTimestamp() {
32         return getTimestamp(new Date());
33     }
34
35     /**
36      * This method will format the provided {@link Date} with the
37      * {@link StringUtils#DEFAULT_SQL_DATE} format.
38      *
39      * @param date a date
40      * @return string represenation of the given date
41      */
42     public static String getTimestamp(Date date) {
43         SimpleDateFormat simpleDate = new SimpleDateFormat(DEFAULT_SQL_DATE);
44         return simpleDate.format(date);
45     }
46
47     /**
48      * Checks the input object for null and if it's null returns a dash instead.
49      *
50      * @param object an input object
51      * @return if object is null a dash is returned,
52      *         otherwise {@link Object#toString()}
53      */
54     public static String prepareDashString(Object object) {
55         return prepareString(object, "-");
56     }
57
58     /**
59      * Checks the input object for null and if's null returns an empty string instead.
60      *
61      * @param object an input object
62      * @return if object is null an empty string is returned,
63      *         otherwise {@link Object#toString()}
64      */
65     public static String prepareEmptyString(Object object) {
66         return prepareString(object, "");
67     }
68
69     /**
70      * Checks if the given object is null and returns its representation given by
71      * replacement.
72      *
73      * @param objectString a string object
74      * @param replacement a replacement
75      * @return String the representation of the object given by replacement
76      */
77     public static String prepareString(Object objectString, String replacement) {
78         return objectString == null ? replacement : objectString.toString();
79     }
80 }