Search in sources :

Example 6 with Ordering

use of com.google.common.collect.Ordering in project gerrit by GerritCodeReview.

the class Schema_124 method fixInvalidSequenceNumbers.

private Collection<AccountSshKey> fixInvalidSequenceNumbers(Collection<AccountSshKey> keys) {
    Ordering<AccountSshKey> o = Ordering.from(comparing(k -> k.getKey().get()));
    List<AccountSshKey> fixedKeys = new ArrayList<>(keys);
    AccountSshKey minKey = o.min(keys);
    while (minKey.getKey().get() <= 0) {
        AccountSshKey fixedKey = new AccountSshKey(new AccountSshKey.Id(minKey.getKey().getParentKey(), Math.max(o.max(keys).getKey().get() + 1, 1)), minKey.getSshPublicKey());
        Collections.replaceAll(fixedKeys, minKey, fixedKey);
        minKey = o.min(fixedKeys);
    }
    return fixedKeys;
}
Also used : VersionedAuthorizedKeys(com.google.gerrit.server.account.VersionedAuthorizedKeys) SimpleSshKeyCreator(com.google.gerrit.server.account.VersionedAuthorizedKeys.SimpleSshKeyCreator) AllUsersName(com.google.gerrit.server.config.AllUsersName) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ListMultimap(com.google.common.collect.ListMultimap) MultimapBuilder(com.google.common.collect.MultimapBuilder) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Account(com.google.gerrit.reviewdb.client.Account) ResultSet(java.sql.ResultSet) Map(java.util.Map) Comparator.comparing(java.util.Comparator.comparing) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) Collection(java.util.Collection) IOException(java.io.IOException) NullProgressMonitor(org.eclipse.jgit.lib.NullProgressMonitor) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) Ordering(com.google.common.collect.Ordering) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) Statement(java.sql.Statement) Collections(java.util.Collections) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) ArrayList(java.util.ArrayList)

Example 7 with Ordering

use of com.google.common.collect.Ordering in project android by JetBrains.

the class PreviewIconsPanel method initializeIconComponents.

private void initializeIconComponents(@NotNull Collection<Density> densities) {
    // Sort the densities, so that we always line up icons from highest to lowest densities.
    Ordering<Density> densityOrdering = new Ordering<Density>() {

        @Override
        public int compare(Density left, Density right) {
            // The lower the enum index, the higher the density
            return Ints.compare(left.ordinal(), right.ordinal());
        }
    };
    densities = densityOrdering.immutableSortedCopy(densities);
    for (Density density : densities) {
        JPanel iconPanel = new JPanel(new VerticalFlowLayout(false, false));
        iconPanel.setBackground(myTheme.getMainColor());
        iconPanel.setOpaque(myTheme != Theme.TRANSPARENT);
        JBLabel title = new JBLabel(density.getResourceValue());
        title.setForeground(myTheme.getAltColor());
        iconPanel.add(title);
        ImageComponent iconImage = new ImageComponent(null);
        iconImage.setBorder(new LineBorder(myTheme.getAltColor()));
        iconImage.setOpaque(false);
        iconPanel.add(iconImage);
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.PAGE_START;
        myIconsPanel.add(iconPanel, c);
        myIconImages.put(density, iconImage);
    }
}
Also used : ImageComponent(com.android.tools.idea.ui.ImageComponent) JBLabel(com.intellij.ui.components.JBLabel) LineBorder(javax.swing.border.LineBorder) Ordering(com.google.common.collect.Ordering) Density(com.android.resources.Density) VerticalFlowLayout(com.intellij.openapi.ui.VerticalFlowLayout)

Example 8 with Ordering

use of com.google.common.collect.Ordering in project druid by druid-io.

the class IndexMerger method merge.

public File merge(List<IndexableAdapter> indexes, final boolean rollup, final AggregatorFactory[] metricAggs, File outDir, IndexSpec indexSpec, ProgressIndicator progress) throws IOException {
    FileUtils.deleteDirectory(outDir);
    FileUtils.forceMkdir(outDir);
    final List<String> mergedDimensions = getMergedDimensions(indexes);
    final List<String> mergedMetrics = Lists.transform(mergeIndexed(Lists.newArrayList(FunctionalIterable.create(indexes).transform(new Function<IndexableAdapter, Iterable<String>>() {

        @Override
        public Iterable<String> apply(@Nullable IndexableAdapter input) {
            return input.getMetricNames();
        }
    }))), new Function<String, String>() {

        @Override
        public String apply(@Nullable String input) {
            return input;
        }
    });
    final AggregatorFactory[] sortedMetricAggs = new AggregatorFactory[mergedMetrics.size()];
    for (int i = 0; i < metricAggs.length; i++) {
        AggregatorFactory metricAgg = metricAggs[i];
        int metricIndex = mergedMetrics.indexOf(metricAgg.getName());
        /*
        If metricIndex is negative, one of the metricAggs was not present in the union of metrics from the indices
        we are merging
       */
        if (metricIndex > -1) {
            sortedMetricAggs[metricIndex] = metricAgg;
        }
    }
    /*
      If there is nothing at sortedMetricAggs[i], then we did not have a metricAgg whose name matched the name
      of the ith element of mergedMetrics. I.e. There was a metric in the indices to merge that we did not ask for.
     */
    for (int i = 0; i < sortedMetricAggs.length; i++) {
        if (sortedMetricAggs[i] == null) {
            throw new IAE("Indices to merge contained metric[%s], but requested metrics did not", mergedMetrics.get(i));
        }
    }
    for (int i = 0; i < mergedMetrics.size(); i++) {
        if (!sortedMetricAggs[i].getName().equals(mergedMetrics.get(i))) {
            throw new IAE("Metric mismatch, index[%d] [%s] != [%s]", i, sortedMetricAggs[i].getName(), mergedMetrics.get(i));
        }
    }
    Function<ArrayList<Iterable<Rowboat>>, Iterable<Rowboat>> rowMergerFn = new Function<ArrayList<Iterable<Rowboat>>, Iterable<Rowboat>>() {

        @Override
        public Iterable<Rowboat> apply(@Nullable ArrayList<Iterable<Rowboat>> boats) {
            if (rollup) {
                return CombiningIterable.create(new MergeIterable<Rowboat>(Ordering.<Rowboat>natural().nullsFirst(), boats), Ordering.<Rowboat>natural().nullsFirst(), new RowboatMergeFunction(sortedMetricAggs));
            } else {
                return new MergeIterable<Rowboat>(new Ordering<Rowboat>() {

                    @Override
                    public int compare(Rowboat left, Rowboat right) {
                        return Longs.compare(left.getTimestamp(), right.getTimestamp());
                    }
                }.nullsFirst(), boats);
            }
        }
    };
    return makeIndexFiles(indexes, sortedMetricAggs, outDir, progress, mergedDimensions, mergedMetrics, rowMergerFn, indexSpec);
}
Also used : MergeIterable(io.druid.java.util.common.guava.MergeIterable) FunctionalIterable(io.druid.java.util.common.guava.FunctionalIterable) IndexedIterable(io.druid.segment.data.IndexedIterable) CombiningIterable(io.druid.collections.CombiningIterable) ArrayList(java.util.ArrayList) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) IAE(io.druid.java.util.common.IAE) Function(com.google.common.base.Function) MergeIterable(io.druid.java.util.common.guava.MergeIterable) Ordering(com.google.common.collect.Ordering) Nullable(javax.annotation.Nullable)

Example 9 with Ordering

use of com.google.common.collect.Ordering in project presto by prestodb.

the class StateCompiler method enumerateFields.

/**
     * Enumerates all the fields in this state interface.
     *
     * @param clazz a subclass of AccumulatorState
     * @param fieldTypes a map of field name and Type
     * @return list of state fields. Ordering is guaranteed to be stable, and have all primitive fields at the beginning.
     */
private static List<StateField> enumerateFields(Class<?> clazz, Map<String, Type> fieldTypes) {
    ImmutableList.Builder<StateField> builder = ImmutableList.builder();
    final Set<Class<?>> primitiveClasses = ImmutableSet.of(byte.class, boolean.class, long.class, double.class);
    Set<Class<?>> supportedClasses = getSupportedFieldTypes();
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals("getEstimatedSize")) {
            continue;
        }
        if (method.getName().startsWith("get")) {
            Class<?> type = method.getReturnType();
            checkArgument(supportedClasses.contains(type), type.getName() + " is not supported");
            String name = method.getName().substring(3);
            builder.add(new StateField(name, type, getInitialValue(method), method.getName(), Optional.ofNullable(fieldTypes.get(name))));
        }
        if (method.getName().startsWith("is")) {
            Class<?> type = method.getReturnType();
            checkArgument(type == boolean.class, "Only boolean is support for 'is' methods");
            String name = method.getName().substring(2);
            builder.add(new StateField(name, type, getInitialValue(method), method.getName(), Optional.of(BOOLEAN)));
        }
    }
    // We need this ordering because the serializer and deserializer are on different machines, and so the ordering of fields must be stable
    Ordering<StateField> ordering = new Ordering<StateField>() {

        @Override
        public int compare(StateField left, StateField right) {
            if (primitiveClasses.contains(left.getType()) && !primitiveClasses.contains(right.getType())) {
                return -1;
            }
            if (primitiveClasses.contains(right.getType()) && !primitiveClasses.contains(left.getType())) {
                return 1;
            }
            // If they're the category, just sort by name
            return left.getName().compareTo(right.getName());
        }
    };
    List<StateField> fields = ordering.sortedCopy(builder.build());
    checkInterface(clazz, fields);
    return fields;
}
Also used : ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Ordering(com.google.common.collect.Ordering) BytecodeExpressions.constantClass(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass) CompilerUtils.defineClass(com.facebook.presto.bytecode.CompilerUtils.defineClass) Method(java.lang.reflect.Method)

Example 10 with Ordering

use of com.google.common.collect.Ordering in project gerrit by GerritCodeReview.

the class VersionedAuthorizedKeys method setKeys.

/**
   * Sets new SSH keys.
   *
   * <p>The existing SSH keys are overwritten.
   *
   * @param newKeys the new public SSH keys
   */
public void setKeys(Collection<AccountSshKey> newKeys) {
    Ordering<AccountSshKey> o = Ordering.from(comparing(k -> k.getKey().get()));
    keys = new ArrayList<>(Collections.nCopies(o.max(newKeys).getKey().get(), Optional.empty()));
    for (AccountSshKey key : newKeys) {
        keys.set(key.getKey().get() - 1, Optional.of(key));
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) VersionedMetaData(com.google.gerrit.server.git.VersionedMetaData) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) SshKeyCreator(com.google.gerrit.server.ssh.SshKeyCreator) ArrayList(java.util.ArrayList) Assisted(com.google.inject.assistedinject.Assisted) Strings(com.google.common.base.Strings) Account(com.google.gerrit.reviewdb.client.Account) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Comparator.comparing(java.util.Comparator.comparing) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) Collection(java.util.Collection) IOException(java.io.IOException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) List(java.util.List) Ordering(com.google.common.collect.Ordering) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Optional(java.util.Optional) InvalidSshKeyException(com.google.gerrit.common.errors.InvalidSshKeyException) Id(com.google.gerrit.reviewdb.client.AccountSshKey.Id) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey)

Aggregations

Ordering (com.google.common.collect.Ordering)10 List (java.util.List)4 Function (com.google.common.base.Function)2 Strings (com.google.common.base.Strings)2 ImmutableList (com.google.common.collect.ImmutableList)2 Account (com.google.gerrit.reviewdb.client.Account)2 AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)2 AllUsersName (com.google.gerrit.server.config.AllUsersName)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)2 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 ISE (io.druid.java.util.common.ISE)2 MergeIterable (io.druid.java.util.common.guava.MergeIterable)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Density (com.android.resources.Density)1 ImageComponent (com.android.tools.idea.ui.ImageComponent)1 CompilerUtils.defineClass (com.facebook.presto.bytecode.CompilerUtils.defineClass)1 BytecodeExpressions.constantClass (com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass)1