Example usage for com.google.common.collect ImmutableSortedSet copyOf

List of usage examples for com.google.common.collect ImmutableSortedSet copyOf

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedSet copyOf.

Prototype

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> copyOf(E[] elements) 

Source Link

Usage

From source file:org.apache.druid.query.materializedview.DataSourceOptimizer.java

/**
 * Do main work about materialized view selection: transform user query to one or more sub-queries.
 * /*  w w  w  .  java2s.c  o  m*/
 * In the sub-query, the dataSource is the derivative of dataSource in user query, and sum of all sub-queries' 
 * intervals equals the interval in user query
 * 
 * Derived dataSource with smallest average data size per segment granularity have highest priority to replace the
 * datasource in user query
 * 
 * @param query only TopNQuery/TimeseriesQuery/GroupByQuery can be optimized
 * @return a list of queries with specified derived dataSources and intervals 
 */
public List<Query> optimize(Query query) {
    long start = System.currentTimeMillis();
    // only topN/timeseries/groupby query can be optimized
    // only TableDataSource can be optimiezed
    if (!(query instanceof TopNQuery || query instanceof TimeseriesQuery || query instanceof GroupByQuery)
            || !(query.getDataSource() instanceof TableDataSource)) {
        return Collections.singletonList(query);
    }
    String datasourceName = ((TableDataSource) query.getDataSource()).getName();
    // get all derivatives for datasource in query. The derivatives set is sorted by average size of 
    // per segment granularity.
    Set<DerivativeDataSource> derivatives = DerivativeDataSourceManager.getDerivatives(datasourceName);

    if (derivatives.isEmpty()) {
        return Collections.singletonList(query);
    }
    lock.readLock().lock();
    try {
        totalCount.putIfAbsent(datasourceName, new AtomicLong(0));
        hitCount.putIfAbsent(datasourceName, new AtomicLong(0));
        costTime.putIfAbsent(datasourceName, new AtomicLong(0));
        totalCount.get(datasourceName).incrementAndGet();

        // get all fields which the query required
        Set<String> requiredFields = MaterializedViewUtils.getRequiredFields(query);

        Set<DerivativeDataSource> derivativesWithRequiredFields = Sets.newHashSet();
        for (DerivativeDataSource derivativeDataSource : derivatives) {
            derivativesHitCount.putIfAbsent(derivativeDataSource.getName(), new AtomicLong(0));
            if (derivativeDataSource.getColumns().containsAll(requiredFields)) {
                derivativesWithRequiredFields.add(derivativeDataSource);
            }
        }
        // if no derivatives contains all required dimensions, this materialized view selection failed.
        if (derivativesWithRequiredFields.isEmpty()) {
            missFields.putIfAbsent(datasourceName, new ConcurrentHashMap<>());
            missFields.get(datasourceName).putIfAbsent(requiredFields, new AtomicLong(0));
            missFields.get(datasourceName).get(requiredFields).incrementAndGet();
            costTime.get(datasourceName).addAndGet(System.currentTimeMillis() - start);
            return Collections.singletonList(query);
        }

        List<Query> queries = Lists.newArrayList();
        List<Interval> remainingQueryIntervals = (List<Interval>) query.getIntervals();

        for (DerivativeDataSource derivativeDataSource : ImmutableSortedSet
                .copyOf(derivativesWithRequiredFields)) {
            final List<Interval> derivativeIntervals = remainingQueryIntervals.stream()
                    .flatMap(interval -> serverView
                            .getTimeline((new TableDataSource(derivativeDataSource.getName()))).lookup(interval)
                            .stream().map(TimelineObjectHolder::getInterval))
                    .collect(Collectors.toList());
            // if the derivative does not contain any parts of intervals in the query, the derivative will
            // not be selected. 
            if (derivativeIntervals.isEmpty()) {
                continue;
            }

            remainingQueryIntervals = MaterializedViewUtils.minus(remainingQueryIntervals, derivativeIntervals);
            queries.add(query.withDataSource(new TableDataSource(derivativeDataSource.getName()))
                    .withQuerySegmentSpec(new MultipleIntervalSegmentSpec(derivativeIntervals)));
            derivativesHitCount.get(derivativeDataSource.getName()).incrementAndGet();
            if (remainingQueryIntervals.isEmpty()) {
                break;
            }
        }

        if (queries.isEmpty()) {
            costTime.get(datasourceName).addAndGet(System.currentTimeMillis() - start);
            return Collections.singletonList(query);
        }

        //after materialized view selection, the result of the remaining query interval will be computed based on
        // the original datasource. 
        if (!remainingQueryIntervals.isEmpty()) {
            queries.add(query.withQuerySegmentSpec(new MultipleIntervalSegmentSpec(remainingQueryIntervals)));
        }
        hitCount.get(datasourceName).incrementAndGet();
        costTime.get(datasourceName).addAndGet(System.currentTimeMillis() - start);
        return queries;
    } finally {
        lock.readLock().unlock();
    }
}

From source file:org.sonar.updatecenter.common.Release.java

public Set<Release> getIncomingDependencies() {
    return ImmutableSortedSet.copyOf(incomingDependencies);
}

From source file:com.outerspacecat.icalendar.RDate.java

/**
 * Creates a new recurrence date. Exactly one of {@code dates},
 * {@code dateTimes}, or {@code periods} must be specified.
 * /*from  ww w.j a  va  2 s. c  o  m*/
 * @param dates the recurrence dates. May be {@code null}, if non {@code null}
 *        must contain one or more elements, all elements must be non
 *        {@code null}.
 * @param dateTimes the recurrence date-times. May be {@code null}, if non
 *        {@code null} must contain one or more elements, all elements must be
 *        non {@code null}.
 * @param periods the recurrence periods. May be {@code null}, if non
 *        {@code null} must contain one or more elements, all elements must be
 *        non {@code null}. All periods must return the same value for
 *        {@link PeriodType#isUtc()}. All periods must return {@code false}
 *        for {@link PeriodType#isUtc()} if {@code timeZone} is non
 *        {@code null}.
 * @param timeZone the time zone of {@code dateTimes} or {@code periods}. May
 *        be {@code null}. Must be {@code null} if {@code dates} is non
 *        {@code null}. Must be {@code null} if {@code periods} is specified
 *        and all periods return {@code true} for {@link PeriodType#isUtc()}.
 * @param extraParameters the extra parameters of the recurrence date. Must be
 *        non {@code null}, all elements must be non {@code null}, may be
 *        empty.
 */
public RDate(final Iterable<LocalDate> dates, final Iterable<LocalDateTime> dateTimes,
        final Iterable<PeriodType> periods, final ZoneId timeZone, final Iterable<Parameter> extraParameters) {
    if (dates != null) {
        Preconditions.checkArgument(dates.iterator().hasNext(), "dates must be non empty");
        for (LocalDate d : dates)
            Preconditions.checkNotNull(d, "each date must be non null");
        Preconditions.checkArgument(dateTimes == null, "cannot specify date-times with dates");
        Preconditions.checkArgument(timeZone == null, "cannot specify time zone with dates");
        Preconditions.checkArgument(periods == null, "cannot specify periods with dates");
    } else if (dateTimes != null) {
        Preconditions.checkArgument(dateTimes.iterator().hasNext(), "date-times must be non empty");
        for (LocalDateTime d : dateTimes)
            Preconditions.checkNotNull(d, "each date-time must be non null");
        Preconditions.checkArgument(dates == null, "cannot specify dates with date-times");
        Preconditions.checkArgument(periods == null, "cannot specify periods with date-times");
    } else if (periods != null) {
        Preconditions.checkArgument(periods.iterator().hasNext(), "periods must be non empty");
        Boolean utc = null;
        for (PeriodType p : periods) {
            Preconditions.checkNotNull(p, "each period must be non null");
            if (utc == null)
                utc = p.isUtc();
            if (utc != p.isUtc())
                throw new IllegalArgumentException("all periods must have the same UTC property");
        }
        if (utc && timeZone != null)
            throw new IllegalArgumentException("cannot specify time zone with UTC periods");
        Preconditions.checkArgument(dates == null, "cannot specify dates with periods");
        Preconditions.checkArgument(dateTimes == null, "cannot specify date-times with periods");
    } else {
        throw new IllegalArgumentException("must specify dates, date-times, or periods");
    }
    if (timeZone != null)
        Preconditions.checkArgument(ParameterValue.isValidValue(timeZone.getId()),
                "timeZone id cannot be represented in iCalendar: " + timeZone.getId());
    Preconditions.checkNotNull(extraParameters, "extraParameters required");

    this.dates = dates != null ? ImmutableSortedSet.copyOf(dates) : null;
    this.dateTimes = dateTimes != null ? ImmutableSortedSet.copyOf(dateTimes) : null;
    this.periods = periods != null ? ImmutableSortedSet.copyOf(periods) : null;
    this.timeZone = timeZone;

    ImmutableMap.Builder<String, Parameter> extraParametersBuilder = ImmutableMap.builder();
    for (Parameter param : extraParameters)
        extraParametersBuilder.put(param.getName(), param);
    this.extraParameters = extraParametersBuilder.build();
}

From source file:com.facebook.buck.rules.coercer.AbstractSourceSortedSet.java

private static SourceSortedSet concatUnnamed(Iterable<SourceSortedSet> elements) {
    SortedSet<SourcePath> unnamedSources = new TreeSet<>();

    for (SourceSortedSet element : elements) {
        Preconditions.checkState(element.getType().equals(Type.UNNAMED),
                "Expected unnamed source list, got: %s", element.getType());
        element.getUnnamedSources().ifPresent(unnamedSources::addAll);
    }//from   www.  j a  v a 2 s  .c o  m

    return ofUnnamedSources(ImmutableSortedSet.copyOf(unnamedSources));
}

From source file:com.opengamma.strata.basics.date.ImmutableHolidayCalendar.java

/**
 * Obtains a combined holiday calendar instance.
 * <p>/*from w ww . j  av a 2  s .com*/
 * This combines the two input calendars.
 * It is intended for up-front occasional use rather than continuous use, as it is relatively slow.
 * 
 * @param cal1  the first calendar
 * @param cal2  the second calendar
 * @return the combined calendar
 */
public static ImmutableHolidayCalendar combined(ImmutableHolidayCalendar cal1, ImmutableHolidayCalendar cal2) {
    // do not override combinedWith(), as this is too slow
    if (cal1 == cal2) {
        return ArgChecker.notNull(cal1, "cal1");
    }
    ImmutableSortedSet<LocalDate> newHolidays = ImmutableSortedSet
            .copyOf(Iterables.concat(cal1.holidays, cal2.holidays));
    ImmutableSet<DayOfWeek> newWeekends = ImmutableSet
            .copyOf(Iterables.concat(cal1.weekendDays, cal2.weekendDays));
    return new ImmutableHolidayCalendar(cal1.id.combinedWith(cal2.id), newHolidays, newWeekends);
}

From source file:com.opengamma.strata.basics.currency.MultiCurrencyAmount.java

/**
 * Returns a collector that can be used to create a multi-currency amount from a stream of amounts.
 * <p>//from w  ww.j a v a 2 s  .c om
 * If the input contains the same currency more than once, the amounts are added together.
 * For example, an input of (EUR 100, EUR 200, CAD 100) would result in (EUR 300, CAD 100).
 *
 * @return the collector
 */
public static Collector<CurrencyAmount, ?, MultiCurrencyAmount> toMultiCurrencyAmount() {
    return Collector.<CurrencyAmount, Map<Currency, CurrencyAmount>, MultiCurrencyAmount>of(
            // accumulate into a map
            HashMap::new,
            // merge two CurrencyAmounts if same currency
            (map, ca) -> map.merge(ArgChecker.notNull(ca, "amount").getCurrency(), ca, CurrencyAmount::plus),
            // combine two maps
            (map1, map2) -> {
                map2.values().forEach((ca2) -> map1.merge(ca2.getCurrency(), ca2, CurrencyAmount::plus));
                return map1;
            },
            // convert to MultiCurrencyAmount
            map -> new MultiCurrencyAmount(ImmutableSortedSet.copyOf(map.values())), UNORDERED);
}

From source file:net.navatwo.jfxproperties.PropertyObject.java

/**
 * Initialize a {@link PropertyObject}./* w  w w  . j  a  va2  s . c  o  m*/
 * <br/>
 * <p>
 * This should only be called from {@link Builder}.
 *
 * @param fields  Fields found in the hierarchy
 * @param methods Methods found in the hierarchy
 */
@SuppressWarnings("unchecked")
PropertyObject(TypeToken<T> base, Collection<? extends PropertyObject<? super T>> supers,
        Set<String> ignoredProperties, Map<String, TypeToken<?>> types, Map<String, Field> fields,
        Map<String, EnumMap<MethodType, Invokable<T, ?>>> methods) {
    this.base = checkNotNull(base, "base == null");
    this.hashCode = base.hashCode();
    this.supers = checkNotNull(supers, "supers == null");

    // Collect all of the properties from the immediate super collectors, these will have been initialized with
    // their super ignored properties as well.
    ImmutableSortedSet.Builder<String> ignoredPropertiesBuilder = ImmutableSortedSet.naturalOrder();
    ignoredPropertiesBuilder.addAll(ignoredProperties);
    supers.stream().flatMap(s -> s.getIgnoredProperties().stream()).forEach(ignoredPropertiesBuilder::add);
    this.ignoredProperties = ignoredPropertiesBuilder.build();

    // now we need to go through and create a mapping of property names to the accessing methods/fields
    // do a union on the keys this gives us all the property names that have been found
    ImmutableMap.Builder<String, PropertyInfo<?>> propertyMapBuilder = ImmutableMap.builder();
    Sets.union(methods.keySet(), fields.keySet()).stream()
            .filter(propName -> !this.ignoredProperties.contains(propName)).forEach(propName -> {
                // Now build the appropriate PropertyInfo<> implementation dependant on the type of the Field.
                // We can use the primitive versions when it will be faster for them to execute later.

                TypeToken<?> propType = types.get(propName).unwrap();

                EnumMap<MethodType, Invokable<T, ?>> mmap = methods.getOrDefault(propName,
                        new EnumMap<>(MethodType.class));

                Field field = fields.get(propName);
                Invokable<T, ? extends ReadOnlyProperty<?>> accessor = (Invokable<T, ? extends ReadOnlyProperty<?>>) mmap
                        .get(MethodType.ACCESSOR);
                Invokable<T, ?> getter = mmap.get(MethodType.GETTER);
                Invokable<T, Void> setter = (Invokable<T, Void>) mmap.get(MethodType.SETTER);

                if (getter != null || setter != null || accessor != null) {

                    PropertyInfoExtractor piExtract = new PropertyInfoExtractor(propName, base, propType, field,
                            getter, setter, accessor);
                    TypeAcceptor.accept(propType, piExtract);

                    propertyMapBuilder.put(propName, piExtract.getPropertyInfo());
                }
            });

    this.localProperties = propertyMapBuilder.build();
    this.thisNames = ImmutableSortedSet.copyOf(localProperties.keySet());

    supers.stream().flatMap(s -> s.getProperties().entrySet().stream())
            .filter(e -> !this.thisNames.contains(e.getKey()))
            .filter(e -> !this.ignoredProperties.contains(e.getKey()))
            .forEach(e -> propertyMapBuilder.put(e.getKey(), e.getValue()));

    // Now create the immutable structures required and store them.
    allProperties = propertyMapBuilder.build();
    allNames = ImmutableSortedSet.copyOf(allProperties.keySet());
}

From source file:com.google.enterprise.connector.persist.MigrateStore.java

@Override
public void run(CommandLine commandLine) throws Exception {
    initStandAloneContext(false);/*from ww  w .ja v  a  2 s  . c  o  m*/
    // Since we did not start the Context, we need to init TypeMap
    // for PersistentStores to function correctly.
    getTypeMap().init();

    try {
        // If user asks for a list of available PersitentStores,
        // print it and exit.
        if (commandLine.hasOption("list")) {
            listStores();
            return;
        }

        // Get then names of the source and destination PersitentStores.
        String sourceName = null;
        String destName = null;
        String[] args = commandLine.getArgs();
        if ((args.length == 1) || (args.length > 2)) {
            printUsageAndExit(-1);
        }
        if (args.length == 2) {
            sourceName = args[0];
            destName = args[1];
        } else {
            Collection<String> storeNames = getStoreNames();
            sourceName = selectStoreName("source", storeNames);
            if (sourceName == null) {
                return;
            }
            storeNames.remove(sourceName);
            destName = selectStoreName("destination", storeNames);
            if (destName == null) {
                return;
            }
        }
        if (sourceName.equals(destName)) {
            System.err.println("Source and destination PersistentStores must be different.");
            return;
        }

        PersistentStore sourceStore = getPersistentStoreByName(sourceName);

        // Determine which connectors to migrate.
        Collection<String> connectors = null;
        String[] connectorNames = commandLine.getOptionValues('c');
        if (connectorNames != null) {
            connectors = ImmutableSortedSet.copyOf(connectorNames);
        } else if (args.length != 2) {
            // If no connectors were specified on the command line, and we had
            // to prompt the user for the source and destination stores, then also
            // prompt the user for a connector to migrate.
            String name = selectConnectorName(getConnectorNames(sourceStore));
            if (name != null) {
                connectors = ImmutableSortedSet.of(name);
            }
        }

        // Actually perform the migration.
        PersistentStore destStore = getPersistentStoreByName(destName);
        if (sourceStore != null && destStore != null) {
            // Adjust the logging levels so that StoreMigrator messages are logged
            // to the Console.
            Logger.getLogger(StoreMigrator.class.getName()).setLevel(Level.INFO);
            StoreMigrator.migrate(sourceStore, destStore, connectors, commandLine.hasOption("force"));
            StoreMigrator.checkMissing(destStore, connectors);
        }
    } finally {
        shutdown();
    }
}

From source file:com.facebook.buck.rules.DefaultJavaLibraryRule.java

protected DefaultJavaLibraryRule(BuildRuleParams buildRuleParams, Set<String> srcs, Set<String> resources,
        @Nullable String proguardConfig, AnnotationProcessingParams annotationProcessingParams,
        boolean exportDeps, String sourceLevel, String targetLevel) {
    super(buildRuleParams);
    this.srcs = ImmutableSortedSet.copyOf(srcs);
    this.resources = ImmutableSortedSet.copyOf(resources);
    this.annotationProcessingParams = Preconditions.checkNotNull(annotationProcessingParams);
    this.proguardConfig = proguardConfig;
    this.sourceLevel = sourceLevel;
    this.targetLevel = targetLevel;
    this.exportDeps = exportDeps;

    if (!srcs.isEmpty() || !resources.isEmpty()) {
        File file = new File(getOutputJarPath(getBuildTarget()));
        this.outputJar = Optional.of(file);
    } else {//from  w ww . j  av a 2  s.co  m
        this.outputJar = Optional.absent();
    }

    // Note that both srcs and resources are sorted so that the list order is consistent even if
    // the iteration order of the sets passed to the constructor changes. See
    // AbstractBuildRule.getInputsToCompareToOutput() for details.
    inputsToConsiderForCachingPurposes = ImmutableList.<String>builder().addAll(this.srcs)
            .addAll(this.resources).build();

    outputClasspathEntriesSupplier = Suppliers.memoize(new Supplier<ImmutableSet<String>>() {
        @Override
        public ImmutableSet<String> get() {
            ImmutableSet<String> outputClasspathEntries;

            // If this java_library exports its dependencies then just return the transitive
            // dependencies.
            if (DefaultJavaLibraryRule.this.exportDeps) {
                outputClasspathEntries = ImmutableSet.copyOf(getTransitiveClasspathEntries().values());
            } else if (outputJar.isPresent()) {
                outputClasspathEntries = ImmutableSet.of(getOutput().getPath());
            } else {
                outputClasspathEntries = ImmutableSet.of();
            }

            return outputClasspathEntries;
        }
    });

    transitiveClasspathEntriesSupplier = Suppliers
            .memoize(new Supplier<ImmutableSetMultimap<BuildRule, String>>() {
                @Override
                public ImmutableSetMultimap<BuildRule, String> get() {
                    final ImmutableSetMultimap.Builder<BuildRule, String> classpathEntries = ImmutableSetMultimap
                            .builder();
                    ImmutableSetMultimap<BuildRule, String> classpathEntriesForDeps = Classpaths
                            .getClasspathEntries(getDeps());

                    classpathEntries.putAll(classpathEntriesForDeps);

                    if (DefaultJavaLibraryRule.this.exportDeps) {
                        classpathEntries.putAll(DefaultJavaLibraryRule.this, classpathEntriesForDeps.values());
                    }

                    // Only add ourselves to the classpath if there's a jar to be built.
                    if (outputJar.isPresent()) {
                        classpathEntries.putAll(DefaultJavaLibraryRule.this, getOutput().getPath());
                    }

                    return classpathEntries.build();
                }
            });

    declaredClasspathEntriesSupplier = Suppliers
            .memoize(new Supplier<ImmutableSetMultimap<BuildRule, String>>() {
                @Override
                public ImmutableSetMultimap<BuildRule, String> get() {
                    final ImmutableSetMultimap.Builder<BuildRule, String> classpathEntries = ImmutableSetMultimap
                            .builder();

                    Iterable<JavaLibraryRule> javaLibraryDeps = Iterables.filter(
                            Sets.union(getDeps(), ImmutableSet.of(DefaultJavaLibraryRule.this)),
                            JavaLibraryRule.class);

                    for (JavaLibraryRule rule : javaLibraryDeps) {
                        classpathEntries.putAll(rule, rule.getOutputClasspathEntries());
                    }
                    return classpathEntries.build();
                }
            });
}

From source file:com.spotify.heroic.suggest.memory.MemoryBackend.java

@Override
public AsyncFuture<TagSuggest> tagSuggest(final TagSuggest.Request request) {
    final Optional<Set<String>> keys = request.getKey().map(MemoryBackend::analyze);
    final Optional<Set<String>> values = request.getValue().map(MemoryBackend::analyze);

    try (final Stream<TagDocument> docs = lookupTags(request.getFilter())) {
        final Set<TagId> ids = docs.map(TagDocument::getId).collect(Collectors.toSet());

        keys.ifPresent(parts -> parts.forEach(k -> ids.retainAll(tagKeys.getOrDefault(k, ImmutableSet.of()))));

        values.ifPresent(/*from w w w .j a v  a 2s. c  o  m*/
                parts -> parts.forEach(k -> ids.retainAll(tagValues.getOrDefault(k, ImmutableSet.of()))));

        final List<TagSuggest.Suggestion> suggestions = ImmutableList.copyOf(ImmutableSortedSet
                .copyOf(request.getLimit().limitStream(ids.stream()).map(tagIndex::get).filter(v -> v != null)
                        .map(d -> new TagSuggest.Suggestion(SCORE, d.id.key, d.id.value)).iterator()));

        return async.resolved(TagSuggest.of(suggestions));
    }
}