Util.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package uk.ac.ic.wlgitbridge.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. /**
  5. * Created by Winston on 19/11/14.
  6. */
  7. public class Util {
  8. private static String SERVICE_NAME;
  9. public static String entries(int entries) {
  10. if (entries == 1) {
  11. return "entry";
  12. } else {
  13. return "entries";
  14. }
  15. }
  16. public static int booleanToInt(boolean b) {
  17. if (b) {
  18. return 1;
  19. } else {
  20. return 0;
  21. }
  22. }
  23. public static boolean intToBoolean(int i) {
  24. return i != 0;
  25. }
  26. private static String removeAllSuffix(String str, String suffix) {
  27. int lastIndexOfSuffix;
  28. String result = str;
  29. while ((lastIndexOfSuffix = result.lastIndexOf(suffix)) > -1) {
  30. result = result.substring(0, lastIndexOfSuffix);
  31. }
  32. return result;
  33. }
  34. /* removeAllSuffixes("something.git///", "/", ".git") => "something" */
  35. public static String removeAllSuffixes(String str, String... suffixes) {
  36. String result = str;
  37. for (String suffix : suffixes) {
  38. result = removeAllSuffix(result, suffix);
  39. }
  40. return result;
  41. }
  42. public static String getContentsOfReader(BufferedReader reader) throws IOException {
  43. StringBuilder sb = new StringBuilder();
  44. for (String line; (line = reader.readLine()) != null; ) {
  45. sb.append(line);
  46. }
  47. return sb.toString();
  48. }
  49. public static void setServiceName(String serviceName) {
  50. SERVICE_NAME = serviceName;
  51. }
  52. public static String getServiceName() {
  53. return SERVICE_NAME;
  54. }
  55. }