Abort power setup if setting gainloss fails
[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 final 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 {@link StringUtils#DEFAULT_SQL_DATE}.
26      *
27      * @see StringUtils#getTimestamp(Date)
28      * @return Timestamp represenation of the given date
29      */
30     public static String getCurrentTimestamp() {
31         return getTimestamp(new Date());
32     }
33
34     /**
35      * This method will format the provided {@link Date} with the {@link StringUtils#DEFAULT_SQL_DATE} format.
36      *
37      * @param date link date
38      * @return string represenation of the given date
39      */
40     public static String getTimestamp(Date date) {
41         SimpleDateFormat myTimeStamp = new SimpleDateFormat(DEFAULT_SQL_DATE);
42         return myTimeStamp.format(date);
43     }
44
45     /**
46      * Checks the input object for null and if it's null returns a dash instead.
47      *
48      * @param object an object
49      * @return if object is null a dash is returned, otherwise {@link Object#toString()}
50      */
51     public static String prepareDashString(Object object) {
52         return prepareString(object, "");
53     }
54
55     /**
56      * Checks the input object for null and if's null returns an empty string instead.
57      *
58      * @param object an object
59      * @return if object is null an empty string is returned, otherwise {@link Object#toString()}
60      */
61     public static String prepareEmptyString(Object object) {
62         return prepareString(object, "");
63     }
64
65     /**
66      * Checks if the given object is null and returns its representation given by replacement.
67      *
68      * @param objectString an object
69      * @param replacement a replacement string
70      * @return a string representing the object
71      */
72     public static String prepareString(Object objectString, String replacement) {
73         return objectString == null ? replacement : objectString.toString();
74     }
75
76     /**
77      * Checks if the given object is null and returns -1 .
78      *
79      * @param object an object
80      * @return a string representing the object or -1 if null
81      */
82     public static String prepareEmptyInt(Object object) {
83         return (object == null ? "-1" : object.toString());
84     }
85 }