Пример #1
0
 /**
  * Checks a prospective crontab specification to see if it could benefit from balanced hashes.
  *
  * @param spec a (legal) spec
  * @return a similar spec that uses a hash, if such a transformation is necessary; null if it is
  *     OK as is
  * @since 1.510
  */
 public static @CheckForNull String hashify(String spec) {
   if (spec.contains("H")) {
     // if someone is already using H, presumably he knows what it is, so a warning is likely false
     // positive
     return null;
   } else if (spec.startsWith("*/")) { // "*/15 ...." (every N minutes) to hash
     return "H" + spec.substring(1);
   } else if (spec.matches("\\d+ .+")) { // "0 ..." (certain minute) to hash
     return "H " + spec.substring(spec.indexOf(' ') + 1);
   } else {
     Matcher m = Pattern.compile("0(,(\\d+)(,\\d+)*)( .+)").matcher(spec);
     if (m.matches()) { // 0,15,30,45 to H/15
       int period = Integer.parseInt(m.group(2));
       if (period > 0) {
         StringBuilder b = new StringBuilder();
         for (int i = period; i < 60; i += period) {
           b.append(',').append(i);
         }
         if (b.toString().equals(m.group(1))) {
           return "H/" + period + m.group(4);
         }
       }
     }
     return null;
   }
 }
  String handle(String question) {
    Matcher additionMatcher = compile("what is (\\d+) plus (\\d+)").matcher(question);
    if (additionMatcher.matches()) {
      return String.valueOf(valueOf(additionMatcher.group(1)) + valueOf(additionMatcher.group(2)));
    }

    Matcher maximumMatcher =
        compile("which of the following numbers is the largest: (.+)").matcher(question);
    if (maximumMatcher.matches()) {
      Integer max = 0;
      for (String number : maximumMatcher.group(1).split(", ")) {
        max = max(max, valueOf(number));
      }
      return max.toString();
    }

    Matcher productMatcher =
        compile(
                "what is the product of \\[(\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+)\\]")
            .matcher(question);
    if (productMatcher.matches()) {
      BigDecimal product = new BigDecimal(1);
      for (int i = 1; i <= 10; i++) {
        BigDecimal b = new BigDecimal(productMatcher.group(i));
        product = product.multiply(b);
      }
      return product.toString();
    }

    Matcher shaMatcher = compile("what is the sha1 for \"([\\w_]+)\"").matcher(question);
    if (shaMatcher.matches()) {
      try {
        byte[] digest = MessageDigest.getInstance("SHA-1").digest(shaMatcher.group(1).getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte aByteDigest : digest) {
          String hex = Integer.toHexString(0xFF & aByteDigest);
          if (hex.length() == 1) {
            sb.append('0');
          }
          sb.append(hex);
        }
        return sb.toString();
      } catch (NoSuchAlgorithmException e) {
        err.println(e.getMessage());
      }
    }

    Matcher piMatcher =
        compile("what is the (\\d+)(?:st|nd|rd|th) decimal of Pi").matcher(question);
    if (piMatcher.matches()) {
      Integer nthDecimal = valueOf(piMatcher.group(1));
      return "141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117"
          .substring(nthDecimal - 1, nthDecimal);
    }

    Matcher feetToMeterMatcher = compile("how much is (\\d+) feet in meters").matcher(question);
    if (feetToMeterMatcher.matches()) {
      return String.valueOf(valueOf(feetToMeterMatcher.group(1)) * 0.3048);
    }

    Matcher hexaAddition =
        compile("what is the decimal value of 0x([\\da-f]+) plus 0x([\\da-f]+)").matcher(question);
    if (hexaAddition.matches()) {
      return String.valueOf(
          parseInt(hexaAddition.group(1), 16) + parseInt(hexaAddition.group(2), 16));
    }

    if ("what is the answer to life, the universe and everything".equals(question)) {
      return "42";
    }
    if ("in which language was the first 'hello, world' written".equals(question)) {
      return "c";
    }
    if ("what does 'RTFM' stand for".equals(question)) {
      return "Read The F*****g Manual";
    }
    if ("who counted to infinity twice".equals(question)) {
      return "Chuck Norris";
    }
    if ("who said 'Luke, I am your father'".equals(question)) {
      return "Darth Vader";
    }

    Matcher dayOfWeekMatcher =
        compile("which day of the week is \\s?([\\d]+) (\\w{3}) (\\d+)").matcher(question);
    if (dayOfWeekMatcher.matches()) {
      Calendar parsedDate = new GregorianCalendar();
      parsedDate.set(DATE, valueOf(dayOfWeekMatcher.group(1)));

      Integer month = null;
      switch (dayOfWeekMatcher.group(2)) {
        case "Jan":
          month = JANUARY;
          break;
        case "Feb":
          month = FEBRUARY;
          break;
        case "Mar":
          month = MARCH;
          break;
        case "Apr":
          month = APRIL;
          break;
        case "May":
          month = MAY;
          break;
        case "Jun":
          month = JUNE;
          break;
        case "Jul":
          month = JULY;
          break;
        case "Aug":
          month = AUGUST;
          break;
        case "Sep":
          month = SEPTEMBER;
          break;
        case "Oct":
          month = OCTOBER;
          break;
        case "Nov":
          month = NOVEMBER;
          break;
        case "Dec":
          month = DECEMBER;
          break;
      }
      if (month != null) {
        parsedDate.set(MONTH, month);
      }

      parsedDate.set(YEAR, valueOf(dayOfWeekMatcher.group(3)));

      return parsedDate.getDisplayName(DAY_OF_WEEK, LONG, US);
    }

    Matcher httpResponseMatcher =
        compile(
                "what HTTP response status do you get when you send a GET request to "
                    + "(?:(http://(?:\\d{1,3}\\.){3}\\d{1,3}(?:\\:\\d+)?/http_code/[0-9a-z]{8})|a teapot)")
            .matcher(question);
    if (httpResponseMatcher.matches()) {
      if ("a teapot".equals(httpResponseMatcher.group(1))) {
        return "418";
      }
      URLConnection urlConnection = null;
      try {
        urlConnection = new URL(httpResponseMatcher.group(1)).openConnection();
        return String.valueOf(((HttpURLConnection) urlConnection).getResponseCode());
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (urlConnection != null) {
          ((HttpURLConnection) urlConnection).disconnect();
        }
      }
    }

    return "";
  }