public Authentication getAuthentication(String token) {
    Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();

    Collection<? extends GrantedAuthority> authorities =
        Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(","))
            .map(SimpleGrantedAuthority::new)
            .collect(Collectors.toList());

    User principal = new User(claims.getSubject(), "", authorities);

    return new UsernamePasswordAuthenticationToken(principal, "", authorities);
  }
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    String[] numbers = sc.nextLine().split(" ");

    String sort = sc.nextLine();

    if (sort.equals("Ascending")) {
      List<Integer> output =
          Arrays.stream(numbers).map(Integer::parseInt).sorted().collect(Collectors.toList());

      for (Object items : output) {
        System.out.print(items + " ");
      }
    } else if (sort.equals("Descending")) {
      List<Integer> output =
          Arrays.stream(numbers)
              .map(Integer::parseInt)
              .sorted(Comparator.reverseOrder())
              .collect(Collectors.toList());

      for (Object items : output) {
        System.out.print(items + " ");
      }
    }
  }
Exemple #3
0
  @Override
  public Optional<Suggestion> requestPlayerSuggestion(Player player, Room room) {
    char userWantsToMakeSuggestion = '\0';

    while (userWantsToMakeSuggestion != 'Y' && userWantsToMakeSuggestion != 'N') {
      this.out.println("Do you want to make an suggestion (Y/N)?");
      this.out.println("Your cards are: " + player.cards);
      userWantsToMakeSuggestion = this.scanner.next().charAt(0);
    }

    if (userWantsToMakeSuggestion == 'Y') {
      this.out.printf("You suggest it was done in the %s, by: \n", room);

      Stream<String> suspects =
          Arrays.stream(CluedoCharacter.values()).map(CluedoCharacter::toString);
      CluedoCharacter suspect = CluedoCharacter.values()[this.selectOptionFromList(suspects)];

      this.out.println("with the ");

      Stream<String> weapons = Arrays.stream(Weapon.values()).map(Weapon::toString);
      Weapon weapon = Weapon.values()[this.selectOptionFromList(weapons)];

      return Optional.of(new Suggestion(suspect, weapon, room));

    } else {
      return Optional.empty();
    }
  }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String inputData = scanner.nextLine();
    List<Team> championsLeague = new ArrayList<>();
    while (!inputData.equals("stop")) {
      String[] inputArgs = inputData.split("\\|+");
      String firstTeamName = inputArgs[0].trim();
      String secondTeamName = inputArgs[1].trim();

      if (!championsLeague.stream().anyMatch(t -> t.name.equals(firstTeamName))) {
        championsLeague.add(new Team(firstTeamName));
      }
      if (!championsLeague.stream().anyMatch(t -> t.name.equals(secondTeamName))) {
        championsLeague.add(new Team(secondTeamName));
      }

      Integer[] firstMatchResult =
          Arrays.stream(inputArgs[2].trim().split(":+"))
              .map(Integer::parseInt)
              .toArray(Integer[]::new);
      Integer[] secondMatchResult =
          Arrays.stream(inputArgs[3].trim().split(":+"))
              .map(Integer::parseInt)
              .toArray(Integer[]::new);

      Integer firstTeamGoals = firstMatchResult[0] + secondMatchResult[1];
      Integer secondTeamGoals = firstMatchResult[1] + secondMatchResult[0];

      Team firstTeam =
          championsLeague.stream().filter(t -> t.name.equals(firstTeamName)).findFirst().get();
      Team secondTeam =
          championsLeague.stream().filter(t -> t.name.equals(secondTeamName)).findFirst().get();

      if (firstTeamGoals > secondTeamGoals) {
        firstTeam.wins++;
      } else if (secondTeamGoals > firstTeamGoals) {
        secondTeam.wins++;
      } else if (firstTeamGoals.equals(secondTeamGoals)) {
        int firstTeamAwayGoals = secondMatchResult[1];
        int secondTeamAwayGoals = firstMatchResult[1];
        if (firstTeamAwayGoals > secondTeamAwayGoals) {
          firstTeam.wins++;
        } else {
          secondTeam.wins++;
        }
      }
      firstTeam.opponents.add(secondTeam);
      secondTeam.opponents.add(firstTeam);
      inputData = scanner.nextLine();
    }

    Collections.sort(championsLeague);
    championsLeague.forEach(System.out::println);
  }
  @Test
  public void testRemoveUnfinishedLeftovers_abort_multipleFolders() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);

    File origiFolder = new Directories(cfs.metadata).getDirectoryForNewSSTables();
    File dataFolder1 = new File(origiFolder, "1");
    File dataFolder2 = new File(origiFolder, "2");
    Files.createDirectories(dataFolder1.toPath());
    Files.createDirectories(dataFolder2.toPath());

    SSTableReader[] sstables = {
      sstable(dataFolder1, cfs, 0, 128),
      sstable(dataFolder1, cfs, 1, 128),
      sstable(dataFolder2, cfs, 2, 128),
      sstable(dataFolder2, cfs, 3, 128)
    };

    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);

    LogTransaction.SSTableTidier[] tidiers = {
      log.obsoleted(sstables[0]), log.obsoleted(sstables[2])
    };

    log.trackNew(sstables[1]);
    log.trackNew(sstables[3]);

    Collection<File> logFiles = log.logFiles();
    Assert.assertEquals(2, logFiles.size());

    // fake an abort
    log.txnFile().abort();

    Arrays.stream(sstables).forEach(s -> s.selfRef().release());

    // test listing
    Assert.assertEquals(
        sstables[1].getAllFilePaths().stream().map(File::new).collect(Collectors.toSet()),
        getTemporaryFiles(dataFolder1));
    Assert.assertEquals(
        sstables[3].getAllFilePaths().stream().map(File::new).collect(Collectors.toSet()),
        getTemporaryFiles(dataFolder2));

    // normally called at startup
    LogTransaction.removeUnfinishedLeftovers(Arrays.asList(dataFolder1, dataFolder2));

    // old tables should be only table left
    assertFiles(dataFolder1.getPath(), new HashSet<>(sstables[0].getAllFilePaths()));
    assertFiles(dataFolder2.getPath(), new HashSet<>(sstables[2].getAllFilePaths()));

    // complete the transaction to avoid LEAK errors
    Arrays.stream(tidiers).forEach(LogTransaction.SSTableTidier::run);
    assertNull(log.complete(null));
  }
 public static <T> List<T> createFromList(String raw, TypedOption<T> option) {
   if (raw == null) {
     return Collections.emptyList();
   }
   final String[] segments = raw.split(option.getListSeparator());
   return Arrays.stream(segments).map(s -> create(s.trim(), option)).collect(Collectors.toList());
 }
  // helper method to test if required files are present in the runtime
  public void testRuntime(RelativeFileSet runtime, String[] file) throws ConfigException {
    if (runtime == null) {
      return; // null runtime is ok (request to use system)
    }

    Pattern[] weave = Arrays.stream(file).map(Pattern::compile).toArray(Pattern[]::new);

    if (!runtime
        .getIncludedFiles()
        .stream()
        .anyMatch(s -> Arrays.stream(weave).anyMatch(pattern -> pattern.matcher(s).matches()))) {
      throw new ConfigException(
          MessageFormat.format(I18N.getString("error.jre-missing-file"), Arrays.toString(file)),
          I18N.getString("error.jre-missing-file.advice"));
    }
  }
Exemple #8
0
 public static <T> LinkedList<T> create(T head, T... members) {
   return Try.catchAndThrow(
       () -> {
         // At least 1 parameter is ensured with this function signature!
         final List<T> args =
             Stream.concat(Stream.of(head), Arrays.stream(members)).collect(Collectors.toList());
         return TailCall.run(() -> build(new StackWindow<T>(args), null));
       });
 }
  /**
   * Deserialized string into inverted index entry
   *
   * @param serializedEntry String serialized form of inverted index
   * @return Inverted Index entry
   */
  public static InvertedIndex deserializeEntry(String serializedEntry) {
    String term = serializedEntry.substring(0, serializedEntry.indexOf('[')).trim();
    String entry = serializedEntry.substring(serializedEntry.indexOf('['));
    entry = entry.replace('[', ' ').replace(']', ' ').trim();
    String[] indices = entry.split(" ");
    Map<String, Long> map =
        Arrays.stream(indices)
            .map(index -> index.split("="))
            .collect(Collectors.toMap(tokens -> tokens[0], tokens -> Long.parseLong(tokens[1])));

    return new InvertedIndex(term, map);
  }
Exemple #10
0
  @Override
  public Optional<Suggestion> requestPlayerAccusation(Player player) {
    char userWantsToMakeAccusation = '\0';

    while (userWantsToMakeAccusation != 'Y' && userWantsToMakeAccusation != 'N') {
      this.out.println("Do you want to make an accusation (Y/N)?");
      userWantsToMakeAccusation = this.scanner.next().charAt(0);
    }

    if (userWantsToMakeAccusation == 'Y') {
      this.out.println("Choose your suspect:");

      Stream<String> suspects =
          Arrays.stream(CluedoCharacter.values())
              .filter(e -> !player.cards.contains(e))
              .map(CluedoCharacter::toString);
      CluedoCharacter suspect = CluedoCharacter.values()[this.selectOptionFromList(suspects)];

      this.out.println("Choose your weapon:");

      Stream<String> weapons =
          Arrays.stream(Weapon.values())
              .filter(e -> !player.cards.contains(e))
              .map(Weapon::toString);
      Weapon weapon = Weapon.values()[this.selectOptionFromList(weapons)];

      this.out.println("Choose your room:");

      Stream<String> rooms =
          Arrays.stream(Room.values()).filter(e -> !player.cards.contains(e)).map(Room::toString);
      Room room = Room.values()[this.selectOptionFromList(rooms)];

      return Optional.of(new Suggestion(suspect, weapon, room));

    } else {
      return Optional.empty();
    }
  }
  /**
   * Randomly initializes this GaussianMixture for a given number of terms.
   *
   * @param random a {@link java.util.Random} object.
   * @param numTerms a number of terms.
   */
  public void randomInitialization(Random random, int numTerms) {

    this.coefficients = new double[numTerms];
    this.terms = new ArrayList<>(numTerms);
    for (int k = 0; k < numTerms; k++) {
      this.coefficients[k] = random.nextDouble();

      Normal aux = new Normal(this.var);
      aux.setMean(5 * random.nextGaussian());
      aux.setVariance(random.nextDouble());

      this.terms.add(aux);
    }
    ;
    DoubleStream aux = Arrays.stream(this.coefficients);
    double suma = aux.sum();
    aux = Arrays.stream(this.coefficients);
    this.coefficients = aux.map(x -> x / suma).toArray();

    // System.out.println(coefficients);
    // this.coefficients = this.coefficients / .map().sum();

  }
  @Test
  public void testAbortMultipleFolders() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);

    File origiFolder = new Directories(cfs.metadata).getDirectoryForNewSSTables();
    File dataFolder1 = new File(origiFolder, "1");
    File dataFolder2 = new File(origiFolder, "2");
    Files.createDirectories(dataFolder1.toPath());
    Files.createDirectories(dataFolder2.toPath());

    SSTableReader[] sstables = {
      sstable(dataFolder1, cfs, 0, 128),
      sstable(dataFolder1, cfs, 1, 128),
      sstable(dataFolder2, cfs, 2, 128),
      sstable(dataFolder2, cfs, 3, 128)
    };

    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);

    LogTransaction.SSTableTidier[] tidiers = {
      log.obsoleted(sstables[0]), log.obsoleted(sstables[2])
    };

    log.trackNew(sstables[1]);
    log.trackNew(sstables[3]);

    Arrays.stream(tidiers).forEach(LogTransaction.SSTableTidier::abort);
    log.abort();

    Arrays.stream(sstables).forEach(s -> s.selfRef().release());
    LogTransaction.waitForDeletions();

    assertFiles(dataFolder1.getPath(), new HashSet<>(sstables[0].getAllFilePaths()));
    assertFiles(dataFolder2.getPath(), new HashSet<>(sstables[2].getAllFilePaths()));
  }
    private ResourcesDef readResourcesDefinition(Element resourcesElement) {
      String name = resourcesElement.getAttribute("name");
      Assert.ifEmpty(name, "can`t read resources bundle without name");

      ResourcesDef resDef = new ResourcesDef(name);

      String depends = resourcesElement.getAttribute("depends");
      if (isNoneEmpty(depends)) {
        resDef.setDepends(
            Arrays.stream(StringUtils.split(depends, ","))
                .map(StringUtils::trim)
                .collect(Collectors.toList()));
      }

      readResources(resourcesElement, resDef);

      return resDef;
    }
 public static Map<String, Set<String>> getSelectedReleaseAndAttachmentIdsFromRequest(
     ResourceRequest request) {
   Map<String, Set<String>> releaseIdToAttachmentIds = new HashMap<>();
   String[] checkboxes =
       request.getParameterValues(PortalConstants.LICENSE_INFO_RELEASE_TO_ATTACHMENT);
   Arrays.stream(checkboxes)
       .forEach(
           s -> {
             String[] split = s.split(":");
             if (split.length == 2) {
               String releaseId = split[0];
               String attachmentId = split[1];
               if (!releaseIdToAttachmentIds.containsKey(releaseId)) {
                 releaseIdToAttachmentIds.put(releaseId, new HashSet<>());
               }
               releaseIdToAttachmentIds.get(releaseId).add(attachmentId);
             }
           });
   return releaseIdToAttachmentIds;
 }
Exemple #15
0
  public static void main(String... args) {

    List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);

    Arrays.stream(numbers.toArray()).forEach(System.out::println);
    int calories = Dish.menu.stream().mapToInt(Dish::getCalories).sum();
    System.out.println("Number of calories:" + calories);

    // max and OptionalInt
    OptionalInt maxCalories = Dish.menu.stream().mapToInt(Dish::getCalories).max();

    int max;
    if (maxCalories.isPresent()) {
      max = maxCalories.getAsInt();
    } else {
      // we can choose a default value
      max = 1;
    }
    System.out.println(max);

    // numeric ranges
    IntStream evenNumbers = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0);

    System.out.println(evenNumbers.count());

    Stream<int[]> pythagoreanTriples =
        IntStream.rangeClosed(1, 100)
            .boxed()
            .flatMap(
                a ->
                    IntStream.rangeClosed(a, 100)
                        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                        .boxed()
                        .map(b -> new int[] {a, b, (int) Math.sqrt(a * a + b * b)}));

    pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));
  }
  /**
   * get document and their corresponding length
   *
   * @param stemmedFile String stemmed file path
   * @return Map of Document -> Length
   * @throws IOException
   */
  private static Map<String, Long> getDocumentAndLength(String stemmedFile) throws IOException {
    HashMap<String, Long> documentLength = new HashMap<>();

    Stream<String> stemmed = FileUtils.readFiles(stemmedFile);
    Iterator<String> reader = stemmed.iterator();
    String lastDocId = null;
    while (reader.hasNext()) {
      String line = reader.next();
      if (isDocument(line)) {
        lastDocId = line.replace('#', ' ').trim();
        documentLength.put(lastDocId, 0L);
      } else {
        long length =
            Arrays.stream(line.split(SEPARATOR_SPACE)).filter(Indexer::isNotNumber).count();
        if (documentLength.containsKey(lastDocId)) {
          documentLength.put(lastDocId, documentLength.get(lastDocId) + length);
        } else {
          documentLength.put(lastDocId, length);
        }
      }
    }

    return documentLength;
  }
 /**
  * get all terms
  *
  * @param line String line
  * @return list of term
  */
 private static List<String> getAllTerms(String line) {
   return Arrays.stream(line.split(SEPARATOR_SPACE))
       .filter(Indexer::isNotNumber)
       .collect(Collectors.toList());
 }
  private static void testRemoveUnfinishedLeftovers_multipleFolders_errorConditions(
      Consumer<LogTransaction> modifier, boolean shouldCommit) throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);

    File origiFolder = new Directories(cfs.metadata).getDirectoryForNewSSTables();
    File dataFolder1 = new File(origiFolder, "1");
    File dataFolder2 = new File(origiFolder, "2");
    Files.createDirectories(dataFolder1.toPath());
    Files.createDirectories(dataFolder2.toPath());

    SSTableReader[] sstables = {
      sstable(dataFolder1, cfs, 0, 128),
      sstable(dataFolder1, cfs, 1, 128),
      sstable(dataFolder2, cfs, 2, 128),
      sstable(dataFolder2, cfs, 3, 128)
    };

    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);

    LogTransaction.SSTableTidier[] tidiers = {
      log.obsoleted(sstables[0]), log.obsoleted(sstables[2])
    };

    log.trackNew(sstables[1]);
    log.trackNew(sstables[3]);

    // fake some error condition on the txn logs
    modifier.accept(log);

    Arrays.stream(sstables).forEach(s -> s.selfRef().release());

    LogTransaction.removeUnfinishedLeftovers(Arrays.asList(dataFolder1, dataFolder2));
    LogTransaction.waitForDeletions();

    if (shouldCommit) {
      // only new sstables should still be there
      assertFiles(dataFolder1.getPath(), new HashSet<>(sstables[1].getAllFilePaths()));
      assertFiles(dataFolder2.getPath(), new HashSet<>(sstables[3].getAllFilePaths()));
    } else {
      // all files should still be there
      assertFiles(
          dataFolder1.getPath(),
          Sets.newHashSet(
              Iterables.concat(
                  sstables[0].getAllFilePaths(),
                  sstables[1].getAllFilePaths(),
                  Collections.singleton(log.logFilePaths().get(0)))));
      assertFiles(
          dataFolder2.getPath(),
          Sets.newHashSet(
              Iterables.concat(
                  sstables[2].getAllFilePaths(),
                  sstables[3].getAllFilePaths(),
                  Collections.singleton(log.logFilePaths().get(1)))));
    }

    // complete the transaction to avoid LEAK errors
    Arrays.stream(tidiers).forEach(LogTransaction.SSTableTidier::run);
    log.txnFile().commit(); // just anything to make sure transaction tidier will finish
    assertNull(log.complete(null));
  }
  /**
   * Given arguments specifying an SSTable, and optionally an output file, export the contents of
   * the SSTable to JSON.
   *
   * @param args command lines arguments
   * @throws ConfigurationException on configuration failure (wrong params given)
   */
  public static void main(String[] args) throws ConfigurationException {
    CommandLineParser parser = new PosixParser();
    try {
      cmd = parser.parse(options, args);
    } catch (ParseException e1) {
      System.err.println(e1.getMessage());
      printUsage();
      System.exit(1);
    }

    if (cmd.getArgs().length != 1) {
      System.err.println("You must supply exactly one sstable");
      printUsage();
      System.exit(1);
    }

    String[] keys = cmd.getOptionValues(KEY_OPTION);
    HashSet<String> excludes =
        new HashSet<>(
            Arrays.asList(
                cmd.getOptionValues(EXCLUDE_KEY_OPTION) == null
                    ? new String[0]
                    : cmd.getOptionValues(EXCLUDE_KEY_OPTION)));
    String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();

    if (Descriptor.isLegacyFile(new File(ssTableFileName))) {
      System.err.println("Unsupported legacy sstable");
      System.exit(1);
    }
    if (!new File(ssTableFileName).exists()) {
      System.err.println("Cannot find file " + ssTableFileName);
      System.exit(1);
    }
    Descriptor desc = Descriptor.fromFilename(ssTableFileName);
    try {
      CFMetaData metadata = metadataFromSSTable(desc);
      if (cmd.hasOption(ENUMERATE_KEYS_OPTION)) {
        JsonTransformer.keysToJson(
            null,
            iterToStream(new KeyIterator(desc, metadata)),
            cmd.hasOption(RAW_TIMESTAMPS),
            metadata,
            System.out);
      } else {
        SSTableReader sstable = SSTableReader.openNoValidation(desc, metadata);
        IPartitioner partitioner = sstable.getPartitioner();
        final ISSTableScanner currentScanner;
        if ((keys != null) && (keys.length > 0)) {
          List<AbstractBounds<PartitionPosition>> bounds =
              Arrays.stream(keys)
                  .filter(key -> !excludes.contains(key))
                  .map(metadata.getKeyValidator()::fromString)
                  .map(partitioner::decorateKey)
                  .sorted()
                  .map(DecoratedKey::getToken)
                  .map(token -> new Bounds<>(token.minKeyBound(), token.maxKeyBound()))
                  .collect(Collectors.toList());
          currentScanner = sstable.getScanner(bounds.iterator());
        } else {
          currentScanner = sstable.getScanner();
        }
        Stream<UnfilteredRowIterator> partitions =
            iterToStream(currentScanner)
                .filter(
                    i ->
                        excludes.isEmpty()
                            || !excludes.contains(
                                metadata.getKeyValidator().getString(i.partitionKey().getKey())));
        if (cmd.hasOption(DEBUG_OUTPUT_OPTION)) {
          AtomicLong position = new AtomicLong();
          partitions.forEach(
              partition -> {
                position.set(currentScanner.getCurrentPosition());

                if (!partition.partitionLevelDeletion().isLive()) {
                  System.out.println(
                      "["
                          + metadata.getKeyValidator().getString(partition.partitionKey().getKey())
                          + "]@"
                          + position.get()
                          + " "
                          + partition.partitionLevelDeletion());
                }
                if (!partition.staticRow().isEmpty()) {
                  System.out.println(
                      "["
                          + metadata.getKeyValidator().getString(partition.partitionKey().getKey())
                          + "]@"
                          + position.get()
                          + " "
                          + partition.staticRow().toString(metadata, true));
                }
                partition.forEachRemaining(
                    row -> {
                      System.out.println(
                          "["
                              + metadata
                                  .getKeyValidator()
                                  .getString(partition.partitionKey().getKey())
                              + "]@"
                              + position.get()
                              + " "
                              + row.toString(metadata, false, true));
                      position.set(currentScanner.getCurrentPosition());
                    });
              });
        } else {
          JsonTransformer.toJson(
              currentScanner, partitions, cmd.hasOption(RAW_TIMESTAMPS), metadata, System.out);
        }
      }
    } catch (IOException e) {
      // throwing exception outside main with broken pipe causes windows cmd to hang
      e.printStackTrace(System.err);
    }

    System.exit(0);
  }
  @Test
  public void testGetTemporaryFilesMultipleFolders() throws IOException {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);

    File origiFolder = new Directories(cfs.metadata).getDirectoryForNewSSTables();
    File dataFolder1 = new File(origiFolder, "1");
    File dataFolder2 = new File(origiFolder, "2");
    Files.createDirectories(dataFolder1.toPath());
    Files.createDirectories(dataFolder2.toPath());

    SSTableReader[] sstables = {
      sstable(dataFolder1, cfs, 0, 128),
      sstable(dataFolder1, cfs, 1, 128),
      sstable(dataFolder2, cfs, 2, 128),
      sstable(dataFolder2, cfs, 3, 128)
    };

    // they should all have the same number of files since they are created in the same way
    int numSStableFiles = sstables[0].getAllFilePaths().size();

    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);

    for (File dataFolder : new File[] {dataFolder1, dataFolder2}) {
      Set<File> tmpFiles = getTemporaryFiles(dataFolder);
      assertNotNull(tmpFiles);
      assertEquals(0, tmpFiles.size());
    }

    LogTransaction.SSTableTidier[] tidiers = {
      log.obsoleted(sstables[0]), log.obsoleted(sstables[2])
    };

    log.trackNew(sstables[1]);
    log.trackNew(sstables[3]);

    for (File dataFolder : new File[] {dataFolder1, dataFolder2}) {
      Set<File> tmpFiles = getTemporaryFiles(dataFolder);
      assertNotNull(tmpFiles);
      assertEquals(numSStableFiles, tmpFiles.size());
    }

    log.finish();

    for (File dataFolder : new File[] {dataFolder1, dataFolder2}) {
      Set<File> tmpFiles = getTemporaryFiles(dataFolder);
      assertNotNull(tmpFiles);
      assertEquals(numSStableFiles, tmpFiles.size());
    }

    sstables[0].markObsolete(tidiers[0]);
    sstables[2].markObsolete(tidiers[1]);

    Arrays.stream(sstables).forEach(s -> s.selfRef().release());
    LogTransaction.waitForDeletions();

    for (File dataFolder : new File[] {dataFolder1, dataFolder2}) {
      Set<File> tmpFiles = getTemporaryFiles(dataFolder);
      assertNotNull(tmpFiles);
      assertEquals(0, tmpFiles.size());
    }
  }
  @Override
  protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usages = refUsages.get();
    ArrayList<String> conflicts = new ArrayList<String>();

    for (PsiElement element : myElements) {
      for (SafeDeleteProcessorDelegate delegate :
          Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
        if (delegate.handlesElement(element)) {
          Collection<String> foundConflicts =
              delegate instanceof SafeDeleteProcessorDelegateBase
                  ? ((SafeDeleteProcessorDelegateBase) delegate)
                      .findConflicts(element, myElements, usages)
                  : delegate.findConflicts(element, myElements);
          if (foundConflicts != null) {
            conflicts.addAll(foundConflicts);
          }
          break;
        }
      }
    }

    final HashMap<PsiElement, UsageHolder> elementsToUsageHolders = sortUsages(usages);
    final Collection<UsageHolder> usageHolders = elementsToUsageHolders.values();
    for (UsageHolder usageHolder : usageHolders) {
      if (usageHolder.hasUnsafeUsagesInCode()) {
        conflicts.add(usageHolder.getDescription());
      }
    }

    if (!conflicts.isEmpty()) {
      final RefactoringEventData conflictData = new RefactoringEventData();
      conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts);
      myProject
          .getMessageBus()
          .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC)
          .conflictsDetected("refactoring.safeDelete", conflictData);
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!ConflictsInTestsException.isTestIgnore())
          throw new ConflictsInTestsException(conflicts);
      } else {
        UnsafeUsagesDialog dialog =
            new UnsafeUsagesDialog(ArrayUtil.toStringArray(conflicts), myProject);
        if (!dialog.showAndGet()) {
          final int exitCode = dialog.getExitCode();
          prepareSuccessful(); // dialog is always dismissed;
          if (exitCode == UnsafeUsagesDialog.VIEW_USAGES_EXIT_CODE) {
            showUsages(
                Arrays.stream(usages)
                    .filter(
                        usage ->
                            usage instanceof SafeDeleteReferenceUsageInfo
                                && !((SafeDeleteReferenceUsageInfo) usage).isSafeDelete())
                    .toArray(UsageInfo[]::new));
          }
          return false;
        } else {
          myPreviewNonCodeUsages = false;
        }
      }
    }

    UsageInfo[] preprocessedUsages = usages;
    for (SafeDeleteProcessorDelegate delegate :
        Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
      preprocessedUsages = delegate.preprocessUsages(myProject, preprocessedUsages);
      if (preprocessedUsages == null) return false;
    }
    final UsageInfo[] filteredUsages = UsageViewUtil.removeDuplicatedUsages(preprocessedUsages);
    prepareSuccessful(); // dialog is always dismissed
    if (filteredUsages == null) {
      return false;
    }
    refUsages.set(filteredUsages);
    return true;
  }
Exemple #22
0
  @Override
  public Map<Long, Long> findFS() {
    int[] usedCounts = Arrays.copyOfRange(counts, 0, capwidth);

    // initial frequency is the same as what we see from counters
    Map<Integer, Long> freq =
        Arrays.stream(usedCounts)
            .boxed()
            .collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
    Long zeroNum = freq.get(0);
    if (zeroNum != null && zeroNum == capwidth) {
      return new HashMap<>();
    }

    // estimate the number of items
    int num = (int) (capwidth * Math.log(1.0 * capwidth / zeroNum));

    Distribution newDistribution = new Distribution(freq);
    Distribution oldDistribution = new Distribution();

    // A pattern is a setting of itmes+frequencies that sum to sumI
    // All patterns in this list will map to a common number sumI
    List<Map<Integer, Integer>> patterns = new ArrayList<>();
    List<Double> probabilities = new ArrayList<>();
    int iterations = 0;

    // iteratively update the distribution
    // while (notConverged()) {
    while (iterations < MAXIMUM_ITERATIONS) {
      oldDistribution.fillFrom(newDistribution);
      newDistribution.clear();
      for (Map.Entry<Integer, Long> entry : freq.entrySet()) {
        int sumI = entry.getKey();
        int freqI = entry.getValue().intValue();
        if (sumI == 0) { // skip key=0
          continue;
        }
        // find new probable patterns, get their probabilities and update the distribution
        getPatterns(patterns, sumI, freqI, oldDistribution);
        computeProbabilities(patterns, oldDistribution, probabilities);
        Iterator<Double> probabilityIterator = probabilities.iterator();
        for (Map<Integer, Integer> pattern : patterns) { // for each pattern
          double probability = probabilityIterator.next();
          for (Map.Entry<Integer, Integer> patternEntry : pattern.entrySet()) {
            newDistribution.addFreq(
                patternEntry.getKey(), freqI * patternEntry.getValue() * probability);
          }
        }
      }

      // scale factor to make sum of distribution equal to 1
      double scale = num / newDistribution.sumFreq();
      pw.println(
          String.format(
              DISTRIBUTION_TRACE_FORMATTER,
              getStep() + 1,
              iterations + 1,
              newDistribution.toString(scale)));
      iterations++;
    }
    pw.flush();
    System.out.println(getStep());

    Map<Long, Long> output = new HashMap<>();
    double freqSum = newDistribution.sumFreq();

    for (Map.Entry<Integer, Double> entry : newDistribution.freq.entrySet()) {
      output.put(entry.getKey().longValue(), (long) (entry.getValue() / freqSum * num));
    }
    return output;
  }
 public Set<String> getTopics() {
   if (topicsString == null || topicsString.isEmpty()) return new HashSet<>();
   return Arrays.stream(topicsString.split(",")).map(String::trim).collect(Collectors.toSet());
 }