Util.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package uk.ac.ic.wlgitbridge.util;
  2. import uk.ac.ic.wlgitbridge.application.SSLConfig;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. /**
  6. * Created by Winston on 19/11/14.
  7. */
  8. public class Util {
  9. private static String SERVICE_NAME;
  10. private static String HOSTNAME;
  11. private static SSLConfig SSL_CONFIG;
  12. private static int PORT;
  13. public static String entries(int entries) {
  14. if (entries == 1) {
  15. return "entry";
  16. } else {
  17. return "entries";
  18. }
  19. }
  20. public static int booleanToInt(boolean b) {
  21. if (b) {
  22. return 1;
  23. } else {
  24. return 0;
  25. }
  26. }
  27. public static boolean intToBoolean(int i) {
  28. return i != 0;
  29. }
  30. private static String removeAllSuffix(String str, String suffix) {
  31. int lastIndexOfSuffix;
  32. String result = str;
  33. while ((lastIndexOfSuffix = result.lastIndexOf(suffix)) > -1) {
  34. result = result.substring(0, lastIndexOfSuffix);
  35. }
  36. return result;
  37. }
  38. /* removeAllSuffixes("something.git///", "/", ".git") => "something" */
  39. public static String removeAllSuffixes(String str, String... suffixes) {
  40. String result = str;
  41. for (String suffix : suffixes) {
  42. result = removeAllSuffix(result, suffix);
  43. }
  44. return result;
  45. }
  46. public static String getContentsOfReader(BufferedReader reader) throws IOException {
  47. StringBuilder sb = new StringBuilder();
  48. for (String line; (line = reader.readLine()) != null; ) {
  49. sb.append(line);
  50. }
  51. return sb.toString();
  52. }
  53. public static void setServiceName(String serviceName) {
  54. SERVICE_NAME = serviceName;
  55. }
  56. public static String getServiceName() {
  57. return SERVICE_NAME;
  58. }
  59. public static void setHostname(String hostname) {
  60. HOSTNAME = hostname;
  61. }
  62. public static String getHostname() {
  63. return HOSTNAME;
  64. }
  65. public static SSLConfig getSSLConfig() {
  66. return SSL_CONFIG;
  67. }
  68. public static void setSSL(SSLConfig ssl) {
  69. SSL_CONFIG = ssl;
  70. }
  71. public static int getPort() {
  72. return PORT;
  73. }
  74. public static void setPort(int port) {
  75. PORT = port;
  76. }
  77. }