public class

OptionsBundle

extends java.lang.Object

implements Config

 java.lang.Object

↳androidx.camera.core.impl.OptionsBundle

Subclasses:

MutableOptionsBundle

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

An immutable implementation of Config.

OptionsBundle is a collection of Config.Options and their values which can be queried based on exact Config.Option objects or based on Option ids.

Summary

Fields
protected static final java.util.Comparator<Config.Option>ID_COMPARE

protected final java.util.TreeMap<Config.Option, java.util.Map>mOptions

Methods
public booleancontainsOption(Config.Option<java.lang.Object> id)

public static OptionsBundleemptyBundle()

Create an empty OptionsBundle.

public voidfindOptions(java.lang.String idSearchString, Config.OptionMatcher matcher)

public static OptionsBundlefrom(Config otherConfig)

Create an OptionsBundle from another configuration.

public Config.OptionPrioritygetOptionPriority(Config.Option<java.lang.Object> opt)

public java.util.Set<Config.OptionPriority>getPriorities(Config.Option<java.lang.Object> opt)

public java.util.Set<Config.Option>listOptions()

public java.lang.ObjectretrieveOption(Config.Option<java.lang.Object> id)

public java.lang.ObjectretrieveOption(Config.Option<java.lang.Object> id, java.lang.Object valueIfMissing)

public java.lang.ObjectretrieveOptionWithPriority(Config.Option<java.lang.Object> id, Config.OptionPriority priority)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

protected static final java.util.Comparator<Config.Option> ID_COMPARE

protected final java.util.TreeMap<Config.Option, java.util.Map> mOptions

Methods

public static OptionsBundle from(Config otherConfig)

Create an OptionsBundle from another configuration.

This will copy the options/values from the provided configuration.

Parameters:

otherConfig: Configuration containing options/values to be copied.

Returns:

A new OptionsBundle pre-populated with options/values.

public static OptionsBundle emptyBundle()

Create an empty OptionsBundle.

This options bundle will have no option/value pairs.

Returns:

An OptionsBundle pre-populated with no options/values.

public java.util.Set<Config.Option> listOptions()

public boolean containsOption(Config.Option<java.lang.Object> id)

public java.lang.Object retrieveOption(Config.Option<java.lang.Object> id)

public java.lang.Object retrieveOption(Config.Option<java.lang.Object> id, java.lang.Object valueIfMissing)

public java.lang.Object retrieveOptionWithPriority(Config.Option<java.lang.Object> id, Config.OptionPriority priority)

public Config.OptionPriority getOptionPriority(Config.Option<java.lang.Object> opt)

public void findOptions(java.lang.String idSearchString, Config.OptionMatcher matcher)

public java.util.Set<Config.OptionPriority> getPriorities(Config.Option<java.lang.Object> opt)

Source

/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.core.impl;

import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * An immutable implementation of {@link Config}.
 *
 * <p>OptionsBundle is a collection of {@link Config.Option}s and their values which can be
 * queried based on exact {@link Config.Option} objects or based on Option ids.
 */
public class OptionsBundle implements Config {
    protected static final Comparator<Option<?>> ID_COMPARE =
            (o1, o2) -> {
                return o1.getId().compareTo(o2.getId());
            };
    private static final OptionsBundle EMPTY_BUNDLE =
            new OptionsBundle(new TreeMap<>(ID_COMPARE));

    // TODO: Make these options parcelable
    protected final TreeMap<Option<?>, Map<OptionPriority, Object>> mOptions;

    OptionsBundle(TreeMap<Option<?>, Map<OptionPriority, Object>> options) {
        mOptions = options;
    }

    /**
     * Create an OptionsBundle from another configuration.
     *
     * <p>This will copy the options/values from the provided configuration.
     *
     * @param otherConfig Configuration containing options/values to be copied.
     * @return A new OptionsBundle pre-populated with options/values.
     */
    @NonNull
    public static OptionsBundle from(@NonNull Config otherConfig) {
        // No need to create another instance since OptionsBundle is immutable
        if (OptionsBundle.class.equals(otherConfig.getClass())) {
            return (OptionsBundle) otherConfig;
        }

        TreeMap<Option<?>, Map<OptionPriority, Object>> persistentOptions =
                new TreeMap<>(ID_COMPARE);
        for (Option<?> opt : otherConfig.listOptions()) {
            Set<OptionPriority> priorities = otherConfig.getPriorities(opt);
            Map<OptionPriority, Object> valuesMap = new ArrayMap<>();
            for (OptionPriority priority : priorities) {
                valuesMap.put(priority, otherConfig.retrieveOptionWithPriority(opt, priority));
            }
            persistentOptions.put(opt, valuesMap);
        }

        return new OptionsBundle(persistentOptions);
    }

    /**
     * Create an empty OptionsBundle.
     *
     * <p>This options bundle will have no option/value pairs.
     *
     * @return An OptionsBundle pre-populated with no options/values.
     */
    @NonNull
    public static OptionsBundle emptyBundle() {
        return EMPTY_BUNDLE;
    }

    @Override
    @NonNull
    public Set<Option<?>> listOptions() {
        return Collections.unmodifiableSet(mOptions.keySet());
    }

    @Override
    public boolean containsOption(@NonNull Option<?> id) {
        return mOptions.containsKey(id);
    }

    @Override
    @Nullable
    public <ValueT> ValueT retrieveOption(@NonNull Option<ValueT> id) {
        Map<OptionPriority, Object> values = mOptions.get(id);
        if (values == null) {
            throw new IllegalArgumentException("Option does not exist: " + id);
        }
        OptionPriority highestPrirotiy = Collections.min(values.keySet());

        @SuppressWarnings("unchecked")
        ValueT value = (ValueT) values.get(highestPrirotiy);
        return value;
    }

    @Override
    @Nullable
    @SuppressWarnings("unchecked")
    public <ValueT> ValueT retrieveOption(@NonNull Option<ValueT> id,
            @Nullable ValueT valueIfMissing) {
        try {
            return retrieveOption(id);
        } catch (IllegalArgumentException e) {
            return valueIfMissing;
        }
    }

    @Override
    @Nullable
    public <ValueT> ValueT retrieveOptionWithPriority(@NonNull Option<ValueT> id,
            @NonNull OptionPriority priority) {
        Map<OptionPriority, Object> values = mOptions.get(id);
        if (values == null) {
            throw new IllegalArgumentException("Option does not exist: " + id);
        }
        if (!values.containsKey(priority)) {
            throw new IllegalArgumentException("Option does not exist: " + id + " with priority="
                    + priority);
        }
        @SuppressWarnings("unchecked")
        ValueT value = (ValueT) values.get(priority);
        return value;
    }

    @Override
    @NonNull
    public OptionPriority getOptionPriority(@NonNull Option<?> opt) {
        Map<OptionPriority, Object> values = mOptions.get(opt);
        if (values == null) {
            throw new IllegalArgumentException("Option does not exist: " + opt);
        }
        OptionPriority highestPrirotiy = Collections.min(values.keySet());
        return highestPrirotiy;
    }

    @Override
    public void findOptions(@NonNull String idSearchString, @NonNull OptionMatcher matcher) {
        Option<Void> query = Option.create(idSearchString, Void.class);
        for (Map.Entry<Option<?>, Map<OptionPriority, Object>> entry :
                mOptions.tailMap(query).entrySet()) {
            if (!entry.getKey().getId().startsWith(idSearchString)) {
                // We've reached the end of the range that contains our search stem.
                break;
            }

            Option<?> option = entry.getKey();
            if (!matcher.onOptionMatched(option)) {
                // Caller does not need further results
                break;
            }
        }
    }

    @NonNull
    @Override
    public Set<OptionPriority> getPriorities(@NonNull Option<?> opt) {
        Map<OptionPriority, Object> values = mOptions.get(opt);
        if (values == null) {
            return Collections.emptySet();
        }

        return Collections.unmodifiableSet(values.keySet());
    }
}