All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.plugins.ws.PluginUpdateAggregator Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.plugins.ws;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.sonar.updatecenter.common.Plugin;
import org.sonar.updatecenter.common.PluginUpdate;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import static java.lang.String.format;

public class PluginUpdateAggregator {

  public Collection aggregate(@Nullable Collection pluginUpdates) {
    if (pluginUpdates == null || pluginUpdates.isEmpty()) {
      return Collections.emptyList();
    }

    Map builders = Maps.newHashMap();
    for (PluginUpdate pluginUpdate : pluginUpdates) {
      Plugin plugin = pluginUpdate.getPlugin();
      PluginUpdateAggregateBuilder builder = builders.get(plugin);
      if (builder == null) {
        builder = PluginUpdateAggregateBuilder.builderFor(plugin);
        builders.put(plugin, builder);
      }
      builder.add(pluginUpdate);
    }

    return Lists.newArrayList(transform(builders.values(), BuilderToPluginUpdateAggregate.INSTANCE));
  }

  private enum BuilderToPluginUpdateAggregate implements Function {
    INSTANCE;

    @Override
    public PluginUpdateAggregate apply(@Nonnull PluginUpdateAggregateBuilder input) {
      return input.build();
    }
  }

  @VisibleForTesting
  static class PluginUpdateAggregateBuilder {
    private final Plugin plugin;

    private final List updates = Lists.newArrayListWithExpectedSize(1);

    // use static method
    private PluginUpdateAggregateBuilder(Plugin plugin) {
      this.plugin = plugin;
    }

    public static PluginUpdateAggregateBuilder builderFor(Plugin plugin) {
      return new PluginUpdateAggregateBuilder(checkNotNull(plugin));
    }

    public PluginUpdateAggregateBuilder add(PluginUpdate pluginUpdate) {
      checkArgument(
        this.plugin.equals(pluginUpdate.getPlugin()),
        format("This builder only accepts PluginUpdate instances for plugin %s", plugin));
      this.updates.add(pluginUpdate);
      return this;
    }

    public PluginUpdateAggregate build() {
      return new PluginUpdateAggregate(this);
    }
  }

  public static class PluginUpdateAggregate {
    private final Plugin plugin;

    private final Collection updates;

    protected PluginUpdateAggregate(PluginUpdateAggregateBuilder builder) {
      this.plugin = builder.plugin;
      this.updates = ImmutableList.copyOf(builder.updates);
    }

    public Plugin getPlugin() {
      return plugin;
    }

    public Collection getUpdates() {
      return updates;
    }

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy