Java Code Examples for java.util.Collections.rotate()

The following are Jave code examples for showing how to use rotate() of the java.util.Collections class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaide   File: DimensionHelpers.java   View Source Code Vote up 6 votes
/**
 * Rotate the list of dimension specifiers until all dimensions with type annotations appear in
 * source order.
 * <p>
 * <p>javac reorders dimension specifiers in method declarations with mixed-array notation, which
 * means that any type annotations don't appear in source order.
 * <p>
 * <p>For example, the type of {@code int @A [] f() @B [] {}} is parsed as {@code @B [] @A []}.
 * <p>
 * <p>This doesn't handle cases with un-annotated dimension specifiers, so the formatting logic
 * checks the token stream to figure out which side of the method name they appear on.
 */
private static Iterable<List<AnnotationTree>> reorderBySourcePosition(
        Deque<List<AnnotationTree>> dims) {
    int lastAnnotation = -1;
    int lastPos = -1;
    int idx = 0;
    for (List<AnnotationTree> dim : dims) {
        if (!dim.isEmpty()) {
            int pos = ((JCTree) dim.get(0)).getStartPosition();
            if (pos < lastPos) {
                List<List<AnnotationTree>> list = new ArrayList<>(dims);
                Collections.rotate(list, -(lastAnnotation + 1));
                return list;
            }
            lastPos = pos;
            lastAnnotation = idx;
        }
        idx++;
    }
    return dims;
}
 
Example 2
Project: GlitchKernel   File: GlitchController.java   View Source Code Vote up 5 votes
public void moveAlgorithmAtIndex(int sourcePosition, int targetPosition)
{
	logger.debug("Shifting algorithm in list. Source=" + sourcePosition + ", Target=" + targetPosition);
	if (sourcePosition <= targetPosition) 
	{
		Collections.rotate(this.algorithmList.subList(sourcePosition, targetPosition + 1), -1);
	} 
	else 
	{
		Collections.rotate(this.algorithmList.subList(targetPosition, sourcePosition + 1), 1);
	}
}
 
Example 3
Project: elasticsearch_my   File: RestClient.java   View Source Code Vote up 5 votes
/**
 * Returns an {@link Iterable} of hosts to be used for a request call.
 * Ideally, the first host is retrieved from the iterable and used successfully for the request.
 * Otherwise, after each failure the next host has to be retrieved from the iterator so that the request can be retried until
 * there are no more hosts available to retry against. The maximum total of attempts is equal to the number of hosts in the iterable.
 * The iterator returned will never be empty. In case there are no healthy hosts available, or dead ones to be be retried,
 * one dead host gets returned so that it can be retried.
 */
private HostTuple<Iterator<HttpHost>> nextHost() {
    final HostTuple<Set<HttpHost>> hostTuple = this.hostTuple;
    Collection<HttpHost> nextHosts = Collections.emptySet();
    do {
        Set<HttpHost> filteredHosts = new HashSet<>(hostTuple.hosts);
        for (Map.Entry<HttpHost, DeadHostState> entry : blacklist.entrySet()) {
            if (System.nanoTime() - entry.getValue().getDeadUntilNanos() < 0) {
                filteredHosts.remove(entry.getKey());
            }
        }
        if (filteredHosts.isEmpty()) {
            //last resort: if there are no good host to use, return a single dead one, the one that's closest to being retried
            List<Map.Entry<HttpHost, DeadHostState>> sortedHosts = new ArrayList<>(blacklist.entrySet());
            if (sortedHosts.size() > 0) {
                Collections.sort(sortedHosts, new Comparator<Map.Entry<HttpHost, DeadHostState>>() {
                    @Override
                    public int compare(Map.Entry<HttpHost, DeadHostState> o1, Map.Entry<HttpHost, DeadHostState> o2) {
                        return Long.compare(o1.getValue().getDeadUntilNanos(), o2.getValue().getDeadUntilNanos());
                    }
                });
                HttpHost deadHost = sortedHosts.get(0).getKey();
                logger.trace("resurrecting host [" + deadHost + "]");
                nextHosts = Collections.singleton(deadHost);
            }
        } else {
            List<HttpHost> rotatedHosts = new ArrayList<>(filteredHosts);
            Collections.rotate(rotatedHosts, rotatedHosts.size() - lastHostIndex.getAndIncrement());
            nextHosts = rotatedHosts;
        }
    } while(nextHosts.isEmpty());
    return new HostTuple<>(nextHosts.iterator(), hostTuple.authCache);
}
 
Example 4
Project: IslamicLibraryAndroid   File: BookmarkRecyclerViewAdapter.java   View Source Code Vote up 5 votes
/**
 * @param sortByDate true to sort bookmarks by date false to sort them by position in the book
 */
public void SortAndAnimate(boolean sortByDate) {
    Bookmark.sortByDate = sortByDate;
    for (int i = 1; i < bookmarkList.size(); i++) {
        int j = Math.abs(Collections.binarySearch(bookmarkList.subList(0, i), bookmarkList.get(i)) + 1);
        Collections.rotate(bookmarkList.subList(j, i + 1), j - i);
        notifyItemMoved(i, j);
    }
}
 
Example 5
Project: IslamicLibraryAndroid   File: HighlightRecyclerViewAdapter.java   View Source Code Vote up 5 votes
/**
 * @param sortByDate true to sort highlights by date false to sort them by position in the book
 */
public void SortAndAnimate(boolean sortByDate) {
    Highlight.sortByDate = sortByDate;
    for (int i = 1; i < highlightList.size(); i++) {
        int j = Math.abs(Collections.binarySearch(highlightList.subList(0, i), highlightList.get(i)) + 1);
        Collections.rotate(highlightList.subList(j, i + 1), j - i);
        notifyItemMoved(i, j);
    }
}
 
Example 6
Project: ArchUnit   File: SourceTest.java   View Source Code Vote up 5 votes
private static List<List<?>> createUnequalTestCasesFor(Md5sum... md5sums) {
    List<List<?>> result = new ArrayList<>();
    List<Md5sum> input = ImmutableList.copyOf(md5sums);
    ArrayList<Md5sum> shifting = Lists.newArrayList(md5sums);
    for (int i = 1; i < md5sums.length; i++) {
        Collections.rotate(shifting, 1);
        result.addAll(zip(input, shifting));
    }
    return result;
}
 
Example 7
Project: Cognizant-Intelligent-Test-Scripter   File: AbstractDataModel.java   View Source Code Vote up 5 votes
public Boolean moveRowsUp(int from, int to) {
    if (from - 1 < 0) {
        return false;
    }
    to = to + 1;
    Collections.rotate(records.subList(from - 1, to), -1);
    setSaved(false);
    return true;
}
 
Example 8
Project: Cognizant-Intelligent-Test-Scripter   File: AbstractDataModel.java   View Source Code Vote up 5 votes
public Boolean moveRowsDown(int from, int to) {
    if (to + 1 > records.size() - 1) {
        return false;
    }
    to += 1;
    Collections.rotate(records.subList(from, to + 1), 1);
    setSaved(false);
    return true;
}
 
Example 9
Project: Cognizant-Intelligent-Test-Scripter   File: WebORObject.java   View Source Code Vote up 5 votes
@JsonIgnore
public Boolean moveRowsUp(int from, int to) {
    if (from - 1 < 0) {
        return false;
    }
    to = to + 1;
    Collections.rotate(attributes.subList(from - 1, to), -1);
    changeSave();
    return true;
}
 
Example 10
Project: jijimaku   File: AnnotationService.java   View Source Code Vote up 5 votes
/**
 * Parse a subtitle file and add annotation if dictionary definitions were found.
 *
 * @return true if at least one annotation was added, false otherwise.
 */
public String[] annotateSubtitleFile(String fileName, String fileContents) throws IOException, FatalParsingException {
  SubtitleFile subtitle = new SubtitleFile(fileName, fileContents, config.getSubtitleStyles());

  // Loop through the subtitle file captions one by one
  while (subtitle.hasNext()) {
    String currentCaptionText = subtitle.nextCaption();
    List<String> colors = new ArrayList<>(config.getHighlightColors());

    // Parse subtitle and lookup definitions
    List<String> alreadyDefinedWords = new ArrayList<>();
    List<String> annotations = new ArrayList<>();
    for (DictionaryMatch match : getFilteredMatches(currentCaptionText)) {
      String color = colors.iterator().next();
      List<String> tokenDefs = annotateDictionaryMatch(match, color);
      if (!tokenDefs.isEmpty() && !alreadyDefinedWords.contains(match.getTextForm())) {
        annotations.addAll(tokenDefs);
        // Set a different color for words that are defined
        subtitle.colorizeCaptionWord(match.getTextForm(), color);
        Collections.rotate(colors, -1);
        alreadyDefinedWords.add(match.getTextForm());
      }
    }
    subtitle.annotate(annotations);
  }

  return subtitle.getNbCaptionAnnotated() == 0 ? null : subtitle.toAssFormat();
}
 
Example 11
Project: Cognizant-Intelligent-Test-Scripter   File: MobileORObject.java   View Source Code Vote up 5 votes
@JsonIgnore
public Boolean moveRowsUp(int from, int to) {
    if (from - 1 < 0) {
        return false;
    }
    to = to + 1;
    Collections.rotate(attributes.subList(from - 1, to), -1);
    changeSave();
    return true;
}
 
Example 12
Project: Cognizant-Intelligent-Test-Scripter   File: MobileORObject.java   View Source Code Vote up 5 votes
@JsonIgnore
public Boolean moveRowsDown(int from, int to) {
    if (to + 1 > attributes.size() - 1) {
        return false;
    }
    to += 1;
    Collections.rotate(attributes.subList(from, to + 1), 1);
    changeSave();
    return true;
}
 
Example 13
Project: athena   File: IntentPerfInstaller.java   View Source Code Vote up 5 votes
private List<NodeId> getNeighbors() {
    List<NodeId> nodes = clusterService.getNodes().stream()
            .map(ControllerNode::id)
            .collect(Collectors.toCollection(ArrayList::new));
    // sort neighbors by id
    Collections.sort(nodes, (node1, node2) ->
            node1.toString().compareTo(node2.toString()));
    // rotate the local node to index 0
    Collections.rotate(nodes, -1 * nodes.indexOf(clusterService.getLocalNode().id()));
    log.debug("neighbors (raw): {}", nodes); //TODO remove
    // generate the sub-list that will contain local node and selected neighbors
    nodes = nodes.subList(0, numNeighbors + 1);
    log.debug("neighbors: {}", nodes); //TODO remove
    return nodes;
}
 
Example 14
Project: Cognizant-Intelligent-Test-Scripter   File: TestCase.java   View Source Code Vote up 5 votes
public Boolean moveRowsDown(int from, int to) {
    if (to + 1 > testSteps.size() - 1) {
        return false;
    }
    to += 1;
    Collections.rotate(testSteps.subList(from, to + 1), 1);
    setSaved(false);
    return true;
}
 
Example 15
Project: Cognizant-Intelligent-Test-Scripter   File: TestSet.java   View Source Code Vote up 5 votes
public Boolean moveRowsDown(int from, int to) {
    if (to + 1 > testSteps.size() - 1) {
        return false;
    }
    to += 1;
    Collections.rotate(testSteps.subList(from, to + 1), 1);
    setSaved(false);
    return true;
}
 
Example 16
Project: openjdk-systemtest   File: SolverTest.java   View Source Code Vote up 4 votes
private void quietRotate() { //Doesn't return, and doesn't update the rotation counter. Use this for the pre-caching, as initial rotation is arbitrary.
	Collections.rotate(contents,1);
}
 
Example 17
Project: OpenJSharp   File: CodingChooser.java   View Source Code Vote up 4 votes
private CodingMethod stressPopCoding(CodingMethod coding) {
    assert(stress != null);  // this method is only for testing
    // Don't turn it into a pop coding if it's already something special.
    if (!(coding instanceof Coding))  return coding;
    Coding valueCoding = ((Coding)coding).getValueCoding();
    Histogram hist = getValueHistogram();
    int fVlen = stressLen(hist.getTotalLength());
    if (fVlen == 0)  return coding;
    List<Integer> popvals = new ArrayList<>();
    if (stress.nextBoolean()) {
        // Build the population from the value list.
        Set<Integer> popset = new HashSet<>();
        for (int i = start; i < end; i++) {
            if (popset.add(values[i]))  popvals.add(values[i]);
        }
    } else {
        int[][] matrix = hist.getMatrix();
        for (int mrow = 0; mrow < matrix.length; mrow++) {
            int[] row = matrix[mrow];
            for (int mcol = 1; mcol < row.length; mcol++) {
                popvals.add(row[mcol]);
            }
        }
    }
    int reorder = stress.nextInt();
    if ((reorder & 7) <= 2) {
        // Lose the order.
        Collections.shuffle(popvals, stress);
    } else {
        // Keep the order, mostly.
        if (((reorder >>>= 3) & 7) <= 2)  Collections.sort(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.reverse(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.rotate(popvals, stressLen(popvals.size()));
    }
    if (popvals.size() > fVlen) {
        // Cut the list down.
        if (((reorder >>>= 3) & 7) <= 2) {
            // Cut at end.
            popvals.subList(fVlen,   popvals.size()).clear();
        } else {
            // Cut at start.
            popvals.subList(0, popvals.size()-fVlen).clear();
        }
    }
    fVlen = popvals.size();
    int[] fvals = new int[1+fVlen];
    for (int i = 0; i < fVlen; i++) {
        fvals[1+i] = (popvals.get(i)).intValue();
    }
    PopulationCoding pop = new PopulationCoding();
    pop.setFavoredValues(fvals, fVlen);
    int[] lvals = PopulationCoding.LValuesCoded;
    for (int i = 0; i < lvals.length / 2; i++) {
        int popl = lvals[stress.nextInt(lvals.length)];
        if (popl < 0)  continue;
        if (PopulationCoding.fitTokenCoding(fVlen, popl) != null) {
            pop.setL(popl);
            break;
        }
    }
    if (pop.tokenCoding == null) {
        int lmin = fvals[1], lmax = lmin;
        for (int i = 2; i <= fVlen; i++) {
            int val = fvals[i];
            if (lmin > val)  lmin = val;
            if (lmax < val)  lmax = val;
        }
        pop.tokenCoding = stressCoding(lmin, lmax);
    }

    computePopSizePrivate(pop, valueCoding, valueCoding);
    return pop;
}
 
Example 18
Project: Cognizant-Intelligent-Test-Scripter   File: TestsetComponent.java   View Source Code Vote up 4 votes
private void sort() {
    Collections.sort(getCurrentTestSet().getTestSteps(), getStepComparator());
    Collections.rotate(getCurrentTestSet().getTestSteps(), getStatusIndex());
    getCurrentTestSet().fireTableDataChanged();
    currentStatus = ++currentStatus % statuses.length;
}
 
Example 19
Project: openjdk-jdk10   File: CodingChooser.java   View Source Code Vote up 4 votes
private CodingMethod stressPopCoding(CodingMethod coding) {
    assert(stress != null);  // this method is only for testing
    // Don't turn it into a pop coding if it's already something special.
    if (!(coding instanceof Coding))  return coding;
    Coding valueCoding = ((Coding)coding).getValueCoding();
    Histogram hist = getValueHistogram();
    int fVlen = stressLen(hist.getTotalLength());
    if (fVlen == 0)  return coding;
    List<Integer> popvals = new ArrayList<>();
    if (stress.nextBoolean()) {
        // Build the population from the value list.
        Set<Integer> popset = new HashSet<>();
        for (int i = start; i < end; i++) {
            if (popset.add(values[i]))  popvals.add(values[i]);
        }
    } else {
        int[][] matrix = hist.getMatrix();
        for (int mrow = 0; mrow < matrix.length; mrow++) {
            int[] row = matrix[mrow];
            for (int mcol = 1; mcol < row.length; mcol++) {
                popvals.add(row[mcol]);
            }
        }
    }
    int reorder = stress.nextInt();
    if ((reorder & 7) <= 2) {
        // Lose the order.
        Collections.shuffle(popvals, stress);
    } else {
        // Keep the order, mostly.
        if (((reorder >>>= 3) & 7) <= 2)  Collections.sort(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.reverse(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.rotate(popvals, stressLen(popvals.size()));
    }
    if (popvals.size() > fVlen) {
        // Cut the list down.
        if (((reorder >>>= 3) & 7) <= 2) {
            // Cut at end.
            popvals.subList(fVlen,   popvals.size()).clear();
        } else {
            // Cut at start.
            popvals.subList(0, popvals.size()-fVlen).clear();
        }
    }
    fVlen = popvals.size();
    int[] fvals = new int[1+fVlen];
    for (int i = 0; i < fVlen; i++) {
        fvals[1+i] = (popvals.get(i)).intValue();
    }
    PopulationCoding pop = new PopulationCoding();
    pop.setFavoredValues(fvals, fVlen);
    int[] lvals = PopulationCoding.LValuesCoded;
    for (int i = 0; i < lvals.length / 2; i++) {
        int popl = lvals[stress.nextInt(lvals.length)];
        if (popl < 0)  continue;
        if (PopulationCoding.fitTokenCoding(fVlen, popl) != null) {
            pop.setL(popl);
            break;
        }
    }
    if (pop.tokenCoding == null) {
        int lmin = fvals[1], lmax = lmin;
        for (int i = 2; i <= fVlen; i++) {
            int val = fvals[i];
            if (lmin > val)  lmin = val;
            if (lmax < val)  lmax = val;
        }
        pop.tokenCoding = stressCoding(lmin, lmax);
    }

    computePopSizePrivate(pop, valueCoding, valueCoding);
    return pop;
}
 
Example 20
Project: jdk8u-jdk   File: CodingChooser.java   View Source Code Vote up 4 votes
private CodingMethod stressPopCoding(CodingMethod coding) {
    assert(stress != null);  // this method is only for testing
    // Don't turn it into a pop coding if it's already something special.
    if (!(coding instanceof Coding))  return coding;
    Coding valueCoding = ((Coding)coding).getValueCoding();
    Histogram hist = getValueHistogram();
    int fVlen = stressLen(hist.getTotalLength());
    if (fVlen == 0)  return coding;
    List<Integer> popvals = new ArrayList<>();
    if (stress.nextBoolean()) {
        // Build the population from the value list.
        Set<Integer> popset = new HashSet<>();
        for (int i = start; i < end; i++) {
            if (popset.add(values[i]))  popvals.add(values[i]);
        }
    } else {
        int[][] matrix = hist.getMatrix();
        for (int mrow = 0; mrow < matrix.length; mrow++) {
            int[] row = matrix[mrow];
            for (int mcol = 1; mcol < row.length; mcol++) {
                popvals.add(row[mcol]);
            }
        }
    }
    int reorder = stress.nextInt();
    if ((reorder & 7) <= 2) {
        // Lose the order.
        Collections.shuffle(popvals, stress);
    } else {
        // Keep the order, mostly.
        if (((reorder >>>= 3) & 7) <= 2)  Collections.sort(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.reverse(popvals);
        if (((reorder >>>= 3) & 7) <= 2)  Collections.rotate(popvals, stressLen(popvals.size()));
    }
    if (popvals.size() > fVlen) {
        // Cut the list down.
        if (((reorder >>>= 3) & 7) <= 2) {
            // Cut at end.
            popvals.subList(fVlen,   popvals.size()).clear();
        } else {
            // Cut at start.
            popvals.subList(0, popvals.size()-fVlen).clear();
        }
    }
    fVlen = popvals.size();
    int[] fvals = new int[1+fVlen];
    for (int i = 0; i < fVlen; i++) {
        fvals[1+i] = (popvals.get(i)).intValue();
    }
    PopulationCoding pop = new PopulationCoding();
    pop.setFavoredValues(fvals, fVlen);
    int[] lvals = PopulationCoding.LValuesCoded;
    for (int i = 0; i < lvals.length / 2; i++) {
        int popl = lvals[stress.nextInt(lvals.length)];
        if (popl < 0)  continue;
        if (PopulationCoding.fitTokenCoding(fVlen, popl) != null) {
            pop.setL(popl);
            break;
        }
    }
    if (pop.tokenCoding == null) {
        int lmin = fvals[1], lmax = lmin;
        for (int i = 2; i <= fVlen; i++) {
            int val = fvals[i];
            if (lmin > val)  lmin = val;
            if (lmax < val)  lmax = val;
        }
        pop.tokenCoding = stressCoding(lmin, lmax);
    }

    computePopSizePrivate(pop, valueCoding, valueCoding);
    return pop;
}