List of usage examples for com.google.common.collect Ordering immutableSortedCopy
public <E extends T> ImmutableList<E> immutableSortedCopy(Iterable<E> elements)
From source file:com.stackframe.bentographer.BentoGrapher.java
/** * @param args the command line arguments *///from w w w.ja v a 2s. c o m public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); File db = findDatabase(); Connection connection = openConnection(db); try { Map<String, Library> libraries = getLibraries(connection); String selected = (String) select(libraries.keySet(), "Choose Library", "Library"); Library library = libraries.get(selected); Collection<Field> fields = getFields(connection, library); fields = Collections2.filter(fields, new Predicate<Field>() { @Override public boolean apply(Field t) { return !ignorableFields.contains(t.type); } }); Comparator<Field> dateComparator = makeTypeComparator("com.filemaker.bento.field.core.date"); Comparator<Field> dateCreatedComparator = makeTypeComparator( "com.filemaker.bento.field.private.timestamp.dateCreated"); Comparator<Field> dateModifiedComparator = makeTypeComparator( "com.filemaker.bento.field.private.timestamp.dateModified"); Ordering<Field> xOrdering = Ordering.from(dateComparator).compound(dateCreatedComparator) .compound(dateModifiedComparator); Collection<Field> sortedXFields = xOrdering.immutableSortedCopy(fields); // FIXME: This depends on the implemenation of Field.toString() returning the type name. It should use a Renderer or something. final Field x = (Field) select(sortedXFields, "Choose X", "X"); fields = Collections2.filter(fields, new Predicate<Field>() { @Override public boolean apply(Field t) { return t != x; } }); Ordering<Field> yOrdering = Ordering.from(Collections.reverseOrder(dateModifiedComparator)) .compound(Collections.reverseOrder(dateCreatedComparator)) .compound(Collections.reverseOrder(dateComparator)); Collection<Field> sortedYFields = yOrdering.immutableSortedCopy(fields); Field y = (Field) select(sortedYFields, "Choose Y", "Y"); // FIXME: Make the rendering of dates smart. It looks like date is seconds since 1/1/2001 and is defined by Core Data. // FIXME: Make the graphs printable. // FIXME: Make it easy to dynamically add more Y values to same graph. // FIXME: Package up a binary that is easy to run. (JNLP/Web Startable?) // FIXME: Publish some screenshots to make it easier to understand what this is for. // FIXME: Make it possible to save graph paramters and automatically bring them back up. // FIXME: Make setting of min as 0 configurable. // FIXME: Fix graph to show actual data points on lines. makeGraph(connection, library, x, y); } finally { connection.close(); } }
From source file:org.grouplens.lenskit.data.history.History.java
/** * Create a history for a particular user. * @param id The user ID./*from www .j a v a2 s. c o m*/ * @param events The events. * @param <E> The root type of the events in the history. * @return A history object. */ @SuppressWarnings("deprecation") @Nonnull public static <E extends Event> UserHistory<E> forUser(long id, List<? extends E> events) { Ordering<Event> ord = Ordering.from(Events.TIMESTAMP_COMPARATOR); if (ord.isOrdered(events)) { return new BasicUserHistory<E>(id, events); } else { return new BasicUserHistory<E>(id, ord.immutableSortedCopy(events)); } }
From source file:org.lenskit.data.history.History.java
/** * Create a history for a particular user. * @param id The user ID./* w w w. ja v a 2 s . c o m*/ * @param events The events. * @param <E> The root type of the events in the history. * @return A history object. */ @SuppressWarnings("deprecation") @Nonnull public static <E extends Event> UserHistory<E> forUser(long id, List<? extends E> events) { Ordering<Event> ord = Ordering.from(Events.TIMESTAMP_COMPARATOR); if (ord.isOrdered(events)) { return new BasicUserHistory<>(id, events); } else { return new BasicUserHistory<>(id, ord.immutableSortedCopy(events)); } }
From source file:net.lldp.checksims.algorithm.similaritymatrix.SimilarityMatrix.java
/** * Generate a similarity matrix from a given set of submissions. * * @param inputSubmissions Submissions to generate from * @param results Results to build from. Must contain results for every possible unordered pair of input submissions * @return Similarity Matrix built from given results * @throws InternalAlgorithmError Thrown on missing results, or results containing a submission not in the input *//* w w w .ja v a 2 s . co m*/ public static SimilarityMatrix generateMatrix(Set<Submission> inputSubmissions, Set<AlgorithmResults> results) throws InternalAlgorithmError { checkNotNull(inputSubmissions); checkNotNull(results); checkArgument(!inputSubmissions.isEmpty(), "Must provide at least 1 submission to build matrix from"); checkArgument(!results.isEmpty(), "Must provide at least 1 AlgorithmResults to build matrix from!"); // Generate the matrix we'll use AlgorithmResults[][] matrix = new AlgorithmResults[inputSubmissions.size()][inputSubmissions.size()]; //Ordering sortBy = Ordering.natural(); Ordering<Submission> sortBy = Ordering.from(new Comparator<Submission>() { public int compare(Submission a, Submission b) { return ((Double) b.getTotalCopyScore()).compareTo(a.getTotalCopyScore()); } }); // Order the submissions List<Submission> orderedSubmissions = sortBy.immutableSortedCopy(inputSubmissions); // Generate the matrix // Start with the diagonal, filling with 100% similarity for (int i = 0; i < orderedSubmissions.size(); i++) { Submission s = orderedSubmissions.get(i); matrix[i][i] = new AlgorithmResults(Pair.of(s, s), Real.ONE, Real.ONE); } // Now go through all the results, and build appropriate two MatrixEntry objects for each for (AlgorithmResults result : results) { int aIndex = orderedSubmissions.indexOf(result.a); int bIndex = orderedSubmissions.indexOf(result.b); if (aIndex == -1) { if (!result.a.testFlag("invalid")) { throw new InternalAlgorithmError( "Processed Algorithm Result with submission not in given input submissions with name \"" + result.a.getName() + "\""); } } else if (bIndex == -1) { if (!result.b.testFlag("invalid")) { throw new InternalAlgorithmError( "Processed Algorithm Result with submission not in given input submissions with name \"" + result.b.getName() + "\""); } } else { matrix[aIndex][bIndex] = result.inverse(); matrix[bIndex][aIndex] = result; } } // Verification pass: Go through and ensure that the entire array was populated for (int x = 0; x < orderedSubmissions.size(); x++) { for (int y = 0; y < orderedSubmissions.size(); y++) { if (matrix[x][y] == null) { throw new InternalAlgorithmError("Missing Algorithm Results for comparison of submissions \"" + orderedSubmissions.get(x).getName() + "\" and \"" + orderedSubmissions.get(y).getName() + "\""); } } } return new SimilarityMatrix(matrix, orderedSubmissions, orderedSubmissions, results); }
From source file:org.sonar.server.measure.ws.ComponentTreeSort.java
public static List<ComponentDto> sortComponents(List<ComponentDto> components, ComponentTreeWsRequest wsRequest, List<MetricDto> metrics, Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric) { List<String> sortParameters = wsRequest.getSort(); if (sortParameters == null || sortParameters.isEmpty()) { return components; }//from ww w. j a v a 2 s. c o m boolean isAscending = wsRequest.getAsc(); Map<String, Ordering<ComponentDto>> orderingsBySortField = ImmutableMap .<String, Ordering<ComponentDto>>builder().put(NAME_SORT, componentNameOrdering(isAscending)) .put(QUALIFIER_SORT, componentQualifierOrdering(isAscending)) .put(PATH_SORT, componentPathOrdering(isAscending)) .put(METRIC_SORT, metricValueOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric)) .put(METRIC_PERIOD_SORT, metricPeriodOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric)) .build(); String firstSortParameter = sortParameters.get(0); Ordering<ComponentDto> primaryOrdering = orderingsBySortField.get(firstSortParameter); if (sortParameters.size() > 1) { for (int i = 1; i < sortParameters.size(); i++) { String secondarySortParameter = sortParameters.get(i); Ordering<ComponentDto> secondaryOrdering = orderingsBySortField.get(secondarySortParameter); primaryOrdering = primaryOrdering.compound(secondaryOrdering); } } return primaryOrdering.immutableSortedCopy(components); }
From source file:org.sonar.server.component.ws.TreeAction.java
public static List<ComponentDto> sortComponents(List<ComponentDto> components, TreeWsRequest wsRequest) { List<String> sortParameters = wsRequest.getSort(); if (sortParameters == null || sortParameters.isEmpty()) { return components; }//from w ww .j a v a2s. c o m boolean isAscending = wsRequest.getAsc(); Map<String, Ordering<ComponentDto>> orderingsBySortField = ImmutableMap .<String, Ordering<ComponentDto>>builder() .put(NAME_SORT, stringOrdering(isAscending, ComponentDto::name)) .put(QUALIFIER_SORT, stringOrdering(isAscending, ComponentDto::qualifier)) .put(PATH_SORT, stringOrdering(isAscending, ComponentDto::path)).build(); String firstSortParameter = sortParameters.get(0); Ordering<ComponentDto> primaryOrdering = orderingsBySortField.get(firstSortParameter); if (sortParameters.size() > 1) { for (int i = 1; i < sortParameters.size(); i++) { String secondarySortParameter = sortParameters.get(i); Ordering<ComponentDto> secondaryOrdering = orderingsBySortField.get(secondarySortParameter); primaryOrdering = primaryOrdering.compound(secondaryOrdering); } } return primaryOrdering.immutableSortedCopy(components); }
From source file:com.replaymod.replaystudio.filter.PacketCountFilter.java
@Override public void onEnd(PacketStream stream, long timestamp) { System.out.println();/*w w w . j a v a2s. c o m*/ System.out.println(); Ordering<Map.Entry<Class<?>, MutableInt>> entryOrdering = Ordering.natural().reverse() .onResultOf(Map.Entry::getValue); for (Map.Entry<Class<?>, MutableInt> e : entryOrdering.immutableSortedCopy(count.entrySet())) { System.out.println(String.format("[%dx] %s", e.getValue().intValue(), e.getKey().getSimpleName())); } System.out.println(); }
From source file:com.github.ferstl.maven.pomenforcers.PedanticModuleOrderEnforcer.java
@Override protected void doEnforce(ErrorReport report) { MavenProject project = EnforcerRuleUtils.getMavenProject(getHelper()); // Do nothing if the project is not a parent project if (!isPomProject(project)) { return;/*from ww w . j a va2s. c om*/ } // Remove all modules to be ignored. List<String> declaredModules = new ArrayList<>(getProjectModel().getModules()); declaredModules.removeAll(this.ignoredModules); // Enforce the module order Ordering<String> moduleOrdering = Ordering.natural(); if (!moduleOrdering.isOrdered(declaredModules)) { reportError(report, declaredModules, moduleOrdering.immutableSortedCopy(declaredModules)); } }
From source file:com.github.ferstl.maven.pomenforcers.PedanticPomSectionOrderEnforcer.java
@Override protected void doEnforce(ErrorReport report) { Node docElement = getPom().getDocumentElement(); NodeList sectionNodes = docElement.getChildNodes(); ArrayList<PomSection> pomSections = new ArrayList<>(); for (int i = 0; i < sectionNodes.getLength(); i++) { Node node = sectionNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { pomSections.add(PomSection.getBySectionName(node.getNodeName())); }/*from ww w . j a v a 2 s. c o m*/ } // The default ordering is the order of the PomSection enum. Ordering<PomSection> ordering = PomSection.createPriorityOrdering(this.sectionPriorities); if (!ordering.isOrdered(pomSections)) { List<PomSection> sortedPomSections = ordering.immutableSortedCopy(pomSections); report.addLine("Your POM has to be organized this way:").emptyLine().addDiff(pomSections, sortedPomSections, "Actual Order", "Required Order", pomSectionToString()); } }
From source file:org.apache.ctakes.assertion.attributes.features.selection.Chi2FeatureSelection.java
@Override public void save(URI uri) throws IOException { if (!this.isTrained) { throw new IllegalStateException("Cannot save before training"); }//w w w . ja va 2 s. com File out = new File(uri); BufferedWriter writer = new BufferedWriter(new FileWriter(out)); Ordering<String> ordering = Ordering.natural().onResultOf(this.chi2Function).reverse(); for (String feature : ordering.immutableSortedCopy(this.selectedFeatureNames)) { writer.append(String.format("%s\t%f\n", feature, this.chi2Function.score(feature))); } writer.close(); }