PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/SmartJsonDb/src/net/smart_json_database/tools/Util.java

https://bitbucket.org/appfellas/smartjsondb
Java | 219 lines | 85 code | 35 blank | 99 comment | 28 complexity | f739749e1feef8077e8403a990b45d8a MD5 | raw file
  1. /*
  2. *
  3. Copyright 2011 Andreas Hohnholt
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package net.smart_json_database.tools;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Collection;
  17. import java.util.Date;
  18. /**
  19. *
  20. * A helper Class
  21. *
  22. * @author Andreas Hohnholt
  23. *
  24. */
  25. public class Util {
  26. private static SimpleDateFormat dateFormat = new SimpleDateFormat(
  27. "yyyy-MM-dd HH:mm:ss");
  28. /**
  29. *
  30. *
  31. * @param s
  32. * @return true if the given string is null or empty
  33. */
  34. public static boolean IsNullOrEmpty(String s) {
  35. if (s == null)
  36. return true;
  37. if ("".equals(s.trim()))
  38. return true;
  39. return false;
  40. }
  41. /**
  42. *
  43. * @param collection
  44. * @return true if the given collection is null or emtpy
  45. */
  46. public static boolean IsNullOrEmpty(Collection<?> collection) {
  47. if (collection == null)
  48. return true;
  49. if (collection.size() < 1)
  50. return true;
  51. return false;
  52. }
  53. /**
  54. *
  55. * @param array
  56. * @return true if the given array is null or emtpy
  57. */
  58. public static boolean IsNullOrEmpty(Object[] array) {
  59. if (array == null)
  60. return true;
  61. if (array.length < 1)
  62. return true;
  63. return false;
  64. }
  65. /**
  66. *
  67. * @param array
  68. * @return true if the given array is null or emtpy
  69. */
  70. public static boolean IsNullOrEmpty(int[] array) {
  71. if (array == null)
  72. return true;
  73. if (array.length < 1)
  74. return true;
  75. return false;
  76. }
  77. /**
  78. *
  79. * @param array
  80. * @return true if the given array is null or emtpy
  81. */
  82. public static boolean IsNullOrEmpty(double[] array) {
  83. if (array == null)
  84. return true;
  85. if (array.length < 1)
  86. return true;
  87. return false;
  88. }
  89. /**
  90. *
  91. * @param array
  92. * @return true if the given array is null or emtpy
  93. */
  94. public static boolean IsNullOrEmpty(long[] array) {
  95. if (array == null)
  96. return true;
  97. if (array.length < 1)
  98. return true;
  99. return false;
  100. }
  101. /**
  102. *
  103. * @param s
  104. * @return false if the given string is null or empty
  105. */
  106. public static boolean IsNotNullOrEmpty(String s) {
  107. return !IsNullOrEmpty(s);
  108. }
  109. /**
  110. * Convert a string with the format yyyy-MM-dd HH:mm:ss to a date object
  111. *
  112. * @param date
  113. * - must have the format yyyy-MM-dd HH:mm:ss
  114. * @return the date based on the given string or null if the the string
  115. * isn't parsable
  116. */
  117. public static Date ParseDateFromString(String date) {
  118. String[] split1 = date.trim().split(" ");
  119. if (split1.length != 2)
  120. return null;
  121. String[] dateElements = split1[0].split("-");
  122. String[] timeElements = split1[1].split(":");
  123. if (dateElements.length != 3)
  124. return null;
  125. if (timeElements.length != 3)
  126. return null;
  127. // Date d = new Date(
  128. // Integer.parseInt(dateElements[0]) - 1900, // incoming year is
  129. // // +1900 // 3910 ->
  130. // // 2010
  131. // Integer.parseInt(dateElements[1]) - 1, // month is wrong else
  132. // Integer.parseInt(dateElements[2]),
  133. // Integer.parseInt(timeElements[0]),
  134. // Integer.parseInt(timeElements[1]),
  135. // Integer.parseInt(timeElements[2]));
  136. Date d = new Date(Integer.parseInt(dateElements[0]) - 1900,
  137. Integer.parseInt(dateElements[1]) - 1,
  138. Integer.parseInt(dateElements[2]),
  139. Integer.parseInt(timeElements[0]),
  140. Integer.parseInt(timeElements[1]));
  141. return d;
  142. }
  143. /**
  144. * Covernt a date object to a string in the format yyyy-MM-dd HH:mm:ss
  145. *
  146. * @param date
  147. * @return a yyyy-MM-dd HH:mm:ss string
  148. */
  149. public static String DateToString(Date date) {
  150. return dateFormat.format(date);
  151. }
  152. /**
  153. * Convert long to int
  154. *
  155. * @exception IllegalArgumentException
  156. * if _long < Integer.MIN_VALUE || _long > Integer.MAX_VALUE
  157. * @param _long
  158. * @return
  159. */
  160. public static int LongToInt(long _long) {
  161. if (_long < Integer.MIN_VALUE || _long > Integer.MAX_VALUE) {
  162. throw new IllegalArgumentException(
  163. "cast from long to int will change the value of " + _long);
  164. }
  165. return (int) _long;
  166. }
  167. /**
  168. * Returns the defaultvalue if the value is null
  169. *
  170. * @param <T>
  171. * @param value
  172. * @param defaultValue
  173. * @return
  174. */
  175. public static <T> T GetDefaultValue(T value, T defaultValue) {
  176. if (value != null)
  177. return value;
  178. return defaultValue;
  179. }
  180. }