예제 #1
0
 private void find(MessageContext context, String args) {
   DiscordApiClient apiClient = context.getApiClient();
   Channel channel = context.getChannel();
   Pattern pattern;
   try {
     pattern = Pattern.compile(args);
   } catch (PatternSyntaxException pse) {
     apiClient.sendMessage(loc.localize("commands.admin.find.response.invalid"), channel);
     return;
   }
   Predicate<String> matcher = pattern.asPredicate();
   List<User> results =
       context
           .getServer()
           .getMembers()
           .stream()
           .map(Member::getUser)
           .filter(u -> matcher.test(u.getUsername()))
           .collect(Collectors.toList());
   int size = results.size();
   if (size > 20) {
     apiClient.sendMessage(loc.localize("commands.admin.find.response.oversize", size), channel);
     return;
   }
   StringJoiner resultJoiner = new StringJoiner(", ");
   results.stream().map(this::userToResult).forEach(resultJoiner::add);
   apiClient.sendMessage(
       loc.localize("commands.admin.find.response.format", size, resultJoiner.toString()),
       channel);
 }
 @Test
 public void shouldPutDelimiterBetweenStrings() {
   StringJoiner joinerWithDelimiter = new StringJoiner(",");
   strings.add("A");
   strings.add("B");
   assertThat(joinerWithDelimiter.join(strings), is("A,B"));
 }
예제 #3
0
  private void ban(MessageContext context, String args) {
    if (args.isEmpty()) {
      return;
    }
    String[] split = args.split(" ");
    List<String> banned = new ArrayList<>();
    List<String> failed = new ArrayList<>();
    Channel channel = context.getChannel();
    for (String userStr : split) {
      User user = findUser(context, userStr);
      String userId = user.getId();
      if (user == NO_USER) {
        userId = userStr;
      }

      if (banChecked(channel, context.getAuthor(), user, context.getServer())) {
        banned.add(userId + " " + user.getUsername());
      } else {
        failed.add(userId + " " + user.getUsername());
      }
    }

    if (channel.getId() != null) {
      StringJoiner joiner = new StringJoiner("\n");
      for (String s : banned) {
        String[] pair = s.split(" ", 2);
        joiner.add(loc.localize("commands.mod.ban.response", pair[1], pair[0]));
      }
      apiClient.sendMessage(joiner.toString(), channel);
    }
  }
 @Override
 public String toString() {
   StringBuilder out = new StringBuilder();
   out.append(type);
   if (args != null) {
     StringJoiner joiner = new StringJoiner(", ", "<", ">");
     for (Object arg : args) joiner.add(arg.toString());
     out.append(joiner);
   }
   return out.toString();
 }
  public static void main(String[] args) {
    // Methods in Comparator
    List<Person> people = new ArrayList<>();
    people.sort(
        Comparator.comparing(Person::getLastName)
            .thenComparing(Person::getFirstName)
            .thenComparing(
                Person::getEmailAddress, Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));

    // Old way of initializing ThreadLocal:
    ThreadLocal<List<String>> oldThreadLocalString =
        new ThreadLocal<List<String>>() {
          @Override
          public List<String> initialValue() {
            return new ArrayList<>();
          }
        };
    System.out.println(oldThreadLocalString.get());

    // New way:
    ThreadLocal<List<String>> newThreadLocalString = ThreadLocal.withInitial(ArrayList::new);
    System.out.println(newThreadLocalString.get());

    // Java Optional
    Optional<Integer> optional = new ArrayList<Integer>().stream().min(Integer::compareTo);
    System.out.println(optional);

    // Files can now return streams
    try {
      Stream stream = Files.list(Paths.get("c:\\temp\\"));
      stream = Files.lines(Paths.get("c:\\temp\\"), Charset.forName("UTF_32"));
      stream = Files.find(Paths.get("c:\\"), 5, (T, U) -> (T == U));

    } catch (IOException e) {
      UncheckedIOException ex = new UncheckedIOException("cause", e);
      System.out.println(ex.getMessage());
    }

    // Rejoice, for we finally have string joins!
    String joinExample = String.join(",", "a", "b", "c", "4", "E", "6");
    System.out.println(joinExample);

    StringJoiner joiner = new StringJoiner("-");
    joiner.add("abbie");
    joiner.add("doobie");
    System.out.println(joiner.toString());
  }
예제 #6
0
  static void stringTest() {
    String s1 = String.join(":", "alibaba", "icbu", "youguang");
    System.out.println(s1);

    String s2 = String.join(":", Lists.newArrayList("alibaba", "icbu", "youguang"));
    System.out.println(s2);

    // 创建一个字符流 统计去重后的字符数
    long count = s2.chars().distinct().count();
    System.out.println(s2 + " distinct char count=" + count);

    StringJoiner joiner = new StringJoiner(":");
    joiner.add("alibaba");
    joiner.add("icbu");
    joiner.add("youguang");
    System.out.println("StringJoiner toString=" + joiner.toString());

    joiner = new StringJoiner(":", "prefix-", "-suffix");
    joiner.add("alibaba");
    joiner.add("icbu");
    joiner.add("youguang");
    System.out.println("StringJoiner toString prefix&suffix=" + joiner.toString());
  }
예제 #7
0
 private String formatDuration(Duration duration) {
   StringJoiner joiner = new StringJoiner(", ");
   long days = duration.toDays();
   if (days > 0) {
     long years = days / 365;
     if (years > 0) {
       long centuries = years / 100;
       if (centuries > 0) {
         long millenia = centuries / 10;
         joiner.add(loc.localize("time.millenia", millenia));
         centuries = centuries % 10;
         if (centuries > 0) {
           joiner.add(loc.localize("time.centuries", centuries));
         }
       }
       years = years % 100;
       if (years > 0) {
         joiner.add(loc.localize("time.years", years));
       }
     }
     days = days % 365;
     joiner.add(loc.localize("time.days", days));
   }
   long hours = duration.toHours() % 24L;
   if (hours > 0) {
     joiner.add(loc.localize("time.hours", hours));
   }
   long minutes = duration.toMinutes() % 60L;
   if (minutes > 0) {
     joiner.add(loc.localize("time.minutes", minutes));
   }
   long seconds = duration.getSeconds() % 60L;
   if (seconds > 0) {
     joiner.add(loc.localize("time.seconds", seconds));
   }
   return joiner.toString();
 }
 @Test
 public void shouldContainBothStringsWhenListIsTwoStrings() {
   strings.add("A");
   strings.add("B");
   assertThat(joiner.join(strings), both(containsString("A")).and(containsString("B")));
 }
 @Test
 public void shouldJoinIntoTheStringWhenListIsOneString() {
   String aString = "A String";
   strings.add(aString);
   assertThat(joiner.join(strings), is(aString));
 }
예제 #10
0
 @Test
 public void shouldJoinIntoAnEmptyStringWhenListIsEmpty() {
   assertThat(joiner.join(strings), is(""));
 }